Thread: scanning multiple inputs

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    scanning multiple inputs

    just started programming and encountered a beginner level loop problem today. this is what the input and output must look like

    INPUT

    4 3.0 2.5 6.0 4.5 [enter]
    5 6.0 2.5 3.4 3.0 5.6 [enter]
    2 4.0 4.6[enter]
    0[enter]

    OUTPUT
    4 4.1 4.3

    the first number in each row of inputs represent the number of values that should be read. for example on the first row, the first number is 4. hence the user enters 4 and then enters 4 values (3.0, 2.5, 6.0 and 4.5) after that. The same goes for 5 and 2. when the user enters 0 the program terminates and prints the output. The output represents the average of the values entered. for example, for the first row of 4 inputs, the average is 4.

    I could have used a simple "while" or "for" loop with a scanf statement inside if the user enters something like this.

    4 [enter]
    3.0[enter]
    2.5[enter]
    6.0[enter]
    4.5[enter]

    since its all supposed to be in the same line, is there anything about scanf that enables the user to key everything in the same line just as the problem requires?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by casey123
    is there anything about scanf that enables the user to key everything in the same line just as the problem requires?
    Since each unit of input is separated from the previous one by whitespace, you can treat this as if each unit of input was on a separate line because whitespace would be ignored when you use scanf in this way.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    Smile

    thanks, much appreciated

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. checking multiple inputs
    By killdragon in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2004, 03:26 PM
  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

Tags for this Thread