尝试使它猜测所选的随机数时,程序进入无限循环?

时间:2018-10-25 01:15:58

标签: c++

这是课堂作业。我试图让它生成一个随机数,然后猜测它生成了什么。我仍然是学习c ++的新手。我知道它必须在迭代时更新变量,但我不知道如何在循环中更新变量。我要去哪里了?
使用命名空间std;

int main()
{
    srand(static_cast<unsigned int>(time(0))); //seed random number generator

    int actual = rand() % 64 + 1; //random number betweeb 1 and 64
    int tries = 0;
    int searchGridHighNumber = 64;
    int searchGridLowNumber = 1;
    int targetLocationPrediction = ((searchGridHighNumber - searchGridLowNumber) / 2) + searchGridLowNumber;
    cout << "Generate Random enemy location on 8x8 grid....\n";
    cout << "The enemy is located at location #" << actual << " on 8x8 grid 
            with 1
            - 64 sectors.\n ";
              cout
         << "Skynet HK -Aerial Initializing software.....";
    do {
        ++tries;

        if (targetLocationPrediction > actual) {
            cout << "Skynet HK-Aerial rader sending out ping #" << tries
                 << endl;
            cout << "the target location prediction of" << targetLocationPrediction << "was higher than the actual enemy location of"
                 << actual << endl;
            "\n";
            int targetLocationPrediction = searchGridHighNumber;
        }

        else if (targetLocationPrediction < actual) {

            cout << "Skynet HK-Aerial rader sending out ping #" << tries << endl;
            cout << "the target location prediction of" << targetLocationPrediction << "was lower than the actual enemy location of" << actual << endl;
            int targetLocationPrediction = searchGridLowNumber;
        }
        else {

            cout << "enemy was hiding at location #" << actual << endl;
            cout << "Target was found at location #" << targetLocationPrediction << endl;
            cout << "Skynet HK-Aerial Software took" << tries << "predictions to find the enemy location on a grid size of 8x8(64)";
            cout << "Press any key to continue...";
        }
    } while (targetLocationPrediction != actual);
    return 0;
}

2 个答案:

答案 0 :(得分:1)

删除所有不必要的小说(在添加精美的输出之前解决问题是个好主意)

int guess = ((high - low) / 2) + low;
do {
    if (guess > actual) {
        int guess = high;
    }

    else if (guess < actual) {
        int guess = low;
    }
} while (guess != actual);

这样可以更清楚地在猜测错误时声明新变量。
这意味着实际的猜测永远不会被修改,循环永远不会终止,因此请删除int

int guess = ((high - low) / 2) + low;
do {
    if (guess > actual) {
        guess = high;
    }

    else if (guess < actual) {
        guess = low;
    }
} while (guess != actual);

下一个问题是,您永远不会缩短猜测间隔–如果猜测太低,则下一个猜测将为1;如果太高,下一个将是64。
另一个非终止循环,除非目标是32。

您需要

  1. 缩短间隔;新的顶部是guess - 1或新的底部是guess + 1
  2. 在新间隔的中间进行新的猜测。

在循环开始时更容易计算猜测,因为您只需要在一个地方进行计算即可:

do {
    int guess = ((high - low) / 2) + low;
    if (guess > actual) {
        high = guess - 1;
    }
    else if (guess < actual) {
        low = guess + 1;
    }
} while (guess != actual);

答案 1 :(得分:0)

在我看来,似乎您是将变量重新分配给两个变量之一。循环结束的唯一方法是,如果实际变量为1或64。

您想要做的是创建一个随机数并猜对吗?那好吧在此程序中,仅当用户正确输入数字时,我才更新变量。我没有设置任何if语句/循环来检查答案是否超过最大值和最小值。

#include <iostream>
#include <time>

using namespace std;
int main()
{
int actualGuess;
bool tries = false;

     srand(static_cast<unsigned int>(time(0))); //seed random number generator
     int actual = rand() % 64 + 1;

//Create the random variable value, and create another random value and guess it.
//use the sleep function because if you call rand() during the same second, it will give you the same value.

//Or you can simply use a user entered variable to compare to the 'actual' value

cout << "Skynet HK -Aerial Initializing software....." << endl;
     do{
        cout << "What is the correct number?" << endl;
        cin> actualGuess;

        if(actual == actualGuess){ 
        cout << "Correct!" << endl;
        tries = true;;
        }

        if(actual != actualGuess){
        cout << "Incorrect!" << endl;}
        }while(tries = false);
 return 0;
 }

这是您的代码的一些伪代码。下面的代码不可编译,旨在了解您的程序流程。

 H_Num = 64;
 L_Num = 1;

actual = 23 //say actual rands to 23
TargLoc = ((H_Num - L_Num) / 2 + L_Num)

TargLoc = (64 - 1 )/ 2 + 1)
TargLoc = 63/2 + 1
TargLoc = 32;  //This is what would happen, given the variables you initialized.


tries = 1

if TargLoc > actual
cout - 1
cout - 32
TargLoc = 64

else if
cout 1
cout 32 - 23
TargLoc = 1 

else
cout - 23
cout - 64
cout - 1
cout