Unitialized local variable

Hi guys I'm new to C++ and I'm taking a class for this but I'm sort of stuck on this assignment. I keep getting errors for uninitialized local for my computer_guess and warning for "Warning 1 warning C4244: 'initializing' : conversion from 'time_t' to 'unsigned int', possible loss of data". I don't know how to fix this and was hoping one of you guys could help me out.


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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <ctime>	//For the time function	
#include <cstdlib>	//For rand and srand
using namespace std;

int num;
char B;
void ComputervsPlayer();
void PlayervsComputer();

int main()
{
	//Get the system time
	unsigned seed = time(0);

	//Seed the random number generator
	srand(seed);
	//Computer Guess Placement
	num = rand() % 100 + 1;

	char a;
	B = 'Y';
	while (B == 'Y' || B == 'y')	//Instead of do/while can use while statement for Yes/No answer
	{
		cout << "Do you want to play a game? ";
		cin >> a;

		if (a == 'Y' || a == 'y')
			ComputervsPlayer();	//Takes it to computer vs player function
		else if (a == 'N' || a == 'n')
		{
			PlayervsComputer();	//Takes it to Computer vs Player function
			
		}
		else
			cout << "Error! Please choose a valid answer" << endl;
	}
	return 0;


}

void PlayervsComputer()
{
	//Variables involved
	int Guess_Num1;
	int Computer_Guess;
	int NumofGuesses = 10;
	//What the user is seeing
	do
	{
		cout << "Please guess what I'm thinking between the ranges\n" << endl;
		cout << "of 1-100: " << endl;
		cin >> Guess_Num1;
		NumofGuesses--;		//This helped keep track of the guess and decreased the number of guess. 
		//The number of Guesses was set to 2 at the time. After 2 rounds it went straight to 'if statement' for number of Guess.

		//Validate the input
		if (Guess_Num1 > 100 || Guess_Num1 < 1)
		{
			cout << "Yeah! You got the right answer!" << endl;
		}
		else

			if (Guess_Num1 > Computer_Guess)
			{
			cout << "That number is too high.\n";
			cout << "Please enter a lower number: ";

			}
			else if (Guess_Num1 < Computer_Guess)
			{
				cout << "That number is too low.\n";
				cout << "Please enter a higher number: ";

			}
			else
				cout << "Error! That is outside the range.\n";
			cout << "Please enter within the range of 1-100.\n" << endl;

			//Trying to get count the number of guesses. If number of guesses is gone, then this statement runs 
			//Letting the user know what the answer was

			if (NumofGuesses <= 0)
			{
				cout << "Ran out of guess. Number was " << Computer_Guess << endl;

			}

	}

	while (Computer_Guess != Guess_Num1);
}
void ComputervsPlayer()	//Computer guessing number
{
	int Guess = 0,
		select = 0,
		Max_Num = 100,
		Min_Num = 1;
	int i = 1;
	int NumofGuesses = 10;


	do
		//for (int i = 1; i <= 100; i++)
	{
		
			if (select == 1) Max_Num = Guess - 1;	//Setting Max guessing number limit Too High number gussing 
			else if (select == 2) Min_Num = Guess + 1;	// Setting Low Guessing number limit Too low 
			cout << "I'm thinking of a number between " << Min_Num << " and " << Max_Num << "... ";

			if (Max_Num == Min_Num) Guess = Max_Num;
			else Guess = rand() % (Max_Num - Min_Num) + Min_Num;	//Create random number
			cout << "Is the number " << Guess << endl;
			cout << "Is it too high or to low or right on the mark?\n";
			cout << "Select: 1 for to low, 2 for to high, and 3 for right on the mark.";
			cin >> select;
			NumofGuesses--;	//Counting number of guesses
			
		
		

		//What the user puts in and sees
		if (select == 1)
			cout << "I'll go higher this time!" << endl;
		else if (select == 2)
			cout << "I'll go lower this time!" << endl;
		else if (select == 3)	//Hits the mark
		{
			cout << "Yeah! I got it right on the mark!" << endl;
			break; // Ends the loop
		}
		else		
		{
			cout << "I don't understand?" << endl;
		}
		cout << "Guess left: " << NumofGuesses << endl;	

		/*if (i == 10)
			cout << "You win this round!" << endl;*/
		if (NumofGuesses <= 0)			//What happens when the computer runs out of guess
		{
			cout << setfill('-') << setw(20) << "-" << endl;
			cout << "Ran out of guess. Number was " << num << endl;
			break;

		}

	} while (Guess <= 100 || Guess >= 1);
	playAgain();
}
Last edited on
conversion from 'time_t' to 'unsigned int', possible loss of data".
unsigned is probably 4 bytes, time_t is probably 8.

uninitialized local for my computer_guess
What computer guess is equals to on line 65?
I was able to figure out the problem, I had to set my computer guess into a global variable for the random number to take hold into the other functions. No I just have a problem of trying to close the program when I say no to "Do you wan to play again?".
Topic archived. No new replies allowed.