Removing whitespaces with getline.

Hey all!

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:

1
2
3
Olliver Lindberg
8602024898
Sandviken 76, 710 27  DYLTABRUK


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.

Thanks for the help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
}
Last edited on
's/ +$//'

May also search for a non-whitespace character from the end of the string, then delete from there.
Should i add it to line 38?

 
getline (in >> ws, TEMPinfo, 's/ +$//');


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.
Last edited on
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.


In c++11 it would be like
1
2
3
4
5
#include <regex>
//...
getline(in >> ws, TEMPinfo);
std::regex spaces_at_end (" +$");
TEMPaddress.city = std::regex_replace(TEMPinfo, spaces_at_end, "");


As an alternative, http://www.cplusplus.com/reference/string/string/find_last_not_of/
Thank you very much. It worked perfectly!
Topic archived. No new replies allowed.