Array

I've been working on a project for 3 hours now and I've got this random guessing game to work flawlessly but i'm having a hard time creating an array that can keep track of the number of guesses a person takes before getting the random number right. I am completely lost. Here is my code so far.

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
  #include <iostream>
#include <stdint.h>
#include <time.h>
#include <fstream>
using namespace std;
char choice;
int isecret;
void describe_program()
{
	cout << "The object of this game is to guess the random number that's in-between 1 and   100 in as few attempts as possible. ";
	cout << "If you guess the number right you win the   game, if not you can try until you get it right. ";
}
void PlayGame()
{
	int guess;
	switch (choice)
	{
	case 'Y':
	case 'y':
	{
		cout << isecret << endl;
		cout << "Enter a guess between 1 and 100\n";
		cin >> guess;
		
		do
		{
			if (guess < isecret)
			{
				cout << "Your guess is too low\n";
				cout << "Try and guess again\n";
				cin >> guess;
			}

			else if (guess>isecret)
			{
				cout << "Your guess is too high" << endl;
				cout << "Try and guess again" << endl;
				cin >> guess;
			}
			if (guess == isecret)
			{
				cout << "Congradulations, you guess the number right\n";
			}
		} while (guess != isecret);
		break;

	}
	case 'N':
	case 'n':
	{
		return;
	}
	}
}
int main()
{
	describe_program();
	cout << "If you want to play the game   enter 'Y' if not enter 'N' to exist." << endl;
	cin >> choice;
	srand(time(0));
	isecret = rand() % 100 + 1;
	while (choice != 'N' && choice != 'n')
	{
		isecret = rand() % 100 + 1;
		PlayGame();
		cout << "Do you want to play the game again" << endl;
		cin >> choice;
	}
	return(0);

}
Last edited on
Why do you need an array? Just create a normal variable.

int nrOfGuesses = 0;

And each guess, you can do

nrOfGuesses++; this is like nrOfGuesses = nrOfGuesses + 1;
So basically, you increase nrOfGuesses each time they guess. Its that simple mate :)
Wish my teacher made it that simple, but i have to use the array because i have to keep track of the users average for each game played and then finally display it when they decided to exist the game, i have to show them the number of guesses for each individual game at the end of the program, not after each game they finish. hope that makes sense.
Okay so, I made some improvements and implemented what you wanted. Read the comments. Take a look -

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
void describe_program()
{
	cout << "The object of this game is to guess the random number that's in-between 1 and   100 in as few attempts as possible. ";
	cout << "If you guess the number right you win the   game, if not you can try until you get it right. ";
}

void PlayGame(int* nog, int i, int isecret) // nog is the array. 
{

	// Im not sure why you had a switch statement in here.
	int guess;

	cout << isecret << endl;
	cout << "Enter a guess between 1 and 100\n";

	do
	{
		
		cin >> guess;
		
		if (guess < isecret)
		{
			cout << "Your guess is too low\n";
			cout << "Try and guess again\n";
			nog[i] = nog[i] + 1; // increase amount of guess each guess.
		}

		else if (guess > isecret)
		{
			cout << "Your guess is too high" << endl;
			cout << "Try and guess again" << endl;
			nog[i] = nog[i] + 1; // increase amount of guess each guess.
		}

		else
		{
			cout << "Congradulations, you guess the number right\n";
		}

	} while (guess != isecret);

	
}
int main()
{
	char choice; // Dont use global variables. Create them in main.
	int isecret = 0; // read above.
	int nog[20] = {}; // creates an array.
	int i = 0; //This is the amount of rounds played.
	describe_program();
	cout << "If you want to play the game   enter 'Y' if not enter 'N' to exist." << endl;
	cin >> choice;
	srand(time(0));
	while (choice != 'N' && choice != 'n')
	{
		isecret = rand() % 100 + 1;
		PlayGame(nog, i, isecret);
		i++; // increases amount of rounds each round.
		cout << "Do you want to play the game again" << endl;
		cin >> choice;
	}

	cout << endl << nog[0] << endl << nog[1] << endl; 
	// This cout statement is just to show that you can print out the amount of guesses
	// each round.  If you play 3 rounds. The amount of guesses will be stored in
	// nog[0], nog[1] and nog[2].
	return(0);
}
Thank you so much for your help. This is exactly what he wants us to do except for a few problems with the code above. This would work if I was suppose to limit the amount of games a user plays, but since I'm not suppose to, according to this code above I would have to display all 20 elements in the array since I will never know how many times the user plays the game. I was thinking of using a counter in the array and increasing it as the user keeps playing, that way it prints out only for the amount of games the user plays. I got this far but i'm not sure how to actually print it 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
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int asize = 0;
int nog[20];
int isecret;
int i = 1;
int total = 0;
char play = 'g';
void describe_program()
{
	cout << "The object of this game is to guess the random number that's in-between 1 and   100 in as few attempts as possible. ";
	cout << "If you guess the number right you win the   game, if not you can try until you get it right. ";
}
void PlayGame()  
{
	int guess;
	cout << isecret << endl;
	cout << "Enter a guess between 1 and 100\n";

	do
	{

		cin >> guess;
		i++;

		if (guess < isecret)
		{
			cout << "Your guess is too low\n";
			cout << "Try and guess again\n";
		}

		else if (guess > isecret)
		{
			cout << "Your guess is too high" << endl;
			cout << "Try and guess again" << endl;
		}

		else
		{
			cout << "Congradulations, you guess the number right\n";
			nog[asize] = i;
		}
		asize++;
	} while (guess != isecret);


}
int main()
{
	char choice;
	describe_program();
	cout << "If you want to play the game   enter 'Y' if not enter 'N' to exist." << endl;
	cin >> choice;
	srand(time(0));
	while (play == 'g')
	{
		if (choice == 'Y' || choice == 'y')
		{
			isecret = rand() % 100 + 1;
			PlayGame();
			cout << "Do you want to play the game again" << endl;
			cin >> choice;
		}
		else if (choice == 'n' || choice == 'N') 
		 break;
		else
			cout << "Invalid choice";
		break;


	}



}
Using your code, I can only play the game once, if I type in Y or y it still exists after one round.

Edit: Also, Im not sure if this is coming from your professor, but if he or she is letting you use global variables... jeez. Seriously stop using global variables.
Last edited on
Topic archived. No new replies allowed.