程序没有接受预期的输入,无法找出原因

发布于 2025-01-19 04:58:55 字数 539 浏览 4 评论 0原文

该程序应该接受等于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2025-01-26 04:58:55

对于初学者来说,在声明中使用初始化程序

int x = a%b;

会调用未定义的行为,因为变量 a 和 b 未初始化并且具有不确定的值。

此外,此语句

cin >> a, b;

相当于带有逗号运算符的表达式语句

( cin >> a ), b;

,相反,您需要编写

int a, b;

cin >> a >> b;

int x = a % b;

cout << x << '\n';

Also the variable i was notinitialized in the for循环

for(int i; i<t; i++){

您必须编写

for ( int i = 0; i < t; i++ ){

If you don't Want to mix the input with使用标准容器 std::vector 时的输出,例如以下方式

#include <iostream>
#include <vector>

int main()
{
    unsigned int t = 0;

    std::cin >> t;

    std::vector<int> v( t );

    for ( auto &x : v )
    {
        int a, b;

        std::cin >> a >> b;

        x = a % b;
    }

    for ( const auto &x : v )
    {
        std::cout << x << '\n';
    }

    return 0;
}

For starters using the initializer in the declaration

int x = a%b;

invokes undefined behavior because variables a and b were not initialized and have indeterminate values.

Also this statement

cin >> a, b;

is equivalent to the expression statement with the comma operator

( cin >> a ), b;

Instead you need to write

int a, b;

cin >> a >> b;

int x = a % b;

cout << x << '\n';

Also the variable i was not initialized in the for loop

for(int i; i<t; i++){

You have to write

for ( int i = 0; i < t; i++ ){

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

#include <iostream>
#include <vector>

int main()
{
    unsigned int t = 0;

    std::cin >> t;

    std::vector<int> v( t );

    for ( auto &x : v )
    {
        int a, b;

        std::cin >> a >> b;

        x = a % b;
    }

    for ( const auto &x : v )
    {
        std::cout << x << '\n';
    }

    return 0;
}
樱&纷飞 2025-01-26 04:58:55

使用STD :: COUT和STD :: CIN中的常见错误是使用逗号而不是Shift Operator,当Seprate Crammuntes传递给STD :: CIN和STD :: COUT。

因此,您应该使用“ &gt;&gt; ”的“ ”。

std::cin >> a >> b;

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:

std::cin >> a >> b;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文