C++ C++;未检查条件语句

C++ C++;未检查条件语句,c++,while-loop,cin,C++,While Loop,Cin,我对这件事挠头有一段时间了,但我似乎不明白 用户将输入一个介于1和6之间的数字。我正在进行检查,以确保他们输入的值是有效的输入。如果不是,它将一直提示他们,直到他们输入有效的输入 我遇到的问题是,如果我输入任何整数值(这不是我想要的。用户输入的整数必须介于1和6之间,那么它应该退出while循环),while循环将终止 我希望有人能看到我看不到的东西。谢谢 #include <iostream> #include <cmath> using namespace std;

我对这件事挠头有一段时间了,但我似乎不明白

用户将输入一个介于1和6之间的数字。我正在进行检查,以确保他们输入的值是有效的输入。如果不是,它将一直提示他们,直到他们输入有效的输入

我遇到的问题是,如果我输入任何整数值(这不是我想要的。用户输入的整数必须介于1和6之间,那么它应该退出while循环),while循环将终止

我希望有人能看到我看不到的东西。谢谢

#include <iostream>
#include <cmath>

using namespace std;

int ReadDouble(int option) {
    while (cin.fail() != 0 && !(option > 0) && !(option <=6)) {
        cin.clear();
        cin.ignore(255, '\n');
        cerr << "Cannot read input \n";
        cout << "Choose an option between 1 and 6: " << endl;
        cin >> option;
    }
    cout << "This worked: " << endl;
    return 0;
}


int main()
{
    int prompt = NULL;
    cout << "1. Cube" << endl;
    cout << "2. Sphere" << endl;
    cout << "3. Prism" << endl;
    cout << "4. Cylinder" << endl;
    cout << "5. Cone" << endl;
    cout << "6. Quit" << endl;
    cout << "Choose an option by typing in the corresponding number: ";
    cin >> prompt;

    ReadDouble(prompt);
}
#包括
#包括
使用名称空间std;
int ReadDouble(int选项){

虽然(cin.fail()!=0&&!(option>0)&!(option这里提到的您的问题是:您需要:
!(option>0)&&!(option>prompt)| | prompt<0 | | prompt>6)不能解决您的问题,但我要指出这一点。
!(option>0)&!(选项A数字不能同时为
6
,您在
中的条件而
永远不可能为真。您可能想在那里使用or(
|
)。@Yksisarvinen谢谢您我是哑巴。
while(!(cin >> prompt) || prompt < 0 || prompt > 6) cout << "Cannot read input.\nChoose an option between 1 and 6: ";