Hi
I have a question how can you store data.

for example i have a txt document stored this data
Jones Jr, brown K.
johnson, James S.
Smith, Mark J.

and i want to divide it to last name and first middle initial
like i want to store it to
string Last_name, First_M_Name;
ifstream ins;

is their a command to copy it to store it to Last_Name and First_M_Name

because i did just
ins>>Lastname>>First_M_Name; \\ i have conflicts when a last name has a Jr.
and if i did getline it will copy the whole name.

so my question is is their a way to copy it in 2 different strings?

you can use getline() with the last argument being the comma instead of the default space. getline(ins,Lastname,',');

what i would do in this situation is read in the entire line using getline(), and then break the line down and store it as you desire.

here is a possible pseudocode:

1) read in the entire line
2) based on your text file format, we can assume the first word will always be a last name, store the first word into string last_name.
3) the second word will need to be tested in order to identify it as a possible suffix (jr. II, sr. etc.). pass the second word into a bool is_suffix(string word) function, if this function returns true, append the word to string last_name else store the word in string first_name.
4) assume any remaining data to be the middle name or initial. store it accordingly.

probably the best way to perform the string parsing is to use a <stringstream> object:

#include <stringstream>

stringstream ss;
string line,
       first_name[69],
       last_name[69],
       middle[69],
       temp;
int i=0;

while(getline(infile, line))
{
     ss << line;
     last_name[i] << ss;
     temp << ss;
     if(is_suffix(temp))
     {
          last_name[i] += ' ';
          last_name[i] += temp;
     }
     else
     {
          first_name[i] = temp;
     }
     middle[i] << ss;
     i++;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.