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:
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.
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.
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.
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?
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) { .. }