Originally Posted by
davids2004
We have not learned functions yet. What do you mean change the commented parts of the function. I redid some code a little different then what you showed and it works.
Yes, you did the same thing as what I was recommending, just you didn't use functions. What I meant by change the commented sections is to do what you did in your code:
int GetNonNegativeInteger ()
{
int input;
cin >> input;
while (input < 0)
{
// display error message.
// ask for input again
}
return input;
}
Change the red code to this:
cout << "Negative number not allowed." << endl;
cin >> input;
Which is what you have in your code, just slightly different each time, which is correct. If you are not going to use a function, doing it the way you did it is correct and is equivalent. When you learn functions, you'll probably do it the way I did it since you are doing the same thing five times. You did it right.