C++ 随机数猜谜游戏

标签 c++

我必须编写一个程序来运行随机猜谜游戏。游戏是从 1 到 100 的数字,猜谜者可以尝试 20 次,最后应该询问他们是否愿意再玩一次。如果猜测者高或低,还必须有多种打印输出选项。我已经完成了部分程序,我知道我仍然需要为打印输出添加其他选项,但现在我的问题是,当我尝试运行我所拥有的时,它说成功但随后出现错误,指出变量“number”正在使用而没有被初始化。我不确定该怎么做才能将其初始化。 (我是 C++ 的新手) 我已经更新了我的程序,但现在遇到了不同的问题。我的程序运行但如果猜测低于它打印的数字太高再试一次太低再试一次但是当数字太高时它只是打印太高再试一次。我还注意到,当用户选择再次玩游戏时,尝试计数器不会随游戏一起重置。最后一件事我必须添加更多的消息,当他们赢了,输了,并且被要求玩另一场比赛,我必须使用随机数来选择它们。因此,关于最佳途径的任何建议都会很棒

#include <iostream>
#include <iomanip> 
#include <ctime>
using namespace std;
char chr;

int main()
{
srand(time(NULL));                                           //the function that generates random numbers
int number=rand()%100+1;                                     //the range of the random numbers
int guess;                                                   //The guess is stored here
int tries=0;                                                 //The number of tries stored here
   char answer;                                                 //The answer to the question is stored here
answer='y';                  

while(answer=='y'||answer=='Y') 
{
    while (tries<=20 && answer=='y'|| answer=='Y')
    {
    cout<<"Enter a number between 1 and 100 "<<endl;          //The user is asked for a guess
    cin>>guess;                                               //The guess is stored here
    tries++;                                                 //Adding a number for every try
    if(guess==0||guess>100)                                  //If statement that produces an error message if user enters a number out of the peramiters
    {
     cout<<"This is not an option try again"<<endl;          //Error message
    }

    if(tries<20)                                            
    cout<<"Tries left: "<<(20-tries)<<endl;                   //Output to let the user know how many guess they have left

    if(number<guess);                                         //if the guess is to high
    cout<<"Too high try again"<<endl;                         //This message prints if guess it to high

    if(number>guess)                                          //if the guess is to low
    cout<<"Too low try again"<<endl;                          //This message prints if the guess is to low

    if(number==guess)                                          //If the user guesses the number
    {
     cout<<"Congratualtions!! "<<endl;                          //Message printed out if the user guesses correctly
     cout<<"You got the right number in "<<tries<<" tries"<<endl;  //Lets the user know how many guess they used
     answer = 'n';
    }
    if(tries >= 20)                                               //If the user uses all their guesses
    {
    cout << "You've run out of tries!"<<endl;                      //The message that prints when a user is out of guesses
    answer='n';
    }
    if(answer=='n')
    {
     cout<<"Would you like to play again?  Enter Y/N"<<endl;       //asking if play would like to play again
     cin>>answer;                                                  //Store users answer
     if (answer=='N'|| answer=='n')                                //if the user says no
     cout<<"Thanks for playing!"<<endl;                            //This message prints out if they choose not to play again

    else
        number=rand()%100+1;                                        //This starts the game over if they choose to play again
    }

    }
    }

cin>>chr;
    return 0;

}

最佳答案

[编辑:添加强制转换以消除编译器警告。]

正如 Jim Rhodes 在评论中所说,问题出在线路上

srand(number>0);

srand()用于初始化随机数生成器,因此用 number>0 调用它没有意义,甚至用 number 调用它也没有意义。它需要一个“种子”值,每次运行程序时都应该不同。获取此类种子的常见方法是使用系统时间:

srand(static_cast<unsigned int>(time(NULL)));

您可能需要#include 另一个 header 才能访问time() .

关于C++ 随机数猜谜游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19648471/

相关文章:

c++ - 为什么我会收到 CL_MEM_OBJECT_ALLOCATION_FAILURE?

c++ - concurrency::fast_math::tanh() 在 parallel_for_each (C++ AMP) 处返回 NaN

c++ - 下面的字符串循环缓冲区实现有哪些可能的改进?

c++ - 将字符串写入指针 C++

c++ - 如何在两点之间强制执行约束

c++ - 如何解决字段 'classname'有不完整的类型错误

c++ - 读取或写入 boost::asio::ssl::stream::next_layer() 是否绕过 SSL 解密/加密?

c++ - 如何为自己的类(class)专门设计标准概念?

c++ - 一些缺少的头文件,如 import/cli.h 、 io/FileOutputStream.h 或 gpu/StreamManager.h

c++ - boost 收集范围的适配器