scanning multiple inputs

This is a discussion on scanning multiple inputs within the C Programming forums, part of the General Programming Boards category; just started programming and encountered a beginner level loop problem today. this is what the input and output must look ...

  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
    21,593
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01:57 PM
  4. Multiple inputs with cin
    By DJPG5 in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2003, 03: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21