C++ Random number guessing game

后端 未结 5 1456
我寻月下人不归
我寻月下人不归 2021-01-28 06:50

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 wo

相关标签:
5条回答
  • 2021-01-28 07:09

    May I suggest using the gettimeofday function, as a way to provide a seed to your random function?

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/time.h>
    //clock_t times(struct tms *buffer);
    int
    main(int argc, char **argv)
    {
        int a;
        struct timeval _wall;
        gettimeofday( &_wall, NULL ); //(_wall.tv_sec,_wall.tv_usec);
        srand(_wall.tv_usec);
        a = rand() % 100 + 1;
        printf("%d\n",a);
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-28 07:12

    [EDIT: Added cast to get rid of compiler warning.]

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

    srand(number>0);
    

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

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

    0 讨论(0)
  • 2021-01-28 07:13

    The problem the compiler is warning you about is with these two lines:

    int number;
    //...
    srand(number>0);
    

    Here, you haven't given the variable number an initial value, so it's uninitialised -- you have absolutely no way of knowing what the value might be at this point. In fact, it's likely to change every time you run your programme. Next you're asking whether the mystery value is greater than zero -- it might be, it might not, but you just don't know. It's mystery behaviour, which is what the compiler is warning you about.

    Now of course, you're trying to initialise the random seed, so having this mystery behaviour might be what you're after! But unfortunately even that isn't going to work as it stands.

    In C++, the expression number>0 is boolean, that is, the result of the expression is either true or false. But the srand() function takes an unsigned int as it's argument, so the compiler has to convert the bool to an unsigned, and it does so by changing false to 0, and true to 1 (probably: I believe it's technically up to the implementation). So no matter what the initial value of number, your random seed is only going to have one of two possible values -- not very random at all!

    Better would be to use a random seed based on something (relatively) unpredictable. It's common for non-cryptographic needs to initialise a seed based on the current time. Something like:

    #include <ctime>
    
    std::time_t now = std::time(0);
    srand(static_cast<unsigned>(now));
    

    would be fine.

    0 讨论(0)
  • 2021-01-28 07:19

    Initialize your variables (always!!) before using them, otherwise you're calling for undefined behavior (or even coppiler errors) within any operations with these!

    int number = 0;
    int guess = 0;
    int tries = 0;
    char answer = '\0';
    

    NOTE (for downvoters, doubting commenters):
    Of course this answer doesn't tell, how to get the srand(number>0); statement right for the desired result (i.e. initializing number with a more appropriate value than 0), but it solves the compile time error asked for in first place, and is good advice for any other scenario, resulting in a similar compiler error (warning). Getting number initialized with an appropriate value to pass to srand() seed method to get the right results expected at runtime, is a completely different question and should be asked as such.

    0 讨论(0)
  • 2021-01-28 07:21

    One problem that I see is that you are picking a new random number for each user guess. This is wrong.

    To fix this you should put the

    number=rand()%100+1;
    

    line before the do while loop that loops asking for guesses.

    A second problem is the condition on that loop is not correct. You should loop until number == guess not number > guess and put the both couts that tell the user if the guess is high or low inside the loop and increment your tries inside the loop as well.

    Also you probably want to have an outer do while() loop for the question that asks you to play again instead of this loop being after the loop that waits till the user gets the right number.

    0 讨论(0)
提交回复
热议问题