程序循环和if语句的问题

时间:2017-10-26 19:40:45

标签: c++ loops if-statement do-loops

我正在编写一个程序,告诉用户何时输入负数或正数,偶数或奇数 然后程序问一个问题,"你想输入另一个号码吗?是还是不是) 我需要考虑用户输入除了' 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;

}

1 个答案:

答案 0 :(得分:0)

需要嵌套的do-while循环来检查所有条件。 我在这里使用cin.fail()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');

另外,您应该阅读:Checking input value is an integer