This is another solution for this challenge.
Problem statement:
In this challenge, write a program that takes in three arguments, a start temperature (in Celsius), an end temperature (in Celsius) and a step size. Print out a table that goes from the start temperature to the end temperature, in steps of the step size; you do not actually need to print the final end temperature if the step size does not exactly match. You should perform input validation: do not accept start temperatures less than a lower limit (which your code should specify as a constant) or higher than an upper limit (which your code should also specify). You should not allow a step size greater than the difference in temperatures.
I want to learn more about C++, so if there is some cool C++ feature I could should have used, please comment (or include it in your answer).
#import <iostream>
#import <cmath>
#define COLUMN_SEPARATOR "\t| "
#define MAX_TEMP 500
#define MIN_TEMP -500
inline bool between(double x, double max, double min) {
return max >= x && min <= x;
}
void getInput(double &lower, double &upper, double &step) {
double temp1, temp2;
std::cout << "Please enter consecutively the upper and lower limits, both between " << MIN_TEMP << " and " << MAX_TEMP << "." << std::endl;
std::cin >> temp1;
std::cin >> temp2;
while (!between(temp1, MAX_TEMP, MIN_TEMP) || !between(temp2, MAX_TEMP, MIN_TEMP)) {
std::cout << "At least one of the temperatures is out of bounds. Please reenter:" << std::endl;
std::cin >> temp1;
std::cin >> temp2;
}
upper = std::max(temp1, temp2);
lower = std::min(temp1, temp2);
std::cout << "Please enter a positive stepsize, smaller than the difference between the limits." << std::endl;
std::cin >> step;
while (step < 0 || step > upper - lower) {
std::cout << "The stepsize is out of bounds. Please reenter:" << std::endl;
std::cin >> step;
}
}
double toFahrenheit(double celsius) {
return celsius*(9/5) + 32;
}
void printTable(double start, double end, double step) {
std::cout << "Celsius" << COLUMN_SEPARATOR << "Fahrenheit" << std::endl;
std::cout << "=======" << COLUMN_SEPARATOR << "==========" << std::endl;
for (double i = start; i < end; i += step) {
std::cout << i << COLUMN_SEPARATOR << toFahrenheit(i) << std::endl;
}
}
int main() {
double start, end, step;
getInput(start, end, step);
printTable(start, end, step);
return 0;
}
Sample run:
192:Challenges 11684$ ./a.out Please enter consecutively the upper and lower limits, both between -500 and 500. 3.692 65.937 Please enter a positive stepsize, smaller than the difference between the upper and lower limit. 5.3729 Celsius | Fahrenheit ======= | ========== 3.692 | 35.692 9.0649 | 41.0649 14.4378 | 46.4378 19.8107 | 51.8107 25.1836 | 57.1836 30.5565 | 62.5565 35.9294 | 67.9294 41.3023 | 73.3023 46.6752 | 78.6752 52.0481 | 84.0481 57.421 | 89.421 62.7939 | 94.7939
#import
? It's supposed to be#include
. \$\endgroup\$#import
is a non-portable#include
. \$\endgroup\$g++ -Wall tempConverter.cpp
. Without warnings/errors/compiler complaints. \$\endgroup\$