Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;求解猜谜游戏的程序_C++_Random - Fatal编程技术网

C++ c++;求解猜谜游戏的程序

C++ c++;求解猜谜游戏的程序,c++,random,C++,Random,我正在读一本名为《跳进c++》的书,书中希望我编写一个程序,找到一个猜谜游戏的解决方案,随机选择一个介于1和100之间的数字,让用户猜出数字是什么,并告诉他们他们的猜测是高、低还是刚刚正确。 基本上,它想让我预测下一个随机数,但由于我是新手,我无法找到一种方法来预测 这是猜谜游戏的代码: #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int x,y;

我正在读一本名为《跳进c++》的书,书中希望我编写一个程序,找到一个猜谜游戏的解决方案,随机选择一个介于1和100之间的数字,让用户猜出数字是什么,并告诉他们他们的猜测是高、低还是刚刚正确。 基本上,它想让我预测下一个随机数,但由于我是新手,我无法找到一种方法来预测

这是猜谜游戏的代码:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int x,y;
int rand_range(int low, int high)
{
  return rand() % (high - low) + low;
}

int main()
{
  int seed = time(NULL);
  srand(seed);
  int y = rand_range(1, 100);
  cout << "Program has picked a no. between 1 to 100... You just make a 
           guess....\n";
  cin >> x;
  while(1)
    {
      if(x == y)
       {
          cout << "just right\n";return 0;
       }
      else if(x < y)
       { 
         cout << "low\n";return 0;
       }
      else
       {
         cout << "high\n";return 0;
       }
   }
}
#包括
#包括
#包括
使用名称空间std;
int x,y;
int rand_范围(int low,int high)
{
返回rand()%(高-低)+低;
}
int main()
{
int seed=时间(空);
srand(种子);
int y=随机范围(1100);
cout>x;
而(1)
{
如果(x==y)
{

cout我已经编辑了您的代码。您将代码放入无限循环中,但对于每种可能的情况,您都退出了循环,因此没有一个有效点。相反,您应该有一个循环,一旦用户猜到正确的输入就结束

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int x,y;
int rand_range(int low, int high)
{
    return rand() % (high - low) + low;
}

int main()
{
    int seed = time(NULL);
    srand(seed);
    int y = rand_range(1, 100);
    cout << "Program has picked a no. between 1 to 100... "
         << "You just make a guess....\n";
    cin >> x;
    while(x != y) {
        if(x < y)
           cout << "low\n";

        else
           cout << "high\n";

        cin >> x;
    }

    cout << "just right\n";
    return 0;

}
#包括
#包括
#包括
使用名称空间std;
int x,y;
int rand_范围(int low,int high)
{
返回rand()%(高-低)+低;
}
int main()
{
int seed=时间(空);
srand(种子);
int y=随机范围(1100);
cout x;
while(x!=y){
if(xcout您的代码有一些基本错误。首先,您需要在循环中获取输入。其次,您将在所有条件中返回。这就是为什么您的代码只尝试一次匹配目标。并且您必须在循环中获取输入,因为您要尝试猜测数字。为此,您每次都必须获取输入。我已编辑您略带r代码。请检查以下代码-

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int x,y;
int rand_range(int low, int high)
{
  return rand() % (high - low) + low;
}

int main()
{
  int seed = time(NULL);
  srand(seed);
  int y = rand_range(1, 100);
  cout << "Program has picked a no. between 1 to 100... You just make a guess....\n";

  while(1)
    {
    cin >> x; // Now taking input in the loop
      if(x == y)
       {
          cout << "just right\n";return 0;
       }
      else if(x < y)
       { 
         cout << "low\n"; //omitted the return line
       }
      else
       {
         cout << "high\n"; // omitted the return line
       }
   }
}
#包括
#包括
#包括
使用名称空间std;
int x,y;
int rand_范围(int low,int high)
{
返回rand()%(高-低)+低;
}
int main()
{
int seed=时间(空);
srand(种子);
int y=随机范围(1100);
cout>x;//现在在循环中获取输入
如果(x==y)
{

请对输入和输出进行示例您需要解释什么不起作用(您想要什么,得到什么)。这是如何工作的?您只接受了一次输入。如果数字与y不匹配,您的程序将无限期地连续打印
high
low
。抱歉,我错过了。修复了while循环,因此它会不断从命令行接收参数。