| | |
Need help not allowing a negative number to be input
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 |
addition api array base based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embed encryption error erroraftercompilation excel file forms fstream function functions game getline givemetehcodez gmail graph gui homework homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix matrix3d memory multiple news node output parameter pointer problem program programming project python random read recursion reference rpg std::coutwstring string strings temperature template test text text-file tree url variable vector video visualization win32 windows winsock word wordfrequency wxwidgets





