0

How to take single line input and store values in its array in C language

#include <stdio.h>

int main()
{
    int arr[5];

    scanf("%d", &arr[0]);

    for(int i=1; i <= 5; i++){
        printf("%d\n", arr[i-1]);
    }

    return 0;
}
  • 2
    You have a loop that *writes* the array to stdout. Not surprisingly, you need one of those (a loop of some form) that *reads* values as well. Obviously the existing code reads *one* value. So... change that? – WhozCraig Jul 11 '22 at 14:57
  • If you input each array value in a loop (perhaps similar to how you output in a loop), you can enter all 5 values on a single line. All that matters is they are separated by some kind of whitespace character(s). – Weather Vane Jul 11 '22 at 15:05
  • @WeatherVane But I want user to input a single line input, see above code again, you may relate it – SUD pythonism Jul 11 '22 at 15:14
  • As I said, you can present all five inputs *on a single line*, even if the instruction to scan each one is in a loop. – Weather Vane Jul 11 '22 at 15:15
  • 1
    `scanf()` doesn't care whether they're on a single line or separate lines. Any whitespace between the numbers will be used. – Barmar Jul 11 '22 at 15:24
  • You might be interested in [What can I use for input conversion instead of scanf?](https://stackoverflow.com/questions/58403537) – Steve Summit Jul 11 '22 at 15:24
  • Is your program allowed to assume that exactly 5 numbers will be entered on the same line of input? Or will it be up to 5 numbers? – Andreas Wenzel Jul 11 '22 at 15:51
  • 1
    SUD pythonism, what should happen when more or less than 5 numbers are on a line? What should happen if input is not numeric? – chux - Reinstate Monica Jul 11 '22 at 16:42

3 Answers3

2

If I understand you correctly, you want to get input from the user and store it in your array, and then you want to print the array.

First, if you need input from user, I like to print a line that asks this from the user, so let's add it to your code.

#include <stdio.h>

int main()
{
    int arr[5];

    printf("Please enter 5 numbers:\n");
    scanf("%d", &arr[0]);

    for(int i=1; i <= 5; i++){
        printf("%d\n", arr[i-1]);
    }

    return 0;
}

Now I can see that you tried to store values in your array, but you did it only for the first one (arr[0]). You should make a loop for that (like you did in the end of your code). Let's add this loop.

#include <stdio.h>

int main()
{
    int arr[5];

    printf("Please enter 5 numbers:\n");
    for(int i=1; i <= 5; i++){
        scanf("%d", &arr[i-1]);
    }

    for(int i=1; i <= 5; i++){
        printf("%d\n", arr[i-1]);
    }

    return 0;
}

Done, it should work now.

Just letting you know that instead of writing 5 (the size of your array), you can define it in the beginning of your code once. (and if sometime you want to change the size, you just need to change it once.)

#include <stdio.h>
#define N 5

int main()
{
    int arr[N];

    printf("Please enter %d numbers:\n", N);
    for(int i=1; i <= N; i++){
        scanf("%d", &arr[i-1]);
    }

    for(int i=1; i <= N; i++){
        printf("%d\n", arr[i-1]);
    }

    return 0;
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
Alfa Hores
  • 347
  • 2
  • 10
1

scanf is for parsing whitespace delimited data, not line-oriented data. If you want to read lines it is much better to use fgets. If you want to read lines, and then break down the lines into whitespace delimited data, you can use sscanf on the buffer returned by fgets:

#include <stdio.h>

#define MAXINTS 100   /* maximum number of integers we can read */
#define MAXLINE 1024  /* maximum input line length */

int main() {
    char buffer[MAXLINE], *p;
    int data[MAXINTS];
    int count = 0, len;

    printf("Enter some numbers: ");
    fgets(buffer, sizeof(buffer), stdin);
    p = buffer;
    while (count < MAXINTS && sscanf(p, "%d%n", &data[count], &len) > 0) {
        ++count;
        p += len; }

    printf("Read %d numbers on a line:", count);
    for (int i = 0; i < count; ++count) printf(" %d", data[i]);
    printf("\n");
}
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
0

If you want your program to read up to 5 numbers that are entered on a single line, then I do not recommend that you use the function scanf. That function is generally not very useful for line-based user input. The function scanf will continue reading more lines of input, until the user has entered exactly 5 numbers or until a conversion error occurs.

In order to read a single line of input, you can use the function fgets to read that line as a string. You can then use the function sscanf or strtol to convert the individual numbers of that string.

If there are supposed to be up to 5 numbers in that string, then you will probably want a loop that iterates up to 5 times and attempts to convert one number per loop iteration.

Alternatively, if you are using sscanf, you could also attempt to convert five numbers at once, using the format string "%d%d%d%d%d" and passing five arguments to the function sscanf. You can then examine the return value of sscanf to determine how many numbers were actually converted.

However, if you do not want to enforce that all numbers are on the same line, and you just want to read exactly 5 numbers from the user, then the solution is simpler. You can use the same kind of loop for scanf that you are already using for printf.

Note that the loop

for(int i=1; i <= 5; i++){
    printf("%d\n", arr[i-1]);
}

can be simplified to the following:

for(int i=0; i < 5; i++){
    printf("%d\n", arr[i]);
}

In accordance with the community guidelines for homework questions, I will not provide a full code solution at this time. You will learn more if you manage to solve the problem yourself. However, I will give you further tips, if necessary.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • *Why* can't you input 5 values for separate calls to `scanf`, all on a single line? Not of them are output until all have been read. – Weather Vane Jul 11 '22 at 15:20
  • @WeatherVane: OP is asking how to "take single line [of] input". This condition will not be enforced by using `scanf`, as it may read several lines. However, it is unclear whether OP actually wants to enforce this. It is unclear whether the program is allowed to assume that exactly 5 numbers will be entered on a single line by the user. In my answer, I stated that the solution you describe is also possible, if OP does not want to enforce that all 5 numbers are on the same line. – Andreas Wenzel Jul 11 '22 at 15:41
  • OP never says it must be enforced, only that they want to input on one line. At the level of the question, there is no reason why `scanf` can't be used. It seems that OP doesn't understand `scanf` and I don't see any need to overcomplicate it. – Weather Vane Jul 11 '22 at 15:44
  • @WeatherVane: OP does not specify whether they want to read up to 5 numbers or exactly 5 numbers on one line, and has not yet responded to my request for clarification. If it is the former, then using `scanf` will not work, as it will continue reading new lines of input until the user has entered 5 numbers or until a conversion error occurs. – Andreas Wenzel Jul 11 '22 at 16:07