程序没有接受预期的输入,无法找出原因
该程序应该接受等于 t 值的输入 a、b,但它似乎只接受一个输入,然后传递输出。 例如,输入:
3
1 2
100 200
40 15
只会导致输出:
3
1 2
01
100 200
预期输出应该是:
1
100
10
程序:
#include <iostream>
using namespace std;
int main()
{
int t;
cin >> t;
for(int i; i<t; i++){
int a, b;
int x = a%b;
cin >> a, b;
cout << x;
}
return 0;
}
我已尝试分解 cin >>>甲、乙;分成两个不同的命令,但这不起作用。我还搞乱了变量在代码中的位置。这些都没有帮助。
This program should take input a,b equal to the value of t, instead it seems to only take one input and then deliver the output.
For example, the inputs of:
3
1 2
100 200
40 15
leads only to an output of:
3
1 2
01
100 200
the expected output should instead be:
1
100
10
The program:
#include <iostream>
using namespace std;
int main()
{
int t;
cin >> t;
for(int i; i<t; i++){
int a, b;
int x = a%b;
cin >> a, b;
cout << x;
}
return 0;
}
I have tried breaking up cin >> a, b; into two different commands but that didn't work. I also messed around with where the variables are located in the code. None of that helped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于初学者来说,在声明中使用初始化程序
会调用未定义的行为,因为变量 a 和 b 未初始化并且具有不确定的值。
此外,此语句
相当于带有逗号运算符的表达式语句
,相反,您需要编写
Also the variable
i
was notinitialized in the for循环您必须编写
If you don't Want to mix the input with使用标准容器
std::vector
时的输出,例如以下方式For starters using the initializer in the declaration
invokes undefined behavior because variables a and b were not initialized and have indeterminate values.
Also this statement
is equivalent to the expression statement with the comma operator
Instead you need to write
Also the variable
i
was not initialized in the for loopYou have to write
If you do not want to mix the input with the output when use the standard container
std::vector<int>
for example the following way使用STD :: COUT和STD :: CIN中的常见错误是使用逗号而不是Shift Operator,当Seprate Crammuntes传递给STD :: CIN和STD :: COUT。
因此,您应该使用“ &gt;&gt; ”的“ ,”。
common mistakes in using std::cout and std::cin ,is using comma instead of shift operator when seprate arguments passed to std::cin and std::cout.
so you should use ">>" of "," when pass new arguments: