C ++:提示需要多次输入数据才能继续

时间:2015-12-21 16:03:58

标签: c++ random cin

我编写的以下程序提示用户输入数字1 - 100.该程序具有来自用户必须猜测的随机数生成器的预选编号。如果用户的猜测太高或太低,程序将通知用户,直到用户的猜测是正确的。我的程序效果很好,但是当提示输入数字时,我必须在某些情况下输入数字四次或更多次以便程序提示我。有人可以告诉我还是帮我找错?

#include <iostream>
#include <ctime>//A user stated that using this piece of code would make a true randomization process.
#include <cstdlib>
using namespace std;

int main()//When inputing a number, sometimes the user has to input it more than once.
{
    int x;
    int ranNum;
    srand( time(0));

    ranNum = rand() % 100 + 1;

    cout << "Please input your guess for a random number between 1 - 100.\n";
    cin >> x;

    while (x > ranNum || x < ranNum)
    {
        {
            if (x > ranNum)
                cout << "Your input was greater than the system's generated random number. Please try again.\n";
            cin >> x;
            if (x > 100 || x < 1)
                cout << "Input invalid. Please input a number between 1 - 100.\n";
            cin >> x;
        }
        {
            if (x < ranNum)
                cout << "Your input was less than the system's generated random number. Please try again.\n";
            cin >> x;
            if (x > 100 || x < 1)
                cout << "Input invalid. Please input a number between 1 - 100.\n";
            cin >> x;
        }
        {
            if (x == ranNum)
                cout << "You guessed right!\n";
        }
    }

    return 0;
}

3 个答案:

答案 0 :(得分:1)

您的括号位置错误。具有多行的if语句应采用

的形式
if (condition)
{
    code1
    code2
    code3
    ...
}

你拥有的是

{
    if condition
        code1
        code2
        code3
        ...
}

所以code1只会在条件为真时运行,但code2code3,其余的将无论如何运行。缩进在C ++中不算什么。我们可以(请永远不要这样做):

            if (condition)
    {
code1
code2
        code3
}

所有三行都将在if语句中运行。

您的代码更正为括号位于正确的位置

while (x != ranNum)
{
    if (x > ranNum)
    {
        cout << "Your input was greater than the system's generated random number. Please try again.\n";
        cin >> x;
        if (x > 100 || x < 1)
        {
            cout << "Input invalid. Please input a number between 1 - 100.\n";
            cin >> x;
        }
    }
    if (x < ranNum)
    {
        cout << "Your input was less than the system's generated random number. Please try again.\n";
        cin >> x;
        if (x > 100 || x < 1)
        {
            cout << "Input invalid. Please input a number between 1 - 100.\n";
            cin >> x;
        }
    }
    if (x == ranNum)
        cout << "You guessed right!\n";
}

我还将while (x > ranNum || x < ranNum)更改为while (x != ranNum),因为我们只想在x不等于ranNum时运行循环

答案 1 :(得分:0)

您没有正确使用if语句:

{
    if (x > ranNum)
        cout << "Your input was greater than the system's generated random number. Please try again.\n";
    cin >> x;
    if (x > 100 || x < 1)
        cout << "Input invalid. Please input a number between 1 - 100.\n";
    cin >> x;
}

上述两个if条件都不会影响任何内容,只会打印输出消息,并且周围的花括号也无用。当条件代码由多个单个语句组成时,您需要将其包装成花括号:

{
    if (x > ranNum)
    {
        cout << "Your input was greater than the system's generated random number. Please try again.\n";
        cin >> x;
    }
    if (x > 100 || x < 1)
    {
        cout << "Input invalid. Please input a number between 1 - 100.\n";
        cin >> x;
    }
}

此外,重复读取输入和执行范围检查的代码也没有意义。以下内容对我来说更直接:

int main()
{
    int ranNum;
    srand( time(0));

    ranNum = rand() % 100 + 1;

    cout << "Please input your guess for a random number between 1 - 100.\n";
    cin >> x;

    int x;
    for (;;) // Endless loop
    {                  
        cin >> x;

        if (x > 100 || x < 1)
        {
            cout << "Input invalid. Please input a number between 1 - 100.\n";
            continue; // Jump to next iteration of the loop
        }                                             

        if (x == ranNum)                              
        {
            cout << "You guessed right!\n";
            break; // Leave the loop
        }

        if (x > ranNum)
            cout << "Your input was greater than the system's generated random number. Please try again.\n";
        else
            cout << "Your input was less than the system's generated random number. Please try again.\n";
    }

    return 0;
}

答案 2 :(得分:0)

您的代码可能如下所示:

#include <iostream>
#include <ctime>//A user stated that using this piece of code would make a true randomization process.
#include <cstdlib>
using namespace std;

int main()//When inputing a number, sometimes the user has to input it more than once.
{
    int x;
int ranNum;
srand(time(0));

ranNum = rand() % 100 + 1;

cout << "Please input your guess for a random number between 1 - 100.\n";


while (x > ranNum || x < ranNum)
{
    cin >> x;
    if (x > ranNum && x<=100 & x>=1) cout << "Your input was greater than the system's generated random number. Please try again.\n";
    else if (x < ranNum && x<=100 & x>=1) cout << "Your input was smaller than the system's generated random number. Please try again.\n";
    else if (x>=100 || x<=1) cout << "Input invalid. Please input a number between 1 - 100.\n";

}
cout << "You guessed right!\n";

    return 0;
}

将来:

当你使用while循环时,它包含if语句。使用是正确的。另外,尝试嵌套if语句,但改为使用逻辑运算符。

相关问题