Thread: Multiple inputs at one line?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    35

    Multiple inputs at one line?

    My code requires the user to input many values and for every input, it goes to a new line. How can I make all the inputs in one line? Here is my part of code;
    Code:
    printf("\nEnter the durations of jobs :\n\n");                  
      for(i=0; i<number; i++)                                            
        scanf("%d", &dur[i]);

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    scanf doesn't read whitespace characters. So as long as you separate your inputs by a "space" in the same line, what you wrote should work.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    You can get more than one value with a single scanf statement.

    Code:
    int x[3];
    printf("Enter three integers, separated by spaces: ");
    scanf("%d %d %d", &x[0], &x[1], &x[2]);
    printf("You entered %d, %d, and %d.\n", x[0], x[1], x[2]);
    That should work for you.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by Babkockdood View Post
    You can get more than one value with a single scanf statement.

    Code:
    int x[3];
    printf("Enter three integers, separated by spaces: ");
    scanf("%d %d %d", &x[0], &x[1], &x[2]);
    printf("You entered %d, %d, and %d.\n", x[0], x[1], x[2]);
    That should work for you.
    Yes, that would work for me but can I determine the amount of scanf inputs at one line depending on the user's input, such as not restricting by 3 numbers, but up to the number that user enters? I considered a for loop but I couldn't make it.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgets to get the whole line, strtok to split it into segments, sscanf to read whatever it is out of each segment.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple inputs on one line.
    By rekluse in forum C Programming
    Replies: 4
    Last Post: 04-21-2008, 06:53 PM
  2. How do i ouput multiple inputs?
    By vodka9er in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2007, 09:45 AM
  3. multiple inputs of multiple types
    By frontz in forum C Programming
    Replies: 8
    Last Post: 01-19-2004, 02:57 PM
  4. Multiple inputs with cin
    By DJPG5 in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2003, 04:10 PM
  5. Getting multiple inputs using cin
    By edk in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2001, 02:34 PM