array of strings into array of integers issue

why does the array score doesnt get only the integers from the string array line?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
        string line[30];
	int score[30];
	ifstream myfile ("example.txt");
	if (!myfile.is_open())
	cout<<"Cant open the specified file"<<endl;
	int a=0;

	while(!myfile.eof())
  {
    getline(myfile,line[a],'(');
	int i=0;
    stringstream(line[a])>>score[i];
	i++;
  }


thx for the help..
Last edited on
What does your input file look like?
Try to indent your code properly when you paste it here, that would help us help you. The variable "a" doesn't seem to ever change, not sure that's what you want.

A stringstream is great to convert strings which are numbers to a number type, but it won't do magic. If the line you read has anything which isn't considered a number, it might fail. The decimal symbol might change depending on OS and IDE, make sure that's not the problem.

1
2
getline(myfile,line[a],'('); // read until the ( character is found
stringstream(line[a])>>score[i]; // store what's before the ( in score[i] 


If your file looks like this: 1000 (John Smith)
Then the stringstream will contain "1000 " including the final space. That could be it, I'm not sure if spaces are ignored when converting the string to an int.
Last edited on
the input file look like this:

Register Teams
Hapoel Ramat-Gan
Irroni Ashdod
Galil Elion
Maccabbi TA
;
> Game 1, Oct. 20, 2013
Maccabbi TA - Irroni Ashdod 98-103 (51-50)
GalilElion - Hapoel Ramat-Gan 91-88 (51-50)
;
> Game 2, Oct. 27, 2013
Maccabbi TA - Galil Elion 90-83 (63-47)
Irroni Ashdod - Hapoel Ramat-Gan 90-98 (63-51)
;
Topic archived. No new replies allowed.