Help with an array

I am trying to write a game where you need to try to guess the random number between 1 and 100. The basic code is a simple do-while loop and it works normally.

My problem is an array that contains the players guesses. It somehow ignores the players first two guesses and adds two zeroes in the end of the array. I have tried to figure this out for a while, but I don't seem to find the error. :(



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
int main()
{

	srand(time(NULL));
	int correct = (rand()%100)+1;
	int guess;
	int guesses = 0;
	int lastGuess [30] = {};
	int i;
	
		cout << "\nGuess the number." << endl;
		cout << "It must be between 1 - 100." << endl << endl;
		
	do
	{
	cout << "Insert your guess: ";
	cin >> guess;
	guesses++;
	lastGuess[i++]=guess;
	
		if(guess < correct)
		{
		cout << "The number is higher than " << guess << endl;
		}
		else if(guess > correct)
		{
		cout << "The number is lower than " << guess << endl;
		}
		else
		{
		cout << "\nCorrect! The answer was " << correct << "." << endl << endl;
		}
	}
	while (guess != correct); 
	
		cout << "You needed to guess " << guesses << " times to get it right." << endl;
		cout << "\nYour guesses were: ";
	
	i=0;
	
	for(guesses; guesses>0; guesses--)
	{
		cout << "[" << lastGuess[i++] << "] ";
	}
	
	cout << "\n";
	
	cin.ignore();
}
look at line 19, then ask if you're really sure what the value of i is at the start of the program.

Also your indentation style seems very arbitrary which leads to confusion. At first glance, I thought line 34 started a while loop and you left the braces out of line 35 and 38.

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
int main()
{

	srand(time(NULL));
	int correct = (rand()%100)+1;
	int guess;
	int guesses = 0;
	int lastGuess [30] = {};
	int i;

	cout << "\nGuess the number." << endl;
	cout << "It must be between 1 - 100." << endl << endl;

	do
	{
		cout << "Insert your guess: ";
		cin >> guess;
		guesses++;
		lastGuess[i++]=guess;

		if(guess < correct)
		{
			cout << "The number is higher than " << guess << endl;
		}
		else if(guess > correct)
		{
			cout << "The number is lower than " << guess << endl;
		}
		else
		{
			cout << "\nCorrect! The answer was " << correct << "." << endl << endl;
		}
	} while (guess != correct); 

	cout << "You needed to guess " << guesses << " times to get it right." << endl;
	cout << "\nYour guesses were: ";

	i=0;

	for(guesses; guesses>0; guesses--)
	{
		cout << "[" << lastGuess[i++] << "] ";
	}

	cout << "\n";

	cin.ignore();
}
Last edited on
30 guess is too much, 7 or 8 guess is enough, otherwise game loss!
and if the correct answer comes after 30 try, overflow may occur, ie lastguess[30], lastguess[31], ........
so use <vector>
Thanks Esslercuffi. The problem is solved by defining 0 as the value of i. But that is a bit confusing for me. Why its value was 2 before?
Since you had not initialized i, the value stored in i was just whatever random junk happened to be in memory at the location i was assigned to point to by the compiler. Always initialize your data.
Topic archived. No new replies allowed.