title: Average, Maximum and Minimum assignment for C++
body: <p>My issue lies in the minimum function toward the end and the program pulls from a fill of about 25 names with 5 grades each. The average and maximum are running smoothly but I can't seem to hammer out the minimum. I managed to get it to pull a few names but then the program crashes. Can anyone help me understand what I am overlooking here? Please and thank you.</p>
  
  <p>If you plug it in a compiler the min function is located between the 200-250 lines and a simple txt file is used to pull names and grades from.</p>
  
  <pre><code>// Headers
  #include &lt;iostream&gt;     // cout, cin
  #include &lt;cstdlib&gt;      // exit()
  #include &lt;string&gt;       // strings
  #include &lt;fstream&gt;      // file processing
  #include &lt;iomanip&gt;      // stream manipulation
  using namespace std;
  
  // Global variables
  const int MAX_STUDENTS = 25;    // We will not process more than 25 students even if the file contains more
  const int MAX_GRADES = 5;       // Each student has exactly 5 grades
  const string FILENAME = "NamesGrades.txt";  // The name of the file that you will read
  
  // Function declarations
  int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES], string fileName, int maxStudents);
  void displayAverages(string students[], int grades[][MAX_GRADES], int studentCount);
  void displayMax(string students[], int grades[][MAX_GRADES], int studentCount);
  void displayMin(string students[], int grades[][MAX_GRADES], int studentCount);
  string getLetterGrade(double grade);
  int getLongestNameLength(string students[], int studentCount);
  
  int main()
  {
      int studentCount = 0;                   // You need one to keep up with the actual number of students
      int grades[MAX_STUDENTS][MAX_GRADES];   // You need a two dimensional arry for the grades of the students
      string students[MAX_STUDENTS];          // You need and array of strings for the student names
      char choice;                            // You need a variable to hold the choice of the user for the menu
  
      // Get students and grades
      studentCount = loadStudentNamesGrades(students, grades, FILENAME, MAX_STUDENTS);
  
      // Loop until user says to quit (Do loop suggested)
      do
      {
          // present menu and get user's choice
          cout &lt;&lt; "\nGrade Report Program\n\n";
          cout &lt;&lt; "\t1. Display Average Grade\n";
          cout &lt;&lt; "\t2. Display Maximum Grade\n";
          cout &lt;&lt; "\t3. Display Minimum Grade\n";
          cout &lt;&lt; "\t4. Quit Program\n";
          cout &lt;&lt; "\nEnter your choice (1-4): ";
          cin &gt;&gt; choice;
  
          // Process the choice
          switch( choice )
          {
              case '1':       // Average
                  displayAverages(students, grades, studentCount);
                  break;
              case '2':       // Maximum
                  displayMax(students, grades, studentCount);
                  break;
              case '3':       // Minimum
                  displayMin(students, grades, studentCount);
                  break;
              case '4':       // Quit
                  break;
              default:
                  cout &lt;&lt; "Invalid option.\n\n";
  
          }
          if(choice != '4' )
          {
              cout &lt;&lt; endl;
              system("PAUSE");
              system("CLS");
          }
  
      }   while( choice != '4' );    
  
      cout &lt;&lt; endl;
  
      return 0;
  }
  
  /***********************************************************
  loadStudentNameGrades opens and read fileName. It will read in two strings, concatenate them, and then save
  to the students array. It then reads five integers and save each to the grades array. The function will return
  the actual number of student/grade combinations read
  PARAM:  students is an array of strings that can hold up ot maxStudents values
          grades is a two dimensional array for holding the grades of each student
          fileName is the name of the file that will be opened and read
          maxStudents is the maximum number of students that we will read from the file
  PRE:    students[] is large enough to contain up to maxStudents elements
          grades[] is large enough ot contain up to maxStudents elements
  POST:   students[] contains the names of up to maxStudents
          grades[][] contains the grades for up to maxStudents
          The number of student/grade combinations actually read from the file is returned. This value can range
          between 0 &lt;= numStudents &lt;= maxStudents
  NOTE:   students[] and grades[] are meant to be parralel arrays. students[0] and grades[0] are the same student     
  ************************************************************/
  // Check 16:15
  int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES], string fileName, int maxStudents)
  {
      ifstream inFile;    // input file stream
      string studentsName, letterGrade;  // Name of the student and grade character (char)
      int numStudents = 0; // number of students actually read
  
  
      // Open the file
      inFile.open(fileName.c_str());
      if( inFile.fail())
      {
          cout &lt;&lt; "Could not open file" &lt;&lt; endl;
          system("PAUSE");
          exit(1);
      }
  
      for( int i = 0; i &lt; maxStudents &amp;&amp; (inFile &gt;&gt; studentsName &gt;&gt; letterGrade); i++, numStudents++)
  
      {
          for( int j = 0; j &lt; MAX_GRADES; j++ )
          {
              inFile &gt;&gt; grades[i][j];
          }
          students[i] = studentsName + " " + letterGrade;
      }
  
      inFile.close();
  
      return numStudents;
  }
  
  /***********************************************************
  displayAverages calculates the average of each student and displays the
  students name, average, and letter grade of the average in a table
  PARAM:  students[] is an array of strings that contains the names of studentCount students
          grades[] is an array of integers that contains the grades of studentCount students
          studentCount contains the value of the number of elements in the students[] and grades[] arrays
  PRE:    students[] and grades[] contain values for studentCount elements
  POST:   table of student names, averages, and letter grades is displayed
  ************************************************************/
  // Check 16:15
  void displayAverages(string students[], int grades[][MAX_GRADES], int studentCount)
  {
      double average;     // Average grades of students
      int total;          // total of all grades (accumulator)
      int maxLength = getLongestNameLength(students,studentCount);
  
      cout &lt;&lt; setprecision(1) &lt;&lt; fixed &lt;&lt; showpoint;
      // Setup table header
      cout &lt;&lt; "\n\nGrade Averages\n";
      cout &lt;&lt; setw(maxLength + 1) &lt;&lt; left &lt;&lt; "Name" &lt;&lt; setw(4) &lt;&lt; right &lt;&lt; "Average" &lt;&lt; setw(6) &lt;&lt; "Grade" &lt;&lt; endl;
      for( int i = 0; i &lt; studentCount; i++ )
      {
          cout &lt;&lt; setw(maxLength + 1) &lt;&lt; left &lt;&lt; students[i];
          total = 0;
  
  
          for( int j = 0; j &lt; MAX_GRADES; j++ )
          {
              total += grades [i][j];
          }
          average = (double)total / MAX_GRADES;
          cout &lt;&lt; setw(7) &lt;&lt; right &lt;&lt; average &lt;&lt; setw(6) &lt;&lt; getLetterGrade(average) &lt;&lt; endl;
      }       
  }
  
  /***********************************************************
  displayMax calculates the maximum grade of each student and displays the
  students name, maximum grade, and letter grade of the maximum grade in a table
  PARAM:  students[] is an array of strings that contains the names of studentCount students
          grades[] is an array of integers that contains the grades of studentCount students
          studentCount contains the value of the number of elements in the students[] and grades[] arrays
  PRE:    students[] and grades[] contain values for studentCount elements
  POST:   table of student names, maximum grades, and letter grades is displayed
  ************************************************************/
  // Check 16:15
  void displayMax(string students[], int grades[][MAX_GRADES], int studentCount)
  {
      int maxGrade;
      int maxLength = getLongestNameLength(students,studentCount);
  
      cout &lt;&lt; "\n\nGrade Maximums\n";
      cout &lt;&lt; setw(maxLength + 1) &lt;&lt; left &lt;&lt; "Student" &lt;&lt; setw(4) &lt;&lt; right &lt;&lt; "Max" &lt;&lt; setw(6) &lt;&lt; "Grade" &lt;&lt; endl;
  
      for( int i = 0; i &lt; studentCount; i++ )
      {
          cout &lt;&lt; setw(maxLength + 1) &lt;&lt; left &lt;&lt; students[i];
          maxGrade = 0;   
          for( int j = 0; j &lt; MAX_GRADES; j++ )
          {
              if(maxGrade &lt; grades [i][j]) maxGrade = grades [i][j];
          }
          cout &lt;&lt; setw(4) &lt;&lt; right &lt;&lt; maxGrade &lt;&lt; setw(6) &lt;&lt; getLetterGrade(maxGrade) &lt;&lt; endl;
      }
  }
  
  
  /***********************************************************
  displayMin calculates the minimum grade of each student and displays the
  students name, minimum grade, and letter grade of the minimum grade in a table
  PARAM:  students[] is an array of strings that contains the names of studentCount students
          grades[] is an array of integers that contains the grades of studentCount students
          studentCount contains the value of the number of elements in the students[] and grades[] arrays
  PRE:    students[] and grades[] contain values for studentCount elements
  POST:   table of student names, minimum grades, and letter grades is displayed
  ************************************************************/
  /* Notes:
  Program seems to pull 2 names and freeze on the third. Unsure as to why?
  Assigns Leslie Carter 36C and code seems to break down afterwords. 
  Issue is undetected any where else and seems to be isolated in this function.
  */
  
  void displayMin(string students[], int grades[][MAX_GRADES], int studentCount)
  {
      int minGrade;
      int maxLength = getLongestNameLength(students,studentCount);
  
      cout &lt;&lt; "\n\nGrade Minimums\n";
      cout &lt;&lt; setw(maxLength + 1) &lt;&lt; left &lt;&lt; "Student" &lt;&lt; setw(4) &lt;&lt; right &lt;&lt; "Min" &lt;&lt; setw(6) &lt;&lt; "Grade" &lt;&lt; endl;
  
      for( int i = 0; i &lt; studentCount; i++ )     // &gt; is null result, &lt; is result
      {
          cout &lt;&lt; setw(maxLength + 1) &lt;&lt; left &lt;&lt; students[i];
          minGrade = 101;                                                 // Produced a result in the correct direction
          for( int j = 0; j &lt; MAX_GRADES; j++ )
          {
              if(minGrade &gt; grades [i][j]) minGrade = grades [i][j];      // Produced a result in the correct direction
          }
          cout &lt;&lt; setw(4) &lt;&lt; right &lt;&lt; minGrade &lt;&lt; setw(6) &lt;&lt; getLetterGrade(minGrade) &lt;&lt; endl;
      }
  }
  
  /***********************************************************
  getLetterGrade converts a numerical grade to a letter grade
  PARAM:  grade is the numerical grade to convert. Expected range is 0 &lt;= grade &lt;= 100
  PRE:    grade contains a value in the correct range
  POST:   The corresponding letter grade of the numerical grade is returned
  ************************************************************/
  // Check 16:15
  string getLetterGrade(double grade)
  {
      if(grade &gt; 90 )
          return "A";
      else if( grade &gt; 80)
          return "B";
      else if( grade &gt; 70)
          return "C";
      else if( grade &gt; 60)
          return "D";
      else if( grade &gt; 50)
          return "F";
  }
  
  /***********************************************************
  getLongestNameLength returns the length of the longest string from a list of strings
  PARAM:  students[] is an array of strings that contains the name of students
          studentCount is the size of the students[] array
  PRE:    students[] contains studentCount names
  POST:   The length of the longest string in students[] is returned
  ************************************************************/
  // Check 16:15
  int getLongestNameLength(string students[], int studentCount)
  {
      int maxLength = 0;
      for( int i = 0; i &lt; studentCount; i++)
      {
          if( students[i].length() &gt; maxLength ) maxLength = students[i].length();
      }
      return maxLength;
  }
  </code></pre>