RyanJohnTyler
RyanJohnTyler

Reputation: 75

Program is crashing because of if statement

So I have this little bit of code that's basically just supposed to compare a number that a user enters to numbers already in an array. If the number is the same it is supposed to say "Number is valid" and "Invalid" otherwise.

const int size=18;
int list[size]={5658845,4520125,7895122,8777541,8451277,1302850,8080152,4562555,5552012,
5050552,7825877,1250255,1005231,6545231,3852085,7576651,7881200,4581002};
bool found = false;
int userNumber;


cout<<"Enter your number: ";
cin>>userNumber;


for(int x = 0;x < 18; x++)
{
    if(list[userNumber] == list[x])
        found = true;
}
if(found)
    cout<<"The number is valid."<<endl;
else
    cout<<"The number is invalid."<<endl;

return 0;

yet when the if statement strikes, the program crashes. I've tried commenting it out and it works fine. I assume it's just cause I'm being a dumbass and missing something but I've been staring at this for the past hour and I can't figure out what I'm doing wrong.

Upvotes: 0

Views: 565

Answers (1)

slipperyseal
slipperyseal

Reputation: 2778

You are referencing the array with the user number as the index, where i think you simply wish to compare each list element with the actual user number.

if(userNumber == list[x])

Upvotes: 4

Related Questions