Thread: Question On Putting User Input into Arrays

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    62

    Question On Putting User Input into Arrays

    So, into the Arrays section of the C Primer Plus, Fifth Edition book i'v run into a snag. While the book "briefly" (And I mean VERY briefly) touches on Arrays, it makes no real mention of how you would actually take user input and place it into each element of an Array.

    Say I wanted to take 10 integers from the user and have it store them into

    int num[10];

    How would I go about doing this? I'v tried using a FOR loop, but most of the time all it's doing is outputting::

    1, 2, 4, 6, 8, 10

    or

    1, 3, 5, 7, 9

    And those are with the user values being 1 2 3 4 5 6 7 8 9 and 10. Anyone help me with this and explain what exactly I need to do to get an array to take user input.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You'll have to show us how you are getting user input and how you are assigning it to the array elements.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    I managed to somehow figure it out.
    ^^

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by NinjaFish View Post
    I managed to somehow figure it out.
    ^^
    Next time post your code with your question, please.

    scanf() ??

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    That's what I figured. Missing ampersand, user entering spaces or new-lines and scanf not set up to skip across those. The usual.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  2. User determined input question
    By lyoncourt in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 06:10 PM
  3. String input reliability question
    By WinterMute in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2007, 11:58 PM
  4. Testing user input question?
    By Hoser83 in forum C Programming
    Replies: 18
    Last Post: 02-15-2006, 01:18 PM
  5. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM