fstream and getline

so im reading from a .txt and am having trouble figuring out how do i make it so that fin >> x or getline(fin,x) would read in certain part of the text

text example
1
2
3
John Smith
123 Will St.
Los Angeles, California

current code problem
1
2
3
4
5
getline(fin,m_name);
getline(fin,m_address);
fin >> m_city;
fin.ignore(' ',',');
getline(fin,m_state);


im currently stuck at the city part, where if the city has a space in between, i cant seem to get it to read in the space.

NAME: John Smith
ADDRESS:123 Will St.
CITY:Los //missing the Angeles
STATE:California


but if the city doesnt have a space in between it would work,
NAME: Mary Green
ADDRESS:3245 Weather St.
CITY:Hollywood
STATE:California


another question that i have is that if i want to sort the .txt in order by first name, do i have the program sort everything in the text, or do i extract it to a string array and sort it there?
It might be easier to read in the entire line and then split it up. You know that City and State are separated by a comma, so it should be rather easy.

I'm not really good with strings, but this seems like an easy way to do it:
http://www.cplusplus.com/reference/string/string/find/
-> 'find' the position of the comma, then split the string into two substrings based on the returned position.
The getline() function allows you to terminate on something other than the newline.

1
2
3
4
5
getline(fin,m_name);
getline(fin,m_address);
getline(fin,m_city,',');
fin>>ws; // Skip any whitespace after the comma
getline(fin,m_state);

Good luck!
Topic archived. No new replies allowed.