| | |
closing a function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 5
Reputation:
Solved Threads: 0
I am a noob therefore this question is hopefully very easy for someone to answer!
in my code i have asked a question if the answer is yes i want the program to continue onto the next question (this is fine) if however the answer no or enter an incorrect answer then i wasn the program to end! i know about if statements so i am ok with that i just need the line of code i put in the no if statement or the else to make the program finish.
i hope this is clear thank you for any cooperation
in my code i have asked a question if the answer is yes i want the program to continue onto the next question (this is fine) if however the answer no or enter an incorrect answer then i wasn the program to end! i know about if statements so i am ok with that i just need the line of code i put in the no if statement or the else to make the program finish.
i hope this is clear thank you for any cooperation
C++ Syntax (Toggle Plain Text)
int main() { char answer; cout << "Do you feel sexy today?"; cin >> answer; if( answer != 'Y') return 0; // exit program // now do other things }
Last edited by Ancient Dragon; Oct 11th, 2008 at 9:24 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Many programmers don't like putting return statements in the middle of functions. So here's another way to do it
C++ Syntax (Toggle Plain Text)
int main() { char answer; cout << "Do you feel sexy today?"; cin >> answer; if( answer == 'Y') { // now do other things } return 0; // end of function }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Apr 2008
Posts: 63
Reputation:
Solved Threads: 1
For future reference you should also realize that you could do this with a while loop for example if u want to keep going through the loop until a button is pushed that will make the button true as such:
C++ Syntax (Toggle Plain Text)
int main() { while(buttonPushed() == false) { //perform your loop } return 0: }
![]() |
Similar Threads
- dev-c++ Premature Closing (C++)
- Dreamweaver MX 2004 setting Options (Site Layout and Usability)
- body.onload += "XX" (JavaScript / DHTML / AJAX)
- help with closing a 2d array file (C++)
- question about rand() function (C++)
- closing a window (Java)
Other Threads in the C++ Forum
- Previous Thread: multi dimernsion array with classes
- Next Thread: [need help] Initializing Objects in Control Structures
| Thread Tools | Search this Thread |
api application array arrays 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 encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







so simple so noob!