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++;
}
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.