简体   繁体   English

如何读取输入直到 C 中的空行

[英]how to read input until empty line in C

I am still a beginner to C.我仍然是 C 的初学者。 I am trying to write a program that reads input (test case) until an empty line is reached.我正在尝试编写一个程序来读取输入(测试用例),直到达到空行。

for example, input is the following:例如,输入如下:

2
1 2
2 5
3 6

2 3
1 7

the first number stands for the number of test cases.第一个数字代表测试用例的数量。 in the output there should be sums of the numbers on each line.在 output 中,每行应该有数字的总和。 OUTPUT: OUTPUT:

3
7
9

5
8

and there should be an empty line between them like in the example output.并且它们之间应该有一个空行,就像示例 output 中一样。

#include <stdio.h>

typedef struct {
    int a;
    int b;
} A;

int main() {
    int n, i = 0;
    A nums[100];
    scanf("%d\n", &n);
    while(n--){
        while(empty line not reached){
            scanf("%d %d", &nums[i].a, &nums[i].b);
            i++;
        }
        for(int j = 0; j < i; j++) printf("%d\n", nums[i].a + nums[i].b);
        printf("\n");
        i = 0;
            
    }
    return 0;
}

so what should I write there while(empty line not reached) how do I make the program detect the empty line and go to the next test case?那么我应该在那里写什么呢while(empty line not reached)如何让程序检测到空行和 go 到下一个测试用例?

Empty line has only one symbol.It is "new line" symbol.空行只有一个符号。它是“新行”符号。 So you should look for two new lines symbols.所以你应该寻找两个新的线条符号。 If you work in non-binary mode with file you should look for \n\n symbols.如果您使用文件在非二进制模式下工作,您应该查找 \n\n 符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM