View Single Post
Join Date: Jan 2008
Posts: 3,761
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 492
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: Need help not allowing a negative number to be input

 
0
  #10
Nov 18th, 2008
Originally Posted by davids2004 View Post
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:

  1. cout << "Negative number not allowed." << endl;
  2. 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.
Reply With Quote