简体   繁体   English

C ++从“ else”语句继续

[英]C++ Continuing from 'else' statement

I'm writing a little c++ console program to improve my programming and to begin a portfolio. 我正在编写一个小的c ++控制台程序,以改进我的编程并开始一个投资组合。 My question is: 我的问题是:

How do I continue my story after the else statement? else语句之后如何继续我的故事?

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! 提前致谢!

You wrap the qustioning code in a while and keep asking until the "correct" answer is given: 您将打包查询代码一段while然后不断询问,直到给出“正确”答案为止:

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);

You can use goto statement example: 您可以使用goto语句示例:

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 这与使用循环的方法相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM