C++ Random number guessing game

后端 未结 5 1528
我寻月下人不归
我寻月下人不归 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: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(time(NULL)));
    

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

提交回复
热议问题