在没有程序的情况下向用户询问同样的事情终止了

时间:2017-04-06 18:02:16

标签: c++

我的程序要求用户回答2个随机数的总和,如果答案是正确的输出是“好”,当它不正确时“再试一次”。但是我希望程序一次又一次地问问我的程序在用户回答时终止,你能帮助我吗?

我希望输出像

<QuerySet [<Item: Item object>, <Item: Item object>, <Item: Itemobject>]>

我的代码:

How much is 19 plus 7 (-1 to End)?: 25
No - Please try again: 26
Very good!
How much is 9 plus 12 (-1 to End)?: 21
Very good!
How much is 71 plus 15 (-1 to End)?: 1
No - Please try again: 86
Very good!
How much is 17 plus 33 (-1 to End)?: -1

2 个答案:

答案 0 :(得分:1)

int main()
{
    int number1, number2, answer;
    int x = 0;
    srand (time(NULL));
    do
    {
        number1 = rand() % 100 + 1;
        number2 = rand() % 100 + 1;

        x = number1 + number2;

        cout << "What is the " << number1 << " plus " << number2 << " ?"  << endl;

        cin >> answer;

        if (answer == -1)
            continue;
        if (answer != x)
            cout << "No,Please try again.";

        else
            cout << "Very good!";

        cin.clear();
        cin.ignore();
        cin.get();
    } while (answer != -1);

    return 0;
}

答案 1 :(得分:0)

在if else条件的地方使用

    while(answer!=x)
    {    cout<<"no! please try again";
         cin>>answer;
    }
   cout<<"good";