How to receive input data in C until you have no input?

3

The deal is as follows:

How to receive C (integer) entries until "ENTER" is pressed?

In my program, the user should enter a list linked in the following format:

num0 num1 num2 num3 num4 num5 num6 num7 num

Ex. list:

19 8 9 14 15 9 -9 0 7 6 5 4 -123 1.

The size of the list is unknown. It can be as many integers as the user wants to type.

How to stop the entry after the usefulness number is entered?

What I need is that after the enter is pressed, the program stops reading the entries.

Ex. The user types: 4 5 6 7 8 9 10 and now [ENTER]. How to read the list, since I do not use any conditions to stop the entry?

The program must accept negative, nonnegative, and zero integers.

    
asked by anonymous 24.11.2016 / 21:28

3 answers

1

You can set a period to end the reading, in case the end is the ENTER line break.

#include <stdio.h>

int main(int argc, char **argv){
    char c = 0;
    int sum = 0;
    int n;
    while(c != '\n'){
        scanf("%d%c", &n, &c);
        sum += n;
    }

    printf("%d\n", sum);

    return 0;
}

What happens here?

The scanf takes the values directly from the stdin , which is the value input, and the stdin has the same properties as a file (has a value reserved for it).

As with reading a file, when reading the values of stdin , the language goes through until it reaches the end of the insertion, but how do I set the end of the insertion?

Note that no scanf has the following format % d% c , when the user types a number, he collects the number and collects the next character entered.

When the character after the number is a line break, or when you hit the enter, c will equal '\n' , thus causing While ends and all sums are added to your variable.

When input is done through files, scanf can identify the last information through having a return less than 1.

Ex:

#include <stdio.h>

int main(int argc, char **argv){
    char c = 0;
    int sum = 0;
    int n;
    while(scanf("%d",&n) > 0){
        sum += n;
    }

    printf("%d\n", sum);

    return 0;
}

    
25.11.2016 / 04:58
2

Without a context it is not possible to offer the best solution, considering that all the items will be inserted and finished with a ENTER and only when you have ENTER without typing anything else is that it should terminate, one of the possible solutions might be this:

#include <stdio.h>

int main(void) {
    int sum = 0;
    while (1) {
        int num = -1;
        scanf("%d", &num);
        if (num >= 0) {
            sum += num;
        } else {
            break;
        }
    }
}

See running on ideone and on CodingGround .

If you can accept negative numbers this solution does not work, but the question has given way. In that case it would be quite complicated and maybe scanf() is not even the best solution, unless you use some trick.

    
24.11.2016 / 21:42
0
while (scanf("%d",&num) != EOF) {
sum += num;
}

To close the entry type CTRL-D.

    
24.11.2016 / 22:21