Help with arrays

I'm a beginner of C++ as you might figure when reading my code below, but I can't get arrays to work as I want it to. I've now spent hours of reading tutorials and other help guides without any success. So now I'm turning to you in hoping one of you could help me in solving this problem with arrays.

I need to store all my guesses in an array, and also display 5 of my guesses in another array.

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 <ctime>
#include <cstdlib>
#include <string>
using namespace std;

int main(){
srand(time(0));
int number = rand() % 50 + 1;
int tries = 0;
const int max = 100;
const int array_size = 5;
int array[array_size] = {};
int guessnumber[max];
int points = 0;
char enter;
int guesses[50];
int n[5];

cout << "Welcome to the guessing game against the computer. The computer will handpick a number that's up to the user to guess." << endl;
do
{
cout << "Please guess my number (1-50) : ";
cin >> guesses[max];
antal++;
if (guesses[max] > number)
cout << "Sorry, you guessed to high." << endl;
else if (guesses[max] < number)
cout << "Sorry, you guessed to low." << endl;
else
cout << "You guessed the correct number in your " << tries << " guesses." << endl;
} while (guesses[max] != number);

if (tries == 1)
{
points = points + 10;
cout << "Points : " << points << endl;
}
else if (tries <= 3)
{
points = points + 5;
cout << "Points : " << points << endl;
}
else if (tries <= 10)
{
points = points + 1;
cout << "Points: " << points << endl;
}
else
{
points = points + 0;
cout << "Points: " << points << endl;
}

if (points = points > 0)
{
cout << "Since you won some points you now have the right to play nothing or double. Do you want to play? (y/n)";
cin >> enter;
}
if((enter=='y')|(enter=='Y'))
cout << "Displaying numbers you guessed on..." << endl;
else
return(0);
for (int i = 0; i < array_size; ++i) {
cout << "Array[" << i << "] => " << array[i] << endl;
}
{
}
    
    return 0;
}


Thanks.
Last edited on
closed account (48T7M4Gy)
I can't get arrays to work as I want it to

Please tell us what the problem is - i.e. what are you trying to do and what is it doing.
Hi,

line 5 causes a problem on line 13, std::array exists in the std namespace. If you don't know what I mean about this, then read up about namespaces.

line 25: What is antal ?

line 36 can be better written:
points += 10;

Similar for line 41

Try to avoid magic numbers in your code like 5 or 10, make them const variables instead, like you did earlier in the code.

line 51: Is, well, pointless :+)

Line 55: Did you mean if (points > 0) ?

Line 63 ends the program, before lines 64 & 65, did you mean these two line to be part if the if statement on line 60? If so, make use of some braces to associate those statements with the if statement.

If your code doesn't compile, then you should post the errors here, verbatim.

hope all goes well.

Edit : Consider making all your variables unsigned int, except the char
Last edited on
Thanks for the tips, however I've now resolved it.

But I've stumble on a new problem. How do you generate a random number from an array?

Example:

My program now displays 5 of the guesses I recently guessed on, but now I want my program to generate a random number among this 5 guessed numbers.

Thanks again.
Topic archived. No new replies allowed.