C++ 返回到“开始”;cin";如果用户到达其他位置(C+;+;)

C++ 返回到“开始”;cin";如果用户到达其他位置(C+;+;),c++,C++,如果用户没有键入正确的选项,它会让用户重新键入一个新的输入,直到它们匹配其中一个if语句 我相信这可能是一件很简单的事情,我只是还没有找到一种在线的方法。。如果您有一些建议,甚至只是链接,我们将不胜感激。。谢谢大家! 代码如下: cout << "Choose a base from the following options" << endl; cout << endl; cout << baseChoice1 << endl; co

如果用户没有键入正确的选项,它会让用户重新键入一个新的输入,直到它们匹配其中一个if语句

我相信这可能是一件很简单的事情,我只是还没有找到一种在线的方法。。如果您有一些建议,甚至只是链接,我们将不胜感激。。谢谢大家!

代码如下:

cout << "Choose a base from the following options" << endl;
cout << endl;
cout << baseChoice1 << endl;
cout << baseChoice2 << endl;
cout << baseChoice3 << endl;
cout << baseChoice4 << endl;
cout << endl;
cout << "Choice: ";

getline(cin,baseChoice);

transform(baseChoice.begin(), baseChoice.end(), baseChoice.begin(), ::toupper);    
transform(baseChoice1.begin(), baseChoice1.end(), baseChoice1.begin(), ::toupper);
transform(baseChoice2.begin(), baseChoice2.end(), baseChoice2.begin(), ::toupper);
transform(baseChoice3.begin(), baseChoice3.end(), baseChoice3.begin(), ::toupper);
transform(baseChoice4.begin(), baseChoice4.end(), baseChoice4.begin(), ::toupper);

cout << endl;
cout << endl;

        while ((baseChoice != baseChoice1) || (baseChoice != baseChoice2) || (baseChoice != baseChoice3) || (baseChoice != baseChoice4)) 
        {
        cout << "This is not a base option, try again!!" << endl;
        cout << "Choice: ";
        getline(cin,baseChoice);
        }


        if (baseChoice == baseChoice1) {
                baseChoice == baseChoice1;
                cout << "Base: " << baseChoice << endl;
        }
        else if (baseChoice == baseChoice2) {
                baseChoice == baseChoice2;
                cout << "Base: " << baseChoice << endl;
        }
        else if (baseChoice == baseChoice3) {
                baseChoice == baseChoice3;
                cout << "Base: " << baseChoice << endl;
        }
        else if (baseChoice == baseChoice4) {
                baseChoice == baseChoice4;
                cout << "Base: " << baseChoice << endl;
        }

cout规则是:应该发生一次的事情必须在循环之前,每次应该发生的事情必须在循环内部

因此,您应该在循环之前将
baseChoice1
转换为
baseChoice4
,并在循环内部将
baseChoice
转换为
。记住布尔代数的规则:not(A或B)是(nota)(notb)。顺便说一句,如果您想在决定继续循环后执行一些额外的操作,那么带中断的无限循环是一个方便的选择

代码可能会变成:

...
cout << baseChoice4 << endl;
cout << endl;

transform(baseChoice1.begin(), baseChoice1.end(), baseChoice1.begin(), ::toupper);
transform(baseChoice2.begin(), baseChoice2.end(), baseChoice2.begin(), ::toupper);
transform(baseChoice3.begin(), baseChoice3.end(), baseChoice3.begin(), ::toupper);
transform(baseChoice4.begin(), baseChoice4.end(), baseChoice4.begin(), ::toupper);

cout << endl;
cout << endl;

baseChoise = "";
for(;;)      
    {
    cout << "Choice: ";

    getline(cin,baseChoice);
    transform(baseChoice.begin(), baseChoice.end(), baseChoice.begin(), ::toupper);


    if ((baseChoice != baseChoice1) && (baseChoice != baseChoice2) && (baseChoice != baseChoice3) && (baseChoice != baseChoice4))
        {
        cout << "This is not a base option, try again!!" << endl;
        }
    else {
        break;
        }
    }
...
。。。

cout规则是:应该发生一次的事情必须在循环之前,每次应该发生的事情必须在循环内部

因此,您应该在循环之前将
baseChoice1
转换为
baseChoice4
,并在循环内部将
baseChoice
转换为
。记住布尔代数的规则:not(A或B)是(nota)(notb)。顺便说一句,如果您想在决定继续循环后执行一些额外的操作,那么带中断的无限循环是一个方便的选择

代码可能会变成:

...
cout << baseChoice4 << endl;
cout << endl;

transform(baseChoice1.begin(), baseChoice1.end(), baseChoice1.begin(), ::toupper);
transform(baseChoice2.begin(), baseChoice2.end(), baseChoice2.begin(), ::toupper);
transform(baseChoice3.begin(), baseChoice3.end(), baseChoice3.begin(), ::toupper);
transform(baseChoice4.begin(), baseChoice4.end(), baseChoice4.begin(), ::toupper);

cout << endl;
cout << endl;

baseChoise = "";
for(;;)      
    {
    cout << "Choice: ";

    getline(cin,baseChoice);
    transform(baseChoice.begin(), baseChoice.end(), baseChoice.begin(), ::toupper);


    if ((baseChoice != baseChoice1) && (baseChoice != baseChoice2) && (baseChoice != baseChoice3) && (baseChoice != baseChoice4))
        {
        cout << "This is not a base option, try again!!" << endl;
        }
    else {
        break;
        }
    }
...
。。。

你可以试试使用“while”循环吗?你能给我一个在这种情况下使用while循环的例子吗?原来的帖子已经用while语句更新了。不管我输入什么,它总是要求我输入一个新的输入(即使while术语是false),StackOverflow上有大量的例子展示了如何处理糟糕的用户输入。由于缺乏研究,我很想投票结束这个问题。但是,为了回答您的问题,您的循环不起作用,因为它需要使用
&&
而不是
|
。这并不能解决问题,但您不需要
while
语句中的大部分括号
while((baseChoice!=baseChoice1)| |(baseChoice!=baseChoice2))
的意思与
while(baseChoice!=baseChoice1 | | baseChoice1!=baseChoice2)
完全相同。你可以尝试使用“while”循环。你能给我一个在这种情况下使用while循环的例子吗?原始帖子已经用while语句更新了。不管我输入什么,它总是要求我输入一个新的输入(即使while术语是false),StackOverflow上有大量的例子展示了如何处理糟糕的用户输入。由于缺乏研究,我很想投票结束这个问题。但是,为了回答您的问题,您的循环不起作用,因为它需要使用
&&
而不是
|
。这并不能解决问题,但您不需要
while
语句中的大部分括号<代码>while((baseChoice!=baseChoice1)| |(baseChoice!=baseChoice2))
的意思与
while(baseChoice!=baseChoice1 | | baseChoice2)
的意思完全相同。