-4

My Input is as below.

3 8 9 3
4 2 4 0 3
5 1 5 9 3 1
0
1 5

As you can see, the first number of each line means the number of how many inputs left in the line.

How can I get all input via scanf? Or please let me know something new.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jung Jaehoon
  • 101
  • 1
  • 12
  • 2
    Why don't you use loop? – Risul Islam Sep 22 '19 at 11:17
  • 2
    Read an int. Read that many more. Don't forget to **always** check `scanf()`'s return value to see if it succeeded or not before trying to use variables it sets. – Shawn Sep 22 '19 at 11:18
  • Please provide a [mre] to show what you already know and what you don't. And please take a look at the [C book list](https://stackoverflow.com/q/562303). – L. F. Sep 22 '19 at 11:19
  • 2
    `scanf` is a pretty poor tool for this job. For one thing, it's stream-based, not line-based. That is, it doesn't pay attention to line boundaries -- a newline is just another whitespace character, as far as it is concerned. So while you could write some straightforward code to (a) read the number at the beginning of the line and (b) use a loop to read that many more numbers, it would happily read the two-line input `10 1 2 3 4` followed by `5 6 7 8 9 10` as **one** set of numbers. – Steve Summit Sep 22 '19 at 11:24
  • Wherreas I completely agree with @Steve about `scanf` being a poor tool for this job, I want to highlight an important implication of `scanf` being stream-based instead of line-based: you don't necessarily consume a whole line in each call, contrary to what the question title leads me to suppose you may be thinking. If you are *required* to use `scanf` for this exercise then that's why everyone is talking about loops. – John Bollinger Sep 22 '19 at 11:29
  • If you want to deal with line-based input, you want to use an input function that deals with whole lines, such as `fgets`. So the other way of doing this (without `scanf`) is to (a) read a line with `fgets` and (2) handle each number on the line. To break the line up at whitespace so you can handle each number individually, you can either (a) use something like `strtok` or (b) create an array of "words" using a function such as `getwords` as described in [section 10.8](https://www.eskimo.com/~scs/cclass/notes/sx10h.html) of these [C notes](https://www.eskimo.com/~scs/cclass/notes/top.html). – Steve Summit Sep 22 '19 at 11:37
  • 1
    Amplifying on what @JohnBollinger said, if you were asking how to read the whole line with just *one* call to `scanf`, where you somehow told `scanf` how many `%d`'s to use based on the first number on the line, I'd say that's impossible. – Steve Summit Sep 22 '19 at 11:41

1 Answers1

0

As mentioned in the comments, the problem with scanf() is (among other things) the way it handles the newline character. However, the sscanf() function (reading from a string) doesn't have this issue! So, depending on exactly what you're trying to achieve, maybe something like the following code will help:

#include <stdio.h>

int main()
{
    int a[5], n = 0, given;
    char buffer[200];
    while (n >= 0) {
        printf("\nEnter n and a list of numbers: ");
        fgets(buffer, 200, stdin);
        printf("Input was: %s", buffer);
        n = -1;
        given = sscanf(buffer, "%d %d %d %d %d %d", &n, &a[0], &a[1], &a[2], &a[3], &a[4]);
        if ((given < 1) || (given != n + 1)) printf("Invalid input!\n");
    }
    return 0;
}

I'm not saying it's an ideal solution, or even that you could use it, but it may give you some clues as to where to go next.

Let me know how you find it.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • @SteveSummit Indeed, but I used that to keep the code simple and easily readable. Had I used a larger number, the `sscanf(…)` line would start to look clumsy. It is, as I said, not an ideal solution - rather a suggestion/hint/pointer as to what may be possible. – Adrian Mole Sep 23 '19 at 11:33
  • 1
    I misspoke on the count of zero. – Jonathan Leffler Sep 23 '19 at 13:58