7

I know that I can use

scanf("%d %d %d",&a,&b,&c): 

But what if the user first determines how many input there'd be in the line?

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
Soham
  • 203
  • 1
  • 2
  • 6

4 Answers4

6

You are reading the number of inputs and then repeatedly (in a loop) read each input, eg:

#include <stdio.h>
#include <stdlib.h>

int main(int ac, char **av)
{
        int numInputs;
        int *input;

        printf("Total number of inputs: ");
        scanf("%d", &numInputs);

        input = malloc(numInputs * sizeof(int));

        for (int i=0; i < numInputs; i++)
        {
                printf("Input #%d: ", i+1);
                scanf("%d", &input[i]);
        }

        // Do Stuff, for example print them:
        for (int i=0; i < numInputs; i++)
        {
                printf("Input #%d = %d\n", i+1, input[i]);
        }

        free(input);
}
ccKep
  • 5,786
  • 19
  • 31
0

Read in the whole line, then use a loop to parse out what you need.

To get you started:

1) Here is the manual page for getline(3): http://man7.org/linux/man-pages/man3/getline.3.html

2) Some alternatves to getline: How to read a line from the console in C?

3) Consider compressing spaces: How do I replace multiple spaces with a single space?

4) Use a loop for parsing. You might consider tokenizing: Tokenizing strings in C

5) Be careful and remember that your user could enter anything.

Community
  • 1
  • 1
Madmartigan
  • 143
  • 1
  • 7
0
#include <conio.h>
#include <stdio.h>
main()
{
    int  a[100],i,n_input,inputs;
    printf("Enter the number of inputs");
    scanf("%d",&n_input);

    for(i=0;i<n_input;i++)
    {
        printf("Input #%d: ",i+1);
        scanf("%d",&a[i]);
    }

    for(i=0;i<n_input;i++)
    {
        printf("\nInput #%d: %d ",i+1,a[i]);
    
    }

}    
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 29 '21 at 15:39
0
    /*
_______________This program is in C Programming Language_______________

We have to directly enter all the elements in one line giving  spaces between them. Compiler will automatically ends the for loop I have used and assign the value to their respective variables or array indexes. Below program and output will give you better understanding. 
*/
#include <stdio.h>

int main()
{
    //taking no of inputs from user
    int len;
    printf("Enter the number of inputs you want to enter : ");
    scanf("%d", &len);
    int i;
    //defined an array for storing multiple outputs
    int arr[100];
    //included a printf statement for better understanding of end user
    printf("Enter the inputs here by giving space after each input : ");
    /*here is the important lines of codess for taking multiple inputs on one line*/
    for (i=0;i<len;i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("Your entered elements is : ");
    for (i=0;i<len;i++)
    {
        printf("%d ", arr[i]);
    }
}

/*
OUTPUT :
Enter the number of inputs you want to enter : 5
5 5 5 8 7 
Your entered elements is : 5 5 5 8 7
*/
  • Interesting suggestion to use "\a", I didn't know how that worked. It would be good to put in a link to reference what "\a" means to scanf to explain your answer. It looks like "\a" is actually the BELL or ALERT char. scanf behaviour seems to be to treat it as a whitespace char on my linux machine. So +1 vote for interesting and educational suggestion but I would say the behaviour is undefined when using "\a" and not all scanf will work the same. – gaoithe Oct 28 '22 at 15:41
  • Reference: https://man7.org/linux/man-pages/man3/sscanf.3.html "A directive is [] A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including none, in the input." And see also: https://stackoverflow.com/questions/62914795/how-do-escape-sequences-actually-behave-in-the-scanf-function – gaoithe Oct 28 '22 at 15:42