stoi is giving me "abort has been called()" error.

So i have been learning how to overload>> after making my own structs. So i am currently trying to add info from a text file into my struct. However one of the structs is in int format but the numbers i get from the textfile is in string so i am trying to convert them to int using stoi function.

TEMPziptot looks like it is all numbers but it also looks like there might be a last line that is empty. Maybe this is making it crash? It crash in the beginning of the program and i cant run my program at all.

if i remove the code on line 26 it works just fine but then i cant add the numbers to my struct since it need to int format and not string format.

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
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; //Adderar string(zip1) och string(zip2).

	cout << TEMPziptot << endl;

	TEMPaddress.zip = stoi(TEMPziptot); //Konverterar från string till int.

	getline(in, TEMPinfo);
	TEMPaddress.city = TEMPinfo;

	return in;
}
Last edited on
If TEMPziptot is empty, don't call stoi but set the zip to some known bad marker value.

For example,
1
2
3
4
5
6
7
8
if (TEMPziptot.size() > 0)
{
  TEMPaddress.zip = stoi(TEMPziptot);
}
else
{
  TEMPaddress.zip = -1;
}


There could be other things that go wrong, that you should think about. Could the string ever contain letters, for example?
Last edited on
Thanks, that worked perfect for me. However i decided to no use else her since i dont need to return a bad marker value, it is just better for the empty line to "disappear".
Topic archived. No new replies allowed.