简体   繁体   中英

How to take a integer array in a single line input [in C]

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;
}

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;
}

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");
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM