1

I need to open a file in this format

Dat Nguyen 77.7 88.8 99.9 11.1 22.2

Pat Nguyen 2 3 4 5 6 

I need to assign the first name of a line to a member array of structs and last name of the line to another member of the struct and each number of the line to a array of scores in the struct and each new line goes to the next index of the struct array, doing the same thing (Sorry if I worded that badly).

I have the first name and last name assignment going fine but when it gets to assigning the numbers to the struct members, it skips the first three numbers. What am I doing wrong?

Here is my code

void fileLoad(Gradebook *students, int &stuCount, int &assignments)
{
ifstream fin;
fin.open("Scores.txt");
if (!fin.is_open())
    cout << "Failed to open file.\n";

if (stuCount < 10)
{
    int n = 0;
    string tempLine;
    string line[10];
    while (!fin.eof())
    {
        getline(fin, tempLine);
        line[n] = tempLine;
        stringstream ss(tempLine);
        ss >> students[stuCount].fname >> students[stuCount].lname;
        assignments = 0;
        for (int i = 0; tempLine[i] != '\0'; i++)
        {
            ss >> students[stuCount].score[assignments];
            if (tempLine[i] == ' ')
                assignments++;

        }
        cout << line[n] << endl;
        assignments--;
        stuCount++;
        n++;
        cout << assignments << endl;
    }
}
else
    cout << "Already max students.\n";
}

Here is the output

Dat Nguyen 77.7 88.8 99.9 11.1 22.2


Pat Nguyen 2 3 4 5 6


1. Add a new student to the class
2. Assign grades for a new assignment
3. List one student, displaying all their grades and their course average
4. List all the scores for a chosen assignment
5. Display all grades currently contained in the gradebook
6. Save the gradebook to file
7. Exit the program
Enter choice: 3

Enter a student #: 1

Dat Nguyen

Assignment 1: 11.1

Assignment 2: 22.2

Assignment 3: -9.25596e+61

Assignment 4: -9.25596e+61

Assignment 5: -9.25596e+61

Assignment 6: -9.25596e+61

Average: -5.28912e+61
halfer
  • 18,701
  • 13
  • 79
  • 158
Dat Nguyen
  • 11
  • 1

1 Answers1

0

This bit of logic is suspect:

for (int i = 0; tempLine[i] != '\0'; i++)
{
    ss >> students[stuCount].score[assignments];
    if (tempLine[i] == ' ')
        assignments++;

}

this iterates through all of the characters in tempLine one by one and tries to read a whole word from ss. Think about that for a second: For every character, read a whole word. ss contains a copy of tempLine but other than that, they are separate entities. Reading ss in word sized blocks and tempLine in char sized blocks, words will run out long before the loop terminates. and since OP isn't testing the read of words for success, Crom only knows what's happening.

I think you want something more like:

while (assignments < MAX_ASSIGMENTS &&
       ss >> students[stuCount].score[assignments])
{ // read all assignments until array full or couldn't read an assignment score 
    assignments++;
}

MAX_ASSIGMENTS is a placeholder for whatever OP used to size the score array.

user4581301
  • 29,019
  • 5
  • 26
  • 45