HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Temperature Converter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
temperatureconverterstackoverflow

Problem

This is an implementation of a practice problem from cprogramming.com: Temperature Converter Challenge

The problem:


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. (This exercise was based on a problem from C Programming Language).

The code:

```
#include
#include
#include
#include

using namespace std;

const double LowestLimit = 0;
const double HighestLimit = 50000;
const string LowerLimitMessage = "Please enter a lower limit, limit >= 0: ";

struct UserInputs
{
public:
double LowerLimit = -1;
double UpperLimit = -1;
double StepAmount = -1;
};

stringstream GetSingleInput(const string message)
{
stringstream returnStream;
string input;
cout > input;
returnStream > parsedInputs.LowerLimit) || parsedInputs.LowerLimit "
> parsedInputs.UpperLimit)
|| parsedInputs.UpperLimit > HighestLimit
|| parsedInputs.UpperLimit "
> parsedInputs.StepAmount)
|| parsedInputs.StepAmount parsedInputs.UpperLimit - parsedInputs.LowerLimit)
{
cout << "Invalid Step Amount!\n";
stepStream = GetSingleInput(stepMessage.str());
}

return parsedInputs;
}

double CelsiusToFarenheit(const double celsius)
{
return celsius * 33.8;
}

int main()
{
auto inputs = GetUserInputs();

cout << "Celsius" << "\t\t\t" << "Farenheit" << "\n";
cout << "----------" << "\t\t" << "----------" << "\n";

Solution

Don't do this:

using namespace std;


In real life it cause more problems than it is worth. So get out of the habit of using it. It will kick your butt one day. Why is “using namespace std;” considered bad practice?

It is unusual to see a stream being returned.

stringstream GetSingleInput(const string message)


But if it works then fine.

Though I would change the return line.

return stringstream(input); // construct and return on single line.
                            // Easier for the compiler to plant
                            // RVO code.


Use of auto:

auto UpperLimitStream = GetSingleInput(UpperLimitMessage.str());


Not sure I like the use of auto here. Types are important to C++. I prefer to use auto where the type does not matter. But here the type sort of does matter (it has to be a stream (or stream like)).

If your application can't fail. Then don't bother with:

return 0;


main() is special and adds this at the end of main. When I see a missing return 0; at the end. It is an indication that the application should not be failing. If I do see a return 0; I start looking around main for the failure cases.

Code Snippets

using namespace std;
stringstream GetSingleInput(const string message)
return stringstream(input); // construct and return on single line.
                            // Easier for the compiler to plant
                            // RVO code.
auto UpperLimitStream = GetSingleInput(UpperLimitMessage.str());

Context

StackExchange Code Review Q#102691, answer score: 11

Revisions (0)

No revisions yet.