Thread: Reading names in array separated by a comma

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    1

    Reading names in array separated by a comma

    Hello, I'm trying to read names separated by a comma using array.

    For example, the expected input would look like the following:
    Juila,Francisco
    Adams,Wong

    I know you can use getline function and set the delimiter to comma. So like ....

    getline(cin, lastName, ',');
    getline(cin, firstName);


    But the program only read the last name and ignore the firstname.


    Thank you for the help.




  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to do better than post 1 line of code and say that it doesn't work.

    Because the obvious thing seems to actually work.
    Code:
    $ cat bar.cpp
    #include <iostream>
    #include <string>
    using namespace std;
    int main ( ) {
      string lastName, firstName;
      getline(cin, lastName, ',');
      getline(cin, firstName);
      cout << firstName << "+" << lastName << endl;
    }
    $ cat bar.txt
    Juila,Francisco
    Adams,Wong
    $ g++ bar.cpp
    $ ./a.out < bar.txt
    Francisco+Juila
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in comma separated numbers from a data file
    By littlemslexii in forum C Programming
    Replies: 8
    Last Post: 04-22-2013, 09:41 AM
  2. homework help wanted: reading a comma seperated file
    By DHart07 in forum C++ Programming
    Replies: 8
    Last Post: 11-17-2010, 12:54 PM
  3. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  4. Reading a comma separated file
    By nwr in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 01:26 PM