C ++随机数猜测游戏 [英] C++ Random number guessing game

查看:123
本文介绍了C ++随机数猜测游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须写一个程序,运行一个随机猜测游戏。游戏是从1-100的数字,猜测者获得20次尝试,并在结束时应该问他们是否想再次播放。如果猜测是高或低,打印输出也必须有多个选项。我有一部分我的程序完成我知道我还需要添加其他选项打印输出,但现在我的问题是,当我试图运行我已经说它成功,但然后有一个错误,说,变量数字被使用而不被初始化。我不知道该怎么做得到它初始化显然。 (我是C ++的新手)
我更新了我的程序,现在有一个不同的问题。我的程序运行,但如果猜测低于它打印的数量太高再试一次,太低再试一次,但当数字太高,它只是打印太高再试一次。我也注意到,当用户选择再次玩时,尝试计数器不会与游戏重置。最后一件事,我必须添加更多的消息,当他们赢了,失去,被要求玩另一个游戏,我必须使用随机数来选择其中之一。

I have to write a program that will run a random guessing game. The game is to be numbers from 1-100, the guesser gets 20 tries and at the end is supposed to be asked if they would like to play again. There also has to be multiple options for print outs if the guesser is to high or low. I have part of my program done I know I still need to add the other options for print outs but right now my issue is that when I try to run what I have it says successful but then there is an error that says that the variable "number" is being used without be initialized. I am not sure what to do to get it initialized apparently. (I am new to C++) I have updated my program and am now having a different issue. My program runs but if the guess is lower than the number it prints Too high try again and Too lower try again but when the number is too high it just prints Too high try again. I also noticed that when the user chooses to play again the tries counter is not resetting with the game. One last thing I have to add more messages for when they win, lose and are asked to play another game and I have to use random number to choose among them. SO any advice on the best route to do that would be great

#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;

}


推荐答案

正如吉姆罗兹在评论中说的,问题是用

As Jim Rhodes said in a comment, the problem is with the line

srand(number>0);

srand() 用于初始化随机数生成器,因此使用 number> 0 ,或者甚至使用 number 。它需要一个种子值,这应该是不同的每次你运行程序。获取这样的种子的常见方式是使用系统时间:

srand() is used for initialising the random number generator, so it doesn't make sense to call it with number>0, or even with number at all. It wants a "seed" value, that should be different every time you run the program. A common way to get such a seed is by using the system time:

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

您可能需要 #include 可访问 time()

You might need to #include another header to get access to time().

这篇关于C ++随机数猜测游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆