File input: problem with converting string to int

I am having issues reading a string from a text file; my value gets read in properly, but when I try to convert my string value to a int value, the value changes to something else. Here is what the input text file looks like:

"Q1,2,8,5,1,10,5,9,9,3,5,6,6,2,8,2,2,6,3,8,7,2,5,3,4,3,3,2,7,9,6,8,7,2,9,10,3"


Basically I want to read in all 36 numbers (one at a time) after the label "Q1" and put them into an array for later computations.

I am using a function to do all this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
	string assessment, read;
	int count=0, value;

	getline(fin, read , ',' ); // this works fine: 'read' now equals 'Q1' as it should.

	while( count != 36 ){

	getline(fin, assessment , ',' ); //this works fine: 'assessment' now equals '2' as it should.
	fin >> assessment;               // assessment still = '2' at this point
		istringstream iss(assessment);  //problem!!!!!!!!!!!!!!
//assessment now equals '8,5,1,10,5,9,9,3,5,6,6,2,8,2,2,6,3,8,7,2,5,3,4,3,3,2,7,9,6,8,7,2,9,10,3' , it should still be '2'... what happened?!?
		iss >> value;

	scores[count] = value;  

	count++;
	}
	 
	return read;
}


Why does assessment magically decide that it is no longer '2' and instead it becomes the 35 other numbers?

If anyone can tell me why this is happening and what I can do to fix it, I would very much appreciate it =].
Last edited on
Er, the problem is right after you call getline() you overwrite whatever happened by doing fin>>assessment; Just remove that line and it should be fine.
Topic archived. No new replies allowed.