closing a function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 5
Reputation: afromong is an unknown quantity at this point 
Solved Threads: 0
afromong afromong is offline Offline
Newbie Poster

closing a function

 
0
  #1
Oct 11th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: closing a function

 
0
  #2
Oct 11th, 2008
  1. int main()
  2. {
  3. char answer;
  4. cout << "Do you feel sexy today?";
  5. cin >> answer;
  6. if( answer != 'Y')
  7. return 0; // exit program
  8. // now do other things
  9.  
  10. }
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 5
Reputation: afromong is an unknown quantity at this point 
Solved Threads: 0
afromong afromong is offline Offline
Newbie Poster

Re: closing a function

 
0
  #3
Oct 11th, 2008
Brilliant cheers so simple so noob!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: closing a function

 
0
  #4
Oct 11th, 2008
Many programmers don't like putting return statements in the middle of functions. So here's another way to do it
  1. int main()
  2. {
  3. char answer;
  4. cout << "Do you feel sexy today?";
  5. cin >> answer;
  6. if( answer == 'Y')
  7. {
  8. // now do other things
  9.  
  10. }
  11. return 0; // end of function
  12. }
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 63
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training

Re: closing a function

 
0
  #5
Oct 11th, 2008
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:

  1. int main() {
  2. while(buttonPushed() == false) {
  3. //perform your loop
  4. }
  5. return 0:
  6. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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