简体   繁体   English

程序循环和if语句的问题

[英]issue with program loops and if statements

I'm writing a program that tells a user when they enter a negative or positive and even or odd number then the program ask a question, " Would you like to enter another number? y(es) or n(o) I need to account for the user entering in something else besides 'y' and 'n' and I need to account for if the user does not enter an integer. Last if the user selects yes, the program will need to go through the loop process of determining if they enter an integer and if its (positive or negative and odd or even) 我正在编写一个程序,告诉用户何时输入负数或正数以及偶数或奇数,然后该程序问一个问题:“您要输入另一个数字吗?y(es)或n(o)我需要输入用户输入除“ y”和“ n”之外的其他内容,我需要考虑用户是否未输入整数。最后,如果用户选择“是”,则程序将需要完成确定以下内容的循环过程如果输入整数,并且输入整数(正数或负数以及奇数或偶数)

int value;
char choice;

cout << "Please enter a number" << endl;
cin >> value;
while (!(cin >> value)) {
    cin.clear();
    cin.ignore(999, '\n');
    cout << "Invalid data type! Please enter 'value' again" << endl;
}
if (value > 0 && value % 2 == 0) {

    cout << value << " is even" << endl;
    cout << value << " is positive" << endl;
}

else if (value < 0 && value % 2 != 0) {
    cout << value << " is odd" << endl;
    cout << value << " is negative" << endl;
}

else if (value > 0 && value % 2 != 0) {
    cout << value << " is odd" << endl;
    cout << value << " is postive" << endl;
}

else if (value < 0 && value % 2 == 0) {
    cout << value << " is even" << endl;
    cout << value << " is negative" << endl;
}
cout << "Would you like to try another number? Say y(es) or n(o)" << endl;
cin >> choice;

while (choice != 'y' &&choice != 'n') {
    cin.clear();
    cin.ignore(999, '\n');
    cout << "Invalid response! Please enter 'choice' again" << endl;
}

do {
    if (choice == 'y') {
        cout << "Please enter a number" << endl;
        cin >> value;
        if (!(cin >> value)) {
            cin.clear();
            cin.ignore(999, '\n');
            cout << "Invalid data type! Please enter 'value' again" << endl;

        if (value > 0 && value % 2 == 0) {

            cout << value << " is even" << endl;
            cout << value << " is positive" << endl;
        }

        else if (value < 0 && value % 2 != 0) {
            cout << value << " is odd" << endl;
            cout << value << " is negative" << endl;
        }

        else if (value > 0 && value % 2 != 0) {
            cout << value << " is odd" << endl;
            cout << value << " is postive" << endl;
        }

        else if (value < 0 && value % 2 == 0) {
            cout << value << " is even" << endl;
            cout << value << " is negative" << endl;
        }
        cout << "Would you like to try another number? Say y(es) or n(o)" << endl;
        cin >> choice;
        }
    }
} while (choice == 'n');
    cout << "Thank you for using my program. Goodbye!" << endl;
return 0;

} }

It would take nested do-while loops to check all your conditions. 这将需要嵌套的do-while循环来检查所有条件。 Here I am using cin.fail() . 在这里,我正在使用cin.fail() cin.fail() detects whether the value entered fits the value defined in the variable. cin.fail()检测输入的值是否适合变量中定义的值。

int value;
char choice;
        do{     
            cout << "Please enter a number" << endl;
            cin >> value;
            if(cin.fail()) // check if input is int
            {
                cout<<"Not an int";
                choice = 'y';
            }
            else
            {
                if (value > 0 && value % 2 == 0) 
                {

                    cout << value << " is even" << endl;
                    cout << value << " is positive" << endl;
                }

                else if (value < 0 && value % 2 != 0) 
                {
                    cout << value << " is odd" << endl;
                    cout << value << " is negative" << endl;
                }

                else if (value > 0 && value % 2 != 0) 
                {
                    cout << value << " is odd" << endl;
                    cout << value << " is postive" << endl;
                }

                else if (value < 0 && value % 2 == 0) 
                {
                    cout << value << " is even" << endl;
                    cout << value << " is negative" << endl;
                }
                do{
                    cout << "Would you like to try another number? Say y(es) or n(o)" << endl;
                    cin >> choice;
                }while(choice != 'y' || choice != 'n');
            }
        }while (choice == 'n');

Also you should read this: Checking input value is an integer 您还应该阅读以下内容: 检查输入值是整数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM