Reading Numbers From File: String Of Space-delimited Numbers

In C++, if getline reads a line containing space-delimited numbers (e.g., 85 95 56 12), how would I pick out each number to stick into an array? Or should I use a different access method than getline?

I'm reading from a file in which the first line is string, second is integer, third is a row of numbers with spaces between them. I need to put the row of numbers into a 2D array, and everything else into 1D arrays.

I've tried several approaches. I am currently attempting to use stringstream to parse a line of numbers.


int main()
{
//***********************************
//Variable Declarations
//***********************************

const int NUM_STUDENTS = 5;
const int NUM_SCORES = 4;
double studentScores[NUM_STUDENTS][NUM_SCORES];
string name[NUM_STUDENTS];
int ID[NUM_STUDENTS];

int row = 0, record = 0, line = 0;
double testScores = 0.0;

string input = "Student Data.txt";
fstream fin;
stringstream ss;

fin.open( input.c_str() );

if (fin)
{
getline(fin, input);
while (fin)
{
for( int record = 0; record < NUM_STUDENTS; record++ )
{
for (line = 0; line < 2; line++)
{
if (line == 0)
{
name[line] = input;
// cout << "Line " << line << "\n\n";
cout << name[line] << endl;
}
if (line == 1)
{
cout << "Line " << line << "\n\n";
ID[line] = atoi(input.c_str());
stringstream ss(input);
cout << ID[line] << endl;
}
if (line == 2)
{
cout << "Line " << line << "\n\n";
ss << input;
for( int col = 0; col < NUM_SCORES; col++ )
{
stringstream ss(input);
ss >> testScores;
studentScores[row][col] = testScores;
ss << "";

}
}
}
}

// cout << input << endl;
getline(fin, input);
}
fin.close();
}
else
{
cout << "ERROR: Cannot open file. \n";
}
}


I'm supposed to be able to use ss.getline, but when I attempt it, my compiler (MS Studio 10) gives an error about overloading the term.
Last edited on
Topic archived. No new replies allowed.