patterncppModerate
Temperature conversion table
Viewed 0 times
conversiontemperaturetable
Problem
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).
Sample run:
`192:Challenges 11684$ ./a.out Please en
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
#import
#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 > temp1;
std::cin >> temp2;
while (!between(temp1, MAX_TEMP, MIN_TEMP) || !between(temp2, MAX_TEMP, MIN_TEMP)) {
std::cout > temp1;
std::cin >> temp2;
}
upper = std::max(temp1, temp2);
lower = std::min(temp1, temp2);
std::cout > step;
while (step upper - lower) {
std::cout > 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 en
Solution
Looks like there are no Americans here. 30 degrees Celsius is about 90 degrees Fahrenheit, so something must be wrong in the conversion. Indeed,
(9/5) is evaluated (at compile time) as an integer division, yielding 1. Change it to (9.0/5.0).Context
StackExchange Code Review Q#58410, answer score: 10
Revisions (0)
No revisions yet.