程序因为if语句而崩溃

Program is crashing because of if statement

本文关键字:崩溃 语句 if 因为 程序      更新时间:2023-10-16

我有这一小段代码基本上是用来比较用户输入的数字和数组中已经存在的数字。如果数字相同,则应该说"Number is valid""Invalid",否则

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;

但是当if语句执行时,程序崩溃。我试着把它注释出来,它工作得很好。我想这只是因为我太蠢了,错过了什么,但我一直盯着这个看了一个小时,我不知道我做错了什么。

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

if(userNumber == list[x])