How to receive C input data until no more input?

Asked

Viewed 2,365 times

3

So here’s the deal:

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

In my program, the user must enter a chained list in the following format:

num0 num1 num2 num3 num4 num5 num6 num7 .. numN.

Ex. of 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 input after last number is typed?

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

Ex. User type: 4 5 6 7 8 9 10 and now [ENTER]. How to read the list since I use no conditions to stop the entry?

The program must accept negative, non-negative and zero integers.

3 answers

2

Without a context it is not possible to offer the best solution, considering that all items will be inserted and finalized with a ENTER and only when you have a ENTER without typing anything else is that should close, one of the possible solutions can 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;
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you can accept negative numbers this solution does not work, but the question gave margin. In this case it would complicate a lot and maybe the scanf() is not the best solution, unless you use some trick.

  • I didn’t say, but I will have to use negative numbers as well. Anyway, thanks for the help.

  • @L.Pedro lack other details to give a more appropriate answer, to tell the truth maybe this question should be closed as unclear, even saying this, it is still complicated to give an answer that meets exactly what you want, without having to guess.

  • I updated the question, take a look.

  • The problem is that there are already 3 answers to the original, so it is too late, if no answer was useful then I think the question should be closed because it is not clear and create a new one. Anyway you need to do better than that, you are still confused and as it indicates to be a much more complicated problem you need to post in the new question what you have already done and what problem you are facing to have a specific question. The current form the question does not work here since you are asking to do everything for you and it is not something so trivial.

1


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

#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 goes on here?

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

Just like reading a file, when reading the values of stdin, the language goes through until reaching the end of the insertion, but how to define the end of the insertion?

Note that in the scanf has the following format "%d%c", when the user enters 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 enter, c will equal '\n', thus making the While close and all sums are added to their variable.

When the entry is made through files, the scanf can identify the latest 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;
}

inserir a descrição da imagem aqui

  • I updated the question, because, the way you taught me, I’m having problems. I think I’m now clearer.

  • @L.Pedro, should you have a problem with what kind of entrance? I did the test with how many numbers you wanted, positive, negative and zeros, there was no mistake, but maybe you should look for signs, who knows this can help you

  • In fact, it’s working. In my program, after receiving entries, I invert the list and print it backwards; however, this program is part of a problem I’m solving on a site where you solve logic problems and submit your source code to an evaluation. On my computer it is working, but on the server of this site, when they run, it generates an execution error. I will give as solved, because the problem may be elsewhere. Thank you very much. Detail: Their system says it generated an error, but does not show which.

  • @L.Pedro, then... this would never work, because the entry made by websites with logic exercises, use entries through files, and this has a difference when programming, I added more things in the answer. When asking the question, saying purpose and/or where the code will run, it helps a lot because of the variations.

0

while (scanf("%d",&num) != EOF) {
sum += num;
}

To close the entry type CTRL-D.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.