Thread: String to Array

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    String to Array

    One thing I am wondering is how to I put a string of info into a multidimensional array?

    Let me specify the array:

    Code:
     char name[10][80];
    Now in one of my functions I need to input between 1 to 10 different names into that array.

    I have tried three different ways, each being unsuccessful. Let me show you the ways I have tried. The first being the only one where I have got some result, but I need it to include spaces.

    Code:
    for (i = 0; i < 10; i++)
    {
        cin >> name[i];
    ...
    }
    Obviously that would only input everything up until a whitespace character.

    I tried the following two methods which I though would've worked, but didn't...

    Code:
    #include <cstring>
    ...
    
    for (i = 0; i < 10; i++)
    {
        gets(name[i]);
    ...
    }
    Code:
    for (i = 0; i < 10; i++)
    {
        cin.getline(name[i], 80, '\n');
    ...
    }

    Here is a snippet of my code and it's output with effort 2.

    Code:
    void enter()
    {
        int i;
        
        for (i = 0; i < 10; i++)
        {
            cout << "\n\nEnter employee's name: ";
            gets(name[i]);
            cout << "Enter phone number: ";
            gets(phone[i]);
            cout << "Enter number of hours worked: ";
            cin >> hours[i];
            cout << "Enter wage: ";
            cin >> wage[i];
        }
    }
    Code:
    Enter employee's name: Enter phone number:
    One more question, what's the difference between gets() and cin.getline() anyhow? Is there a better one to use?

    I'm probably just doing something extremely stupid, but I'm still new at this so take it easy on me

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You need to eat the newline character after inputting the wage. So:
    Code:
            cout << "\n\nEnter employee's name: ";
            cin.getline(name[i], 80, '\n');
            cout << "Enter phone number: ";
            cin.getline(phone[i], 80, '\n');
            cout << "Enter number of hours worked: ";
            cin >> hours[i];
            cout << "Enter wage: ";
            cin >> wage[i];
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
    >One more question, what's the difference between gets() and cin.getline() anyhow? Is there a better one to use?

    If you're coding C++, use cin.getline(). I believe gets() also leaves the newline character at the end of the string. If you are using C, fgets() is preferred over gets().

  3. #3
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Yes, I am coding in C++. The book I'm reading uses gets() and I read an online tut which uses cin.getline(). I was just wondering.

    Code:
        for (i = 0; i < 1; i++)
        {
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "\n\nEnter employee's name: ";
            cin.getline(name[i], 80, '\n');
            cout << "Enter phone number: ";
            cin.getline(phone[i], 80, '\n');
            cout << "Enter number of hours worked: ";
            cin >> hours[i];
            cout << "Enter wage: ";
            cin >> wage[i];
        }
    }
    I do that and it works correctly. Thanks swoopy. Although I don't really understand what the cin.ignore() thing does. Could you please explain?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Although I don't really understand what the cin.ignore() thing does. Could you please explain?

    When you do:
    cin >> wage[i];
    There is a newline character left in the buffer from when you hit the <ENTER> key. Then when you loop the:
    cin.getline(name[i], 80, '\n');
    reads this newline character before you have a chance to enter the next name.

    The cin.ignore() will read characters either until it sees a newline, or until the input buffer is filled. In this case you could probably just do:
    cin.ignore();
    Which only ignores one character, but the other is more general.

    One correction: I said I thought gets() appends a newline character to the string. Actually it's fgets() which appends the newline character, not gets().
    Last edited by swoopy; 12-10-2003 at 01:54 PM.

  5. #5
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Cool stuff. Thanks for the assistance swoopy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM