How to read a line and store it into an array?

This is a discussion on How to read a line and store it into an array? within the C Programming forums, part of the General Programming Boards category; Hi, I was just wondering if anyone can help me with this problem? For some reason it won't allow me ...

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

    How to read a line and store it into an array?

    Hi,

    I was just wondering if anyone can help me with this problem? For some reason it won't allow me to store the input from the user into a single element of the array for example:

    > Please Enter Your Full Name:
    David Jones

    >Please Enter Your Phone Number:
    > Please Enter Your Credit Card Number:

    > You Have entered:
    Name: David
    Phone Number: Jones
    Credit Card Number: 3343434343


    --------

    See the problem? Once it hits the space it immediately assigns the last name input to the element of the array...

    I know you can simply do: scanf("%s %s", name[1], name[2]);

    although my code is a bit complicated where I'm using both arrays and structures which is therefore crucial to just read a single line and store it into the array.

    I also tried concatenation although I need the space in between the first and last name...

    Any help will be great..

    Oh and heres a snippet of my code where the problem lies:

    Code:
    printf("Enter the (First and Last) name for this record:\n");
                                scanf("%s", temp);
                                strcpy(record[reccount].name, temp);
    // for some reason it keeps seperating the file once you hit space.
                                printf("Enter the phone number for this record:\n");
                                scanf("%s", temp);
                                strcpy(record[reccount].phoneNo, temp);

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Once it hits the space it immediately
    That's how it works

    Please provide complete (compilable) code.

    Without seeing how temp and record and name and .... are defined, most likely you will get little help.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Well temp is just a regular char array i.e. char temp[20] in order to store the string which will then be copied to the structure array called record (which includes the arrays name and phone number).

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use fgets.


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

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    printf("Enter the (First and Last) name for this record:\n");
    scanf("%[^\n]", record.[reccount].name);

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    fgets(str, sizeof(str), stdin);

    That's how you do it. If it stores a newline, use str[strlen(str) -1] = '\0'; to remove it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read text file and store it in an array
    By look2hook in forum C Programming
    Replies: 2
    Last Post: 12-03-2010, 10:47 PM
  2. read and store text file as an array
    By abotaha in forum C++ Programming
    Replies: 1
    Last Post: 08-02-2010, 08:57 PM
  3. what's a good way to read each line of a file into an array
    By help_seed in forum C++ Programming
    Replies: 6
    Last Post: 03-25-2010, 12:45 PM
  4. Read Each Line of File as Array Record
    By Leo2010 in forum C Programming
    Replies: 3
    Last Post: 02-23-2010, 09:13 AM
  5. Read first line of a file and store in a variable
    By Saeid87 in forum C Programming
    Replies: 5
    Last Post: 06-19-2009, 11:27 AM

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