| | |
Need help not allowing a negative number to be input
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 43
Reputation:
Solved Threads: 0
•
•
•
•
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:
C++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Problems with displaying total payroll and allowing user to quit by entering a neg. (C++)
Other Threads in the C++ Forum
- Previous Thread: GCC Exception handling w/ threads
- Next Thread: complier needed
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





