Arrays taking in different numbers

When writing this code i am finding that when going from the getline to the for loop the numbers of my age and transferFee variable are getting changed somehow. The numbers are no where even close to original numbers.

Along with these 2 variable the other variables only print out 1 letter and it is completely stumping me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  string      name,team,age, nation, pos, from, to, transferFee;
  int         aAge[30];
		getline(data, name);
		getline(data, team);
		getline(data, age);
		getline(data, nation);
		getline(data, pos);
		getline(data, from);
		getline(data, to);
		getline(data, transferFee);
	
		for (int i = 0; i < 30; ++i)
		{
			aAge[i] = age.at(i);

		}
there seems to be two problems with your code, first of all
1. you are getting one set of data from your source(which i believe is a txt file, i could be wrong)
2. you do not know how to use std::at() function.

post a little ,more code, maybe i can come up with a solution for you.
correct, i am pulling from a csv file.
where am i messing up with the .at?

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
GetInfo()
{
	ifstream  data("playerdata.csv");
	if (!data)
	{
		cout << "Could not find file, please re-enter" << endl;
		return false;
	}
	else
	{
	
		string      name,team,age, nation, pos, from, to, transferFee;
		int			aAge[30];
		getline(data, name);
		getline(data, team);
		getline(data, age);
		getline(data, nation);
		getline(data, pos);
		getline(data, from);
		getline(data, to);
		getline(data, transferFee);
	
		for (int i = 0; i < 30; ++i)
		{
			aAge[i] = age.at(i);

		}

		return true;
	}
	return false;
}


edit: http://justpaste.it/playerdata
Last edited on
ok i am coming up with a solution, i just need to get to my pc. and the at() function i believe finds the location in a string. and could you put some sample data from your csv in http://justpaste.it/ for me to see
Last edited on
ok i dont have your csv file but i went ahead and assumed you have all the ages in a single line and here is the code for that.
also csv files are comma separated that's the reason i used getline and stringstream to parse it.


1
2
3
4
5
6
7
8
9
10
11
12
13
// replace this with your for loop
	istringstream s(age);
		for (int i = 0; i < 30 ; ++i)
		{	
			string ageToPutIn;

			getline(s, ageToPutIn, ',');
// the stoi function is in c++ 11, so if you are working with
//an outdated compiler, might not work
			aAge[i] = stoi(ageToPutIn);
		}


Do Not forget to include sstream
if it doesnt work let me know
Last edited on
Wow that worked perfectly, thank you so much!
Topic archived. No new replies allowed.