从整数列表中交叉检查整数运行时错误

cross checking integer from list of integers runtime error

本文关键字:整数 检查 运行时错误 列表      更新时间:2023-10-16

以下代码已尽可能简化,应该执行以下操作:

1) 用户填充 5 个整数的列表。

2)用户输入一个"数字"。

3a) 如果"数字"与 5 个整数之一相同 - 将显示该数字的双倍,然后重复 2。

3b) 如果"数字"与 5 个整数之一不同 - 结束程序。

但是,在运行时,我输入了一个列表中的数字,并得到输出响应,说它不在列表中。 同样,激活此 else 语句后,人们会期望 while 循环以 a = 0 的形式停止,但它继续循环。

这是我认为错误所在,有关列表中未显示的数字:

if (number == list[x])

我可能在括号中使用了错误的陈述。

但是我不知道为什么它也继续循环,也许两个错误都是因为同一件事。任何帮助使程序如上所述运行将不胜感激。

这是完整的代码:

#include <iostream>
using namespace std;
int main()
{
int list[5];
int a, x, number;
a = 1;
cout << "Fill up the list" << endl;
for (x = 0; x <= 4; x++){
    cin >> list[x];
    }
while (a = 1) {
cout << "enter one of the numbers you put on the list" << endl;
cout << "we will double it " << endl;
cin  >> number ;
if (number == list[x])
{
    cout << "double that number is " << number * 2 << endl;
}
else
{
    cout << "Thats not on the list" << endl;
    a = 0;
}
}
return 0;
}

我已经改变了

if (number == list [x])

if ((number == list[0])||(number == list[1])|| (number == list[2]) || (number == list[3])||(number == list[4]))

并更改了

while (a = 1)

while (a == 1)

但是,如果有人知道替代方案而不是

if ((number == list[0])||(number == list[1])|| (number == list[2]) || (number == list[3])||(number == list[4]))

我愿意接受建议。

完整的代码现在是:

#include <iostream>
using namespace std;
int main()
{
int list[5];
int a, x, number;
a = 1;
cout << "Fill up list bitch" << endl;
for (x = 0; x <= 4; x++){
    cin >> list[x];
    }
while (a == 1) {
cout << "enter one of the numbers you put on the list" << endl;
cout << "we will double it " << endl;
cin  >> number ;
if ((number == list[0])||(number == list[1])|| (number == list[2]) || (number == list[3])||(number == list[4]))
{
    cout << "double that number is " << number * 2 << endl;
}
else
{
    cout << "Thats not on the list you scumbag" << endl;
    a = 0;
}
}
return 0;