patterncppMinor
Setting the smallest number so far from user input
Viewed 0 times
smallestthenumberuserinputsettingfarfrom
Problem
I'm trying to teach myself C++ from Stroustrup's Principles and Practice Using C++ (second edition) and I've gotten to the drill in chapter 4. The problem tells me to make a program that takes in a number with a unit of either feet, inches, meters, and centimeters, as well as log the largest and smallest numbers the user has input. I got the code to work by initializing my smallest number as 0 and making an
Is there a way to either start with an infinitely larger "smallest number" variable so it always changes it in the
if statement check if the input was smaller or 0 (because you can't really have 0 length).Is there a way to either start with an infinitely larger "smallest number" variable so it always changes it in the
if statement, or a way to where it has to do as few if statements as possible? Also, I'd rather not ask for an initial starting number before the while loop.#include "std_lib_facilities.h"
using namespace std;
int main()
{
cout > x >> unit)
{
if((char)x == '|')
break;
cout largest)
{
largest = x;
}
if(x < smallest || smallest == 0)
{
smallest = x;
}
cout << "The smallest number so far is: " <<smallest<< " meters\n";
cout << "The largest number so far is: " << largest << " meters\n";
sum+=x;
}
else
cout << "Invalid unit! Try again." << endl;
}
cout << "The sum of all lengths is: " << sum << " meters\n";
}Solution
Overall, this looks pretty good. It's straightforward and readable, which is great.
Naming
One of the most important things in programming is naming things so that anyone reading the code can figure out the meaning. You've done a pretty good job, but I think you could improve it. Calling a variable
Your constant names are OK, but could be better. (It's great that you're using named constants at all, though!) It would be nice to add that your converting from
Finding the Min and Max
You ask if there's a good constant to initialize the minimum to. I usually use the constant
A Better Loop
You say you don't like having the initial prompt outside of the loop, and I agree. It's good to remind the user what input you're expecting each time through the loop. I'd probably rewrite the loop like this:
Now it will prompt the user each time through the loop, and when they decide to quit, they can simply enter "2" at the prompt.
Naming
One of the most important things in programming is naming things so that anyone reading the code can figure out the meaning. You've done a pretty good job, but I think you could improve it. Calling a variable
x is kind of useless. You could name it something like userValue, or even input so someone reading the code at least knows from the name that it's something that's input by the user.Your constant names are OK, but could be better. (It's great that you're using named constants at all, though!) It would be nice to add that your converting from
cm or in to meters. And there are some common conventions for constants, too. Some people prefer constants to be all capital letters, and others prefer to prefix them with the letter "k" (like it's a mathematical constant). So you could name them CM_TO_METERS or kInToMeters, etc. That way if you add other types of conversions in the future (like converting to Imperial), then you might add kCmToInches or whatever.Finding the Min and Max
You ask if there's a good constant to initialize the minimum to. I usually use the constant
DBL_MAX in this case. (It's defined in `.) If you're looking for only positive numbers, then you can use 0 as the start value for the maximum value. Then any number entered will automatically be greater than that.
Potential Problem
I see one thing that could be a problem. You declare x as a double, but the first thing you do with it is cast it to a char and check if it equals |. That seems really odd, error prone, and hard to understand. If it were me, I would probably refactor this to prompt the user to tell me what they want to do (convert another value, or quit), then after they've told me they want to enter a number, get the numeric input into x`.A Better Loop
You say you don't like having the initial prompt outside of the loop, and I agree. It's good to remind the user what input you're expecting each time through the loop. I'd probably rewrite the loop like this:
bool done = false;
do {
cout > action;
if (action == '2')
{
done = true;
}
else
{
// Your code for getting and converting a number
}
} while (!done);Now it will prompt the user each time through the loop, and when they decide to quit, they can simply enter "2" at the prompt.
Code Snippets
bool done = false;
do {
cout << "What would you like to do?\n";
cout << "1) Convert a value\n";
cout << "2) Quit\n";
char action;
cin >> action;
if (action == '2')
{
done = true;
}
else
{
// Your code for getting and converting a number
}
} while (!done);Context
StackExchange Code Review Q#104987, answer score: 5
Revisions (0)
No revisions yet.