I am overloading my operator>> to work with my struct. I added some comments to my code to explain my error and the two types of solutions i have tried so far to worked best but still not all the way there.
I have textfile with several hundreds lines of this:
And i am adding these one by one to my vector that is a struct and it is working all good until the last name which is the city. This is because of the extra white spaces after it. The original name is " DYLTABRUK (several whitespaces here that i cant write hehe) "
And it is easy to remove the first two whitespaces and it also easy removing all white spaces by making it stop reading after first whitespace. However a few of the city names is like " BY KYRKBY " so it stops reading halfway and the name in the vector only shows as "BY". Check my code for more comments regarding this.
istream& operator>>(istream& in, person& p)
{
string TEMPinfo;
getline(in, TEMPinfo);
transform(TEMPinfo.begin(), TEMPinfo.end(), TEMPinfo.begin(), ::tolower);
p.name = TEMPinfo;
getline(in, TEMPinfo);
p.id = TEMPinfo;
address TEMPaddress;
getline(in, TEMPinfo, ',');
TEMPaddress.street = TEMPinfo;
string TEMPzip1;
string TEMPzip2;
in >> TEMPzip1;
in >> TEMPzip2;
string TEMPziptot = TEMPzip1 + TEMPzip2;
if (TEMPziptot.size() > 0)
{
TEMPaddress.zip = stoi(TEMPziptot);
}
//This part works really good. However the some of the citynames i add to
//TEMPaddress.city is two words. For example i have a city called "BY KYRKBY" and it
//only shows upp as "BY".
in >> TEMPinfo;
cout << TEMPinfo << "." << endl;
TEMPaddress.city = TEMPinfo;
//This code however show "BY KYRKBY" but all the white spaces behind city comes
//with it. So it is like "BY KYRKBY ".
getline (in >> ws, TEMPinfo);
cout << TEMPinfo << "." << endl; //
TEMPaddress.city = TEMPinfo;
return in;
}
I am really a beginner here so need som clarification, thanks.
EDIT:
So after trying it out it is clear to me that it should be maximum of 2chars there and if change it to 2chars for example s/ it says there is no overloaded function for getline.
That's a regex. '/' is to separate the fields
the first field 's' is the substitute command
the second field is the match '+' means one or more. So ' +' is one or more spaces '$' means end of line (not confuse it with line break)
So match all the withespaces at the end of the line.
the third field is the substitution. In this case it's empty, so the spaces will be removed.