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

3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by chunalt787
0
int main()
{
   char answer;
    cout << "Do you feel sexy today?";
    cin >> answer;
    if( answer != 'Y')
       return 0; // exit program
    // now do other things

}
0

Many programmers don't like putting return statements in the middle of functions. So here's another way to do it

int main()
{
   char answer;
    cout << "Do you feel sexy today?";
    cin >> answer;
    if( answer == 'Y')
    {
                // now do other things

    }
    return 0; // end of function
}
0

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:

int main() {
        while(buttonPushed() == false) { 
        //perform your loop
        }
return 0:
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.