Reading names in array separated by a comma

This is a discussion on Reading names in array separated by a comma within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm trying to read names separated by a comma using array. For example, the expected input would look like ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    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. 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, 11:54 AM
  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

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