程序因为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])
相关文章:
- 当回溯以零开始时,如何调试崩溃
- 内联映射初始化的动态atexit析构函数崩溃
- 执行函数时导致崩溃的变量
- 我的简单if-else语句是如何无法访问的代码
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 为什么是0;C++中的有效语句
- 程序崩溃并显示"std::out_of_range"错误
- C++ 非 void 函数的崩溃没有返回语句
- C++:添加逻辑上正确的 if 语句会使我的程序崩溃
- C++在try-catch语句处崩溃
- C++-映射迭代器中的if语句导致程序崩溃的原因
- 使用 Qt5 的抛出语句时即时崩溃
- 由于 if 语句,C++ 程序崩溃
- 返回 std::string 的函数在没有 return 语句的情况下崩溃,这与返回 int 而不返回语句的函数不同
- 为什么我的程序在返回语句时崩溃
- C++ 随机 COUT 语句导致程序在重载的"="运算符内崩溃
- 程序因为if语句而崩溃
- 为什么当我忘记返回语句时,我的c++程序崩溃了,而不是仅仅返回垃圾
- 为什么这个sprint语句崩溃了