C ++使用用户的输入重复查询

时间:2014-05-08 18:47:09

标签: visual-c++ if-statement input while-loop

我想要做的是问一个简单的问题,使用1或2作为是或否作为输入。如果他们说1或2,那么它将继续使用代码。但如果他们说数字(7)或字符(' q'),那么问题将再次重复。我还没弄明白如何重复这个问题和输入。 这就是我到目前为止所做的:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int answer = 0;

do
{
cout << "Are you Bald? Yes(1) or No(2)?" << endl;
cin >> answer; cout << endl;

    if (answer == 1)
    {
        cout << "You Are Bald." << endl;
    }

    else  if (answer == 2)
    {
        cout << "You are not bald." << endl;
    }

    else { cout << "Please Input proper answer" << endl << endl; 
} while (answer < 1, answer > 2);
system("pause");
return 0;

1 个答案:

答案 0 :(得分:0)

试试这个:

    #include "stdafx.h"
    #include <iostream>
    using namespace std;

    int main()
    {
    int answer = 0;
    while(1)
    {
        cout << "Are you Bald? Yes(1) or No(2)?" << endl;
        cin >> answer; cout << endl;

        if (answer == 1)
        {
            cout << "You Are Bald." << endl;
            break;
        }

        else  if (answer == 2)
        {
            cout << "You are not bald." << endl;
            break;
        }

        else 
        { 
           cout << "Please Input proper answer" << endl << endl; 
        }

    }
    return 0;
}