problems getting guessing game to work

I got an assignment in my programming class, where I supposed to make a small game, which should include: rand(), loops, arrays, functions and pointers.
So my code is looking like this:

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
#include <iostream>
#include <time.h>

using namespace std;

int main()
{
	int Guesses = 5;
	int Guess;
	int Answer;

	int myArray[20] = {rand()%20+1};

	
	Answer =  (int) &myArray[1];

	cout << "Welcome to my little guessing game.\n";
	cout << "I'm thinking of a number between 1 and 20, can you guess it in " << Guesses << " tries?\n";

		for (int i = 0; i < Guesses; i++)
		{
			cout << "Guess #" << i+1 << ": ";
			cin >> Guess;

		if (Guess != Answer)
		{
			if (Guess > Answer)
				cout << "too high.\n";
			else 
			    cout << "too low.\n"; 
		
		
	     }
		else
		{
		cout << "you won.\n";
		system("pause");
		return 0;
		}
	}



	cout << "you lost\n";
	cout << "the right answer was: " << Answer << endl;
	system ("pause");

	return 0;
} 


but the number is some random big number instead of a number between 1 and 20.
can't fix this problem, what is the problem?
What the hell are you doing on lines 12 and 15? o_O

First, you initialize an array of 20 pointers with a single value (thus myArray[0] has a value, the rest is uninitialized). Then, you cast the address of the first element of that array to an int, and save it.

The fix is straightforward (in fact, it's the obvious way, rather than... this), but I fear you might stop posting if I give it to you, and I really want the explanation of that code.
Care to explain what the purpose of the array is supposed to be?

(int) &myArray[1] converts the address of the second element to an int. Never use this type of cast, use the C++ cast operators instead (e.g. static_cast). If you had done that, you would've noticed this is incorrect.
As a rule of thumb in the beginning: if you need a cast, you did something wrong.

Also, indices in an array start at 0, not 1.
well i'm actually have no idea, just needed to include a pointer, and thought I would address it to the first number in myArray, but clearly I did something wrong doing so.
Well, that's not the way to do it. If you have to use a pointer, try dynamically declaring answer, as such:
1
2
3
4
int *Answer = new int;
*Answer = rand()%20+1;
// Code goes here
delete Answer;


P.S.: The first number in an array is array[0], not array[1].
Well the purpose of the array, was meant to be have 20 numbers, 1-20 , which would be picked randomly.
You aren't doing that, though. The first element of the array is a random number between 1-20, the rest is initialized to zero.
And you're always using the second value in the array (or rather, its address), not a random one.
Then you'd either have to put "rand()%20+1" 20 times, or use empty-initialization and then use a loop to assign values.
Garminic, your pointer example do not seems to work. get 6 new errors, the Answer int in line 10, differs from the *Answer. why do the compiler complain?
is this what you mean?

int *Answer = new int;
*Answer = rand()%20+1;
int Guesses = 5;
int Guess;
int Answer;
You don't need the last line. "Answer" is already defined (by my code snippet).
You'll have to use *Answer to get the value though, so comparing the value of Guess to Answer would be:
if (Guess == *Answer) { .. }
Last edited on
Topic archived. No new replies allowed.