Need help not allowing a negative number to be input

Thread Solved
Reply

Join Date: Nov 2008
Posts: 43
Reputation: davids2004 is an unknown quantity at this point 
Solved Threads: 0
davids2004 davids2004 is offline Offline
Light Poster

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

 
0
  #11
Nov 18th, 2008
Originally Posted by VernonDozier View Post
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.
Thanks for your help. I gave you rep as well.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC