Storing user inputs into array and splitting into elements.

This is a discussion on Storing user inputs into array and splitting into elements. within the C Programming forums, part of the General Programming Boards category; So the user enter the following doubles: 1.5 2.45 3.1 4.1 5.8 , how do i split these into 5 ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    50

    Storing user inputs into array and splitting into elements.

    So the user enter the following doubles: 1.5 2.45 3.1 4.1 5.8 , how do i split these into 5 elements and store them into an array?
    any help would be appreciated or links to helpful sites.


    Last edited by Salem; 10-05-2012 at 11:38 PM. Reason: snip link to copy/paste question

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    O_o

    One at a time?

    Soma

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,707
    With sscanf()?

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    yeah i used a for loop :
    for ( int i = 0 ; i < 10; i++ ) {
    scanf( "%d", &number );
    b[ i ] = number;

    but if i enter any numbers not in the order of 1 2 3 4 5 6 ... the loop just keeps going and does not stop.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,554
    > the loop just keeps going and does not stop.
    Perhaps it's because %d is for decimal integers, not doubles.

    Post your complete code, not 2 line snippets.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing out char elements in an array from user input
    By hencherz in forum C Programming
    Replies: 11
    Last Post: 02-23-2012, 07:48 AM
  2. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 04:28 AM
  3. accessing user entered array elements by index?
    By richdb in forum C Programming
    Replies: 10
    Last Post: 04-08-2006, 11:10 AM
  4. letting user input number of elements in array
    By iamthejake2000 in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2005, 12:35 PM
  5. Replies: 6
    Last Post: 04-12-2002, 08:33 AM

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