如何在 C++ 中继续该函数而不退出 after else 语句

how do I continue on the function without exiting after else statement in c++

本文关键字:退出 after 语句 else 函数 C++ 继续      更新时间:2023-10-16

您好,所以我的问题是如何在 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);
    }
}