从整数列表中交叉检查整数运行时错误
cross checking integer from list of integers runtime error
以下代码已尽可能简化,应该执行以下操作:
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;
相关文章:
- 检查输入是否不是整数或数字
- 检查TCHAR数组输入是否为带符号整数C++
- 检查字符串是否为整数
- 检查节点的输入验证的整数
- 如何检查是否定义了固定宽度的整数
- 优化正在杀死我在 clang 6 中的整数溢出检查
- 从双精度转换为整数的显式类型是否始终检查整数溢出?
- 将字符串转换为整数类型T,检查是否存在溢出
- 在C/C++中检查数字是否为整数(不带scanf/gets/etc)
- 如何在 c++ 中解析包含整数的字符串并检查是否大于最大值
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- QT按钮组和可检查的按钮:如何将切换信号与整数和布尔值连接?
- 如何检查商是否为整数?C++
- 检查Android NDK COD中的整数溢出
- 如何使用条件来检查类型名 T 是否是 C++ 中浮点类型的整数类型
- 我们如何找到 N sqrt(m) 并检查答案是否是 C++ 中的整数
- 检查整数是否增加了C++
- 检查用户输入的整数是否包含所有 1 或 0?
- 如果数字是整数,我如何检查此代码
- 如何检查整数是否包含数字 0 和 1