为什么这个C ++ While循环不起作用?

时间:2016-08-24 03:47:08

标签: c++ loops while-loop

我只是想让用户输入他们的名字/年龄并验证它是否正确。如果没有,那么在程序中止之前它们会进行4次尝试。但是我的while循环不循环,而是继续循环到下一个循环。我在while括号(op != 1) (!(op = 1))等中尝试了各种各样的东西。

int main() {
    system("Color 0A");
    string name;
    int age;
    int tries = 0;
    int op = 0;
    cout << "Hello User" << endl;
    Sleep(3000);
    while ((op != 1) && (tries < 4)) {
        name = entName(name);
        cout << "So your name is " << name << "?" << endl;
        cout << "Enter '1' for YES or '2' for NO. ";
        cin >> op;
        if (op == 1) {
            cout << "Perfect!";
        }           
        if (op == 2) {
            cout << "Please Try Again!";
            tries+ 1;
            }
        if (tries = 4) {
            //abort the program
        }   
    }
    int op2 = 0;
    int tries2 = 0;
    while ((op2 != 1) && (tries2 < 4)) {
        op2 = 3;
        age = entAge();
        cout << "So you are " << age << " years old?" << endl;
        while ((op2 != 1) && (op2 != 2)) {
            cout << "Enter '1' for YES or '2' for NO. ";
            cin >> op2;
            if (op2 == 1) {
                cout << "Perfect!\n";
            }
            if (op2 == 2) {
                cout << "Please Try Again!\n";
                tries2++;
            }
            if (tries2 = 4) {
                //abort the programhi
            }
        }
    }
    return 0;
}

我对C ++很陌生,所以如果它有一个简单的答案,我很抱歉。但无论如何,我已经调试了半个多小时,我在网上看了20多分钟。

3 个答案:

答案 0 :(得分:2)

if (tries = 4) {
            //abort the program
        }   

将此更改为

if (tries == 4) {
            //abort the program
        }   

并且

f (op == 2) {
            cout << "Please Try Again!";
            tries+= 1; // tries+ 1;
            }

您可以使用此tries+ 1;增加C ++中的值。使用tries+= 1;tries++;

答案 1 :(得分:2)

tries+ 1;应为tries += 1;tries++;

而且,

if (tries = 4) {
    //abort the program
}

应该是:

if (tries == 4) {
    //abort the program
}

答案 2 :(得分:0)

您的程序应如下所示:

int main() 
{
    system("Color 0A");
    string name;
    int age;
    int tries = 0;
    int op = 0;
    cout << "Hello User" << endl;
    Sleep(3000);
    while ((op != 1) && (tries < 4)) {
        name = entName(name);
        cout << "So your name is " << name << "?" << endl;
        cout << "Enter '1' for YES or '2' for NO. ";
        cin >> op;
        if (op == 1) {
            cout << "Perfect!";
        }           
        if (op == 2) {
            cout << "Please Try Again!";
            tries+= 1;
        }
        if (tries == 4) {
            //abort the program
        }   
    }
    int op2 = 0;
    int tries2 = 0;
    while ((op2 != 1) && (tries2 < 4)) {
        op2 = 3;
        age = entAge();
        cout << "So you are " << age << " years old?" << endl;
        while ((op2 != 1) && (op2 != 2)) {
            cout << "Enter '1' for YES or '2' for NO. ";
            cin >> op2;
            if (op2 == 1) {
                cout << "Perfect!\n";
            }
            if (op2 == 2) {
                cout << "Please Try Again!\n";
                tries2++;
            }
            if (tries2 == 4) {
                //abort the programhi
            }
        }
}

您忘记在多个地方使用=符号。 tries = 4应该是tries == 4,用于将变量tries与数字4进行比较。tries = 4将变量tries重新分配给四,并且您的while循环正在获得在第一次运行后终止。此外,tries + 1应为tries += 1tries++,以便将tries变量的值增加一。

相关问题