Testing each array

The program below will generate 5 random numbers and asks the user to enter 5 numbers to see how many numbers he guessed right. What i need help is how do i test each array that the enter inputted, with the numbers the computer generated, with a loop without having both of the index increasing so i can test each individual index with the computer generated numbers 5 times before moving on to the next index for the user inputted index

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
srand(time(0));
	int number[5];
	//store each index with a random number
	for (int x = 0; x < 5; x++)
	{
		number[x] = rand()%5;
	}
	int userNumber[5];
	int right = 0;
	cout << "The computer just generated 5 numbers. Lets test your luck. Please enter 5 numbers: ";
	//input for user 5 numbers
	for (int x = 0; x < 5; x++)
	{
		cin >> userNumber[x];
	}
	//the test
	for (int x = 0; x < 5; x++)
	{
		if (userNumber[x] == number[x])
		{
			right++;
		}

	}
	//the random numbers
	for (int x = 0; x < 5; x++)
	{
		cout << number[x] << ',';
	}
	//results
	cout << "You got " << right << " right!";
	cout << "The numbers were: ";

i can write the test like this:
for (int x = 0; x < 5; x++)
{
if (userNumber[0] == number[x])
{
right++;
}

if (userNumber[1] == number[x])
{
right++;
}
if (userNumber[2] == number[x])
{
right++;
}
}
where i state out the index number but it would take forever if i had 10000 values.
Last edited on
You could use a nested for loop:

1
2
3
4
5
6
for(int x = 0; x < 5; x++)
    for(int y = 0; y < 5; y++)
    {
        if(userNumber[i] == number[j])
            right++;
    }


But I'm not really sure this is what you want--if the user matches all five numbers, he will be told he got 25 right. What behavior are you trying to produce?
Ohhh that worked. ty
Topic archived. No new replies allowed.