简体   繁体   中英

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.

您正在引用以用户编号作为索引的数组,我认为您只是希望将每个列表元素与实际用户编号进行比较。

if(userNumber == list[x])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM