简体   繁体   中英

how to read input until empty line in C

I am still a beginner to 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:

3
7
9

5
8

and there should be an empty line between them like in the example 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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