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

Recommended Answers

All 4 Replies

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

}

Brilliant cheers :) so simple so noob!

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
}

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:
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.