Line 6. C++ supports const global variables. Safer than preprocessor macros.
Lines 23-24 could be:
1 2
|
while ( i < SIZE && getline( infile, line[i] ) )
{
|
Reasoning:
1. You don't want to read more than your line can hold.
2. The returned values of getline() and good() are effectively same in conditional.
Line 28 is not useful. std::cin has no place in this party.
1 2
|
std::string studentID[SIZE];
char grades[SIZE][SIZE];
|
Both could be 1-D string arrays, but you want to learn.
Lines 32-34. No.
1. The end condition of the loop is scary; first element of line that is an empty string.
2. If array has SIZE elements, then array+SIZE is one past the end.
3. std::cin still has no part in this party.
What you probably want is to iterate over the lines, except the first one.
( int row = 1; row < i; ++row )
Ok, now you have one line from inFile,
line[row]
. What do you want?
To split that line into studentID and grade.
More specifically, to store them to
studentID[row] and grades[row]
. Just remember that element 0 in each array is empty.
How to split? One could use the std::stringstream, or do as the chars do.
Loop over
line[row]
in phases. This is a nested loop within the outer iteration's body.
In first phase proceed only up to the first space. Append each character into string studentID[row]. std::string has operator+=(char).
Second phase copies the rest of characters into grades
grades[row][x] = line[row][col];
Note how col advances from 0 to line[row].size(). You don't want x to grow to SIZE though. Add a null '\0' to grades after the last written character.
Test output:
1 2 3 4
|
for ( int row = 1; row < i; ++row )
{
std::cout << studentID[row] << " got " << grades[row] << std::endl;
}
|