DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Need help not allowing a negative number to be input (http://www.daniweb.com/forums/thread157971.html)

davids2004 Nov 18th, 2008 1:05 am
Re: Need help not allowing a negative number to be input
 
Quote:

Originally Posted by VernonDozier (Post 738396)
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.

Thanks for your help. I gave you rep as well.


All times are GMT -4. The time now is 3:26 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC