如何在 C++ 中继续该函数而不退出 after else 语句
how do I continue on the function without exiting after else statement in c++
您好,所以我的问题是如何在 else 语句之后继续使用新输入?在我的程序中,我写了一个 else 语句,如果输入既不是 1 也不是 2,用户必须输入一个新值才能得到他/她想要的结果。但是在我的 else 声明之后,我的程序关闭了。我不知道如何解决这个问题。我对 c++ 很陌生,所以请对自己保持严厉的评论......这是我的代码:
// This program will allow the user to input from the keyboard
// whether the last word to the following proverb should be party or country:
// "Now is the time for all good men to come to the aid of their _______"
// Inputting a 1 will use the word party. A 2 will use the word country.
#include <iostream>
#include <string>
using namespace std;
void writeProverb(int);
int main ()
{
int wordCode;
cout << "Given the phrase:" << endl;
cout << "Now is the time for all good men to come to the aid of their ___" << endl;
cout << "Input a 1 if you want the sentence to be finished with party" << endl;
cout << "Input a 2 if you want the sentence to be finished with country." << endl;
cout << "Please input your choice now" << endl;
cin >> wordCode;
cout << endl;
writeProverb(wordCode);
return 0;
}
void writeProverb (int number)
{
if (number == 1)
cout << "Now is the time for all good men to come to the aid of their party." << endl;
else if (number == 2)
cout << "Now is the time for all good men to come to the aid of their country." << endl;
else
{
cout << "I'm sorry but that is an incorrect choice: Please input a 1 or 2" << endl;
}
}
所以基本上,在 else 语句之后,我希望我的程序等待用户输入 1 或 2,而不仅仅是关闭。
do {
cout << "Please input your choice now" << endl;
cin >> wordCode;
cout << endl;
writeProverb(wordCode);
}while (wordCode != 1 && wordCode != 2);
如果用户输入 1 或 2,则此代码退出。否则。
你想要有一个do-while
结构,直到你得到一个合法的价值,正如Sakthi Kumar已经指出的那样。
但是,您不想将法律价值的知识转移到主要内容上。因此,如果值合法,请让 writeProverb
方法返回。这使事情保持在适当的抽象级别。您还应该考虑使用对象更新"菜单"打印,从而将所有内容绑定在一起。
// ... in main()
do {
cout << "Please input your choice now" << endl;
cin >> wordCode;
cout << endl;
writeProverb(wordCode);
} while( !writeProverbIfLegalNumber(wordCode) );
}
bool writeProverbIfLegalNumber (int number) {
if (number == 1) {
cout << "Now is the time for all good men to come to the aid of their party." << endl;
return true;
}
else if (number == 2) {
cout << "Now is the time for all good men to come to the aid of their country." << endl;
return true;
}
else
{
cout << "I'm sorry but that is an incorrect choice: Please input a 1 or 2" << endl;
}
return false;
}
这是基本逻辑,您应该阅读有关循环以及如何使用它们的信息。当 wordCode 不在 1 和 2 之间(包括 1 和 2)时,此while
循环将继续;换句话说,它会一直持续到你得到 1 或 2。
int main ()
{
int wordCode;
cout << "Given the phrase:" << endl;
cout << "Now is the time for all good men to come to the aid of their ___" << endl;
cout << "Input a 1 if you want the sentence to be finished with party" << endl;
cout << "Input a 2 if you want the sentence to be finished with country." << endl;
do
{
cout << "Please input your choice now 1 or 2: " << endl;
cin >> wordCode;
} while(!(1 <= wordCode && wordCode <=2));
cout <<endl;
writeProverb(wordCode);
return 0;
}
在这种情况下
,您可以将判断语句放在"main"函数中。您可以使用"while"语句来控制逻辑。
boolean flag = true;
while(flag){
cout<<"input";
cin>>wordCode;
if(wordCode==1 || wordCode==2){
flag=false;
writeProverb(wordCode);
}
}
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 如何让LLDB在成功时退出,在失败时等待
- C++控制台应用程序阻止退出
- 程序在执行程序的其余部分之前退出
- 构造函数在退出函数时无法初始化一个参数
- 为什么异常不退出程序?
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- 退出简单while循环时出现问题
- 使用vscode调试时,GDB意外退出
- pclose() 不会给我进程退出代码
- 为什么系统函数总是在C++中返回已转移的退出状态?
- C++从另一个函数退出函数
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- C++逗号分隔的输入数组代码过早退出
- Netbeans 10:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 为什么 C++ 中的以下结构声明会导致退出 127?
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 如何在 C++ 中继续该函数而不退出 after else 语句