繁体   English   中英

程序因if语句而崩溃

[英]Program is crashing because of if statement

所以我有一点点代码,基本上只是将用户输入的数字与数组中已有的数字进行比较。 如果数字相同,则应该说"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])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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