Thread: Loop doesn't seem to function properly

1. Loop doesn't seem to function properly

Code:
#include <iostream>

using namespace std;

const int MAX_P = 4;
const int MAX_C = 10;
int inArray(int a[], int sc)
{
for(int i = 0; i < MAX_P; i++)
{
if(sc == a[i]) {
return 1;
} else {
return 0;
}
}
}

int main() {
int computer[MAX_P], user[MAX_P];
int i;

for (i = 0; i < MAX_P; i++) {
computer[i] = (rand() % MAX_C);
cout << computer[i];
}

cout << endl << endl;

for (i = 0; i < MAX_P; i++) {
cout << "Geef een nummer voor " << i << ": ";
cin >> user[i];
}

for (i = 0; i < MAX_P; i++) {
if (inArray(computer, user[i])) {
if (user[i] == computer[i]) {
cout << "getal op de goede plek" << endl;
} else {
cout << "getal wel in de reeks" << endl;
}
} else {
cout << "getal niet in reeks" << endl;
}
}

cin.get();
return 0;
}
The purpose of this program is for the PC to 'generate' 4 numbers (and put them in computer[]).
Our user has to fill in 4 numbers and the computer matches them and tells the user which ones are on the right position or not even part of the array.

I really can't grasp why, but this code doesn't seem to work out..
If i lower MAX_P to 1 it works, but with any higher number it justs checks the first number for validity.

Maybe someone can help me with this?

2. If i lower MAX_P to 1 it works, but with any higher number it justs checks the first number for validity.
Code:
if(sc == a[i]) {
return 1;
} else {
return 0;
}
The function will return 0 if the first element of a[] doesn't match sc. Remove the return 0 from the loop and put it after the loop, so if you don't get a match from any of the numbers, the function returns zero.