C++ Random number guessing game

后端 未结 5 1465
我寻月下人不归
我寻月下人不归 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: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.

提交回复
热议问题