multiple inputs by user dynamically at runtime

五迷三道 提交于 2019-12-04 18:18:24

The answer was provided by BLUEPIXY by his nice code. Here we will consider inputs as a pair.

Either it will be a pair of number and space or it will be a pair of number and newline character.

Example: 2 3 4

So in this input we take as pairs, like - '2', '3' and '4\n'. When we encounter a \n we stop the infinite loop. Here the code goes:

#include <stdio.h>

int main(void){
    int n;

    scanf("%d", &n);
    while(n--){
        int v, sum = 0;
        while(1){
            char ch = 0;
            scanf("%d%c", &v, &ch);
            sum += v;
            if(ch == '\n' || ch == 0)
                break;
        }
        printf("%d\n", sum);
    }

    return 0;
}

Inputs:

3
1 6 7
2 7 3 4
2 1

Output:

14
16
3
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!