C++ Random number guessing game

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  #include <iostream>
#include <iomanip>
using namespace std;
char chr;

int main()
{
	int number;
	int guess;
	int tries;
    char answer;

    srand(number>0);
	do
	{
    number=rand()%100+1;;
	cout<<"Enter a number between 1 and 100"<<endl;
	cin>>guess;
	if (number<guess)
		cout<<"Too high try again"<<endl;
	tries=1;
	} while(number>guess);
		cout<<"Too low try again"<<endl;
		tries++;
	if(number==guess)
		cout<<"Congratualtions!! "<<endl;
	cout<<"You got the right number in "<<tries<<" tries";
	do
	{
cout<<"Would you like to play again?  Enter Y/N";
		cin>>answer;
    if ('N')
		cout<<"Thanks for playing!";
	} while(answer='Y');
			

	
	cin>>chr;
		return 0;

	}
At the top, include another library: #include <ctime>
Change line 13 to srand(time(0)); What you have now is not a normal way to seed the random number generator (but if you created a release build this code would probably run anyway).
I have changed my srand to srand(time(0)); and added the #include<ctime> and it still isn't working.
Are you still getting the same error ?

What if you try with srand(time(NULL)) ?
Last edited on
It is not the same error now the error is 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data

I changed the srand to NULL it didn't change anything.
This is not an actual error, but a warning message. In other words, assuming the rest of your code is good, your program should run properly.

If you want to get rid of the warning message, put this instead:

srand((unsigned int)time(NULL));

Last edited on
I'm noticing a few things you need to fix with your code.
1. Line 16 has two ;;
2. Your if statements are either missing one or both { }

closed account (18hRX9L8)
Hello. Here is your fixed code with comments:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <cstdlib>
#include <ctime>

int main(void) {
	srand(time(NULL)); // To not have the same numbers over and over again.
	
	while(true) { // Main loop.
		// Initialize and allocate.
		int number = rand() % 99 + 2; // System number is stored in here.
		int guess; // User guess is stored in here.
		int tries = 0; // Number of tries is stored here.
		char answer; // User answer to question is stored here.
		
		//std::cout << number << "\n"; // Was used for debug...
		
		while(true) { // Get user number loop.
			// Get number.
			std::cout << "Enter a number between 1 and 100 (" << 20 - tries << " tries left): ";
			std::cin >> guess;
			std::cin.ignore();
			
			// Check is tries are taken up.
			if(tries >= 20) {
				break;
			}
			
			// Check number.
			if(guess > number) {
				std::cout << "Too high! Try again.\n";
			} else if(guess < number) {
				std::cout << "Too low! Try again.\n";
			} else {
				break;
			}
			
			// If not number, increment tries.
			tries++;
		}
		
		// Check for tries.
		if(tries >= 20) {
			std::cout << "You ran out of tries!\n\n";
		} else {
			// Or, user won.
			std::cout<<"Congratulations!! " << std::endl;
			std::cout<<"You got the right number in " << tries << " tries!\n";
		}
		
		while(true) { // Loop to ask user is he/she would like to play again.
			// Get user response.
			std::cout << "Would you like to play again (Y/N)? ";
			std::cin >> answer;
			std::cin.ignore();
			
			// Check if proper response.
			if(answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
				break;
			} else {
				std::cout << "Please enter \'Y\' or \'N\'...\n";
			}
		}
		
		// Check user's input and run again or exit;
		if(answer == 'n' || answer == 'N') {
			std::cout << "Thank you for playing!";
			break;
		} else {
			std::cout << "\n\n\n";
		}
	}
	
	// Safely exit.
	std::cout << "\n\nEnter anything to exit. . . ";
	std::cin.ignore();
	return 0;
}


Some things I needed to fix:
1. Line 2 is useless.
2. Line 3 is not a good coding practice.
3. Line 13: Number is undefined, thus resulting in undefined behaviour. Also you need to include cstdlib for srand.
4. Line 16: You keep changing the number every loop!
5. Line 21: You keep setting tries to 1 when you are supposed to increment it.
6. Lines 23 & 24: Move this inside the loop, you need to check for all possibilities.
7. Line 22: So if the guess is higher, we break? How about if it is lower?
8. Line 32: You are just checking 'N' with true, which will always return false. You need to check 'N' against the user response.
9. Line 34: This loop will keep going until the user presses 'N'. This is bad because if the user wants to play again, he/she cannot because this loop does not acknowledge that.
10. Line 38: The user has no idea what to do. Tell the user what to do!
Last edited on
@Annatar You can put 100 semi-colons in a row and it would make no difference also you do not need braces after if/for loops the braces are for enclosing multiple statements if you are only doing one statement it is fine to do it with no braces.
@giblit Thanks for clearing the semi-colon thing for me. Regarding the braces, I did mean for if/for loops with multiple statements, but it turns out I read the code wrong by assuming copy/paste had modified the alignment.

For example, I thought this
1
2
3
if (number<guess)
		cout<<"Too high try again"<<endl;
	tries=1;


actually meant this

1
2
3
4
5
if (number<guess)
{
	cout<<"Too high try again"<<endl;
	tries=1;
}



Sorry for the misunderstanding, didn't want to lead anyone in the wrong direction.
Topic archived. No new replies allowed.