GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Jun-2009, 21:24
StoryGame464 StoryGame464 is offline
New Member
 
Join Date: Jun 2009
Posts: 2
StoryGame464 is on a distinguished road
Exclamation

C++ If/Else Statement


Okay, im making a "simple" program using C++. In it I have a thig where I have simple yes/no answers, and other things like that. In the begining the user is asked, do you want to start? If someone answers yes, it starts. if they answer no, it sends the to the fatal "Press any btton to exit". how do I do that exactly, so that I do not need 500 lines of code in the "if" statement for yes.
  #2  
Old 06-Jun-2009, 03:53
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 285
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: C++ If/Else Statement


Quote:
Originally Posted by StoryGame464
Okay, im making a "simple" program using C++. In it I have a thig where I have simple yes/no answers, and other things like that. In the begining the user is asked, do you want to start? If someone answers yes, it starts. if they answer no, it sends the to the fatal "Press any btton to exit". how do I do that exactly, so that I do not need 500 lines of code in the "if" statement for yes.
One way is to turn the order of yes and no. In other words, if someone answers no, you quit. If not, you continue. For example:
CPP / C++ / C Code:
// in main()
char answer;
cin >> answer;
if (answer == 'n') // If not continue, exit
    return 0;

// Here comes the rest of the code
// If the user does not enter 'n', the program will continue here,
// no need for else

But the real solution, I think, is to reorganize your program. I would venture a guess that if there really are 500 lines of code in your else part, you don't make much use of functions? Perhaps you're not that far yet, but you might as well take a look at functions, since they really do make your life easier. Does your else part perform some repetitive tasks?

You said you have simple yes/no answers, so I take it your program is asking for input at several places? Do you have that separated into a function?

One solution is to put the whole (or as much as possible) processing of the program into a separate function. Then you could do something like
CPP / C++ / C Code:
if (answer == 'y')
    runProgram(); // All your logic is here
else
    return 0;

... But in the end, that code has to be SOMEWHERE. Does it really make a difference whether it's directly in that if/else part or just in a function called from the if/else part? Or just not in an if/else part at all?

If you wanted, you could show your code and people could perhaps give pointers on what to separate into functions to reduce the code size (that is, if you haven't already).
  #3  
Old 06-Jun-2009, 09:45
StoryGame464 StoryGame464 is offline
New Member
 
Join Date: Jun 2009
Posts: 2
StoryGame464 is on a distinguished road

Re: C++ If/Else Statement


Ye I do use functions, and it is more then just the yes no answers. It is a sort of simple game I am working on, but I just couldn't figure out how to end the program if they answered no or quit for the starting the game. Thank you very much for the help.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I need help on My SQL Database Creation Statement asdfg MySQL / PHP Forum 6 08-Mar-2008 05:13
Is "else if" a separate kind of statement in Java? anders Java Forum 1 20-Apr-2007 16:09
Fortran problem... Justin Fox Miscellaneous Programming Forum 6 24-Oct-2006 16:30
Need a bit of help with switch() Caibre C Programming Language 5 12-Apr-2005 18:02
C if/else statement problem JYGUTI C Programming Language 1 28-Mar-2005 06:58

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:26.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.