0

I'm writing a little c++ console program to improve my programming and to begin a portfolio. My question is:

How do I continue my story after the else statement?

Part of the code I have is:

else {
    cout << "That's not an answer mate!" << endl;
}

How do I make the program NOT exit, but ask for a new input from the user?

Thanks in advance!

3
  • 4
    Hint: use while Commented Dec 8, 2016 at 9:37
  • 2
    Start to use structured programming. You are thinking in terms of "go to X, then go to Y, then go to Z". You need loops. Every introductory book on programming covers loops. Commented Dec 8, 2016 at 9:41
  • You my friend, are a smart person. Thanks :) Commented Dec 8, 2016 at 9:45

2 Answers 2

2

You wrap the qustioning code in a while and keep asking until the "correct" answer is given:

bool error;
do {
  error = false;
  //some code here..
  //if statement to check..
  else
  {
    cout << "That's not an answer mate!" << endl;
    error = true;
  }
}while(error);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use goto statement example:

Condition1: cout<<"Enter age: ";
cin>>age;

if (age>18){
cout<<"Welcome"<<endl;
//same code...
}
else{
goto Condition1;
}

this is the same method as using loops

3 Comments

Advising to use goto to a beginner. You must be joking
I have to agree with @MuhammetAliAsan on this one - goto is a terrible practice.
Noted with thanks... @Muhammet Ali Asan , cc @ EJoshuaS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.