I am working on a program that reads students name and test score from a file. The program should assign the appropriate letter grade, then output the name score, and grade. It should also determine the highest test score. I do not see where my errors are. Please help!

//
// Author: Lauren Settle
//
// Program: Student Test Scores
//
// This program prompts the user to input the names of students followed by the corresponding test scores.
// The program will then read and output the student name, test score, and the letter grade based on the score.
// The program will also output the highest test score along with the student who recieved it.
//
//

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;


struct studentType
{
       string studentFName; //students first name
       string studentLName; //students last name
       int testScore; //students test score
       char grade; //corresponding letter grade
};
void openFile (fstream& inFile);
void getData (fstream& inFile, studentType studentData[], int listSize);
void assignGrade (studentType studentData[], int listSize);
double highScore (studentType studentData[], int listSize); 
void printResults (studentType studentData[], int m);

int main()
{
    
    fstream inFile;
    studentType studentData[20];
    int listSize = 20;
    int m;
    
    openFile (inFile);
    getData (inFile, studentData, listSize);
    assignGrade (studentData, listSize);
    highScore (studentData, listSize);
    printResults (studentData, m);
    
    /* Scaffolding code for testing purposes */ 
cin.ignore(256, '\n');
cout << "Press ENTER to continue..." << endl;
cin.get();
/* End Scaffolding */ 
    return 0;
}

void openFile (fstream& inFile)
{
     inFile.open ("STUDENTFILE.TXT", ios::in);
     
}

void getData (fstream& inFile, studentType studentData[], int listSize)
{
     
     int j;

     while (!inFile.eof())
     {
      for (j = 0; j < listSize; j++)
      {
       inFile >> studentData[j].studentFName
              >> studentData[j].studentLName
              >> studentData[j].testScore;
       }
      }
}

void assignGrade (studentType studentData[], int listSize)
{
     int j;
     
     for (j = 0; j < listSize; j++)
     {
         if (studentData[j].testScore >= 90)
        studentData[j].grade = 'A';
     else if (studentData[j].testScore >= 80)
        studentData[j].grade = 'B';
     else if (studentData[j].testScore >= 70)
        studentData[j].grade = 'C';
     else if (studentData[j].testScore >= 60)
        studentData[j].grade = 'D';
     else 
        studentData[j].grade = 'F';
     }
}

double highScore (studentType studentData[], int listSize) 
{
     int i;
     double maxScore = 0;
     int m;     
     
     for (i = 1; i < listSize; i++)
         if (studentData[i].testScore >= maxScore)
            {
               maxScore = studentData[i].testScore;
               m = i;
            }
     return m;
}

void printResults (studentType studentData[], int m)
{
     int j;
     
     cout << left;
     cout << studentData[j].studentLName << " , ";
     cout << studentData[j].studentFName;
     cout << right;
     cout << setw(15) << studentData[j].testScore;
     cout << setw(20) << studentData[j].grade;
     cout << endl << endl << endl << endl;
     
     cout << left;
     cout << " Highest Test Score " << endl;
     cout << studentData[m].testScore;
     cout << setw(10) << studentData[m].studentFName << " " << studentData[m].studentLName;
     cout << endl;
}

What exactly is the problem? It compiles fine. Right off the bat, though, I ran it without the input file and it doesn't exit, so you need to change it so it checks for a bad stream before attempting to retrieve data.

This article has been dead for over six months. Start a new discussion instead.