Victor Martins Victor Martins - 1 month ago 10
C++ Question

How can I output the student name before a desired value?

Ok, Guys, I'm very confused as to how I can create this program. I have a good piece of it written however I have hit a standstill in which I do not know I can make this happen. So is this the following I assignment I have to do.

Assignment:
Download the file gradeBook.txt available through D2l. The file contains an unknown number of
students.

this is whats in the text file:

Melanie
84
Danielle Marie
90
Nicolas Raul
87
Michael
67
Joshua
46
Alexis Michelle
90
Jared M.
55
Andres Gabriel
78
Pierre Louis
80
Charles
60
Cin Lian
95
Carlos Manuel
81

Information for each student is displayed in two lines:

 The first line indicates the name of the student – whitespaces are allowed.

 The second line shows the student’s grade in a class.
Write a program that prompts the user for the name of the file and displays a nicely formatted
report showing:

a. Student with the highest grade in the class – Name and grade

b. Student with the lowest grade in the class – Name and grade

c. Average grade in the class

here's what I have so far:

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

using namespace std;

int main() {

fstream gFile;
int choice, grades;
string gb,students;
do {
cout << "Student Grade Book Info Program....\n";
cout << "\tPlease Select an Option: (1 or 2) \n" << endl
<< "\t1. Review Grades"
<< "\n\t2. Quit"
<< "\n\tChoose: ";
cin >> choice;

switch (choice) {

case 1:
cout << "\n\tPlease Enter the Name of the File you wish to View the Grades for: " << endl;
cout << "\n\tAvailable Grade Books: gradeBook\n" <<
"\tType in the Grade Book you would like to view. ";
cin.ignore();
getline(cin, gb);

if (gb == "gradeBook") {

gFile.open("gradeBook.txt", ios::in);

if (gFile) {

cout << "\t" << setw(20) << left << "Student"
<< setw(30) << left << "Grade";

int counter = 0;
int highest = 0;
while (gFile >> students) {

getline(gFile, students);
gFile >> grades;

if (grades >= highest) {
highest = grades;
}
counter++;
}
cout << highest;
cout << endl;
}
else {
cout << "\tError... Could open gradeBook.txt ";
return 5;
}

}
else {
cout << "\n\tError there is no such entry in the system.\n" << endl;

}

case 2:
break;
}

} while (choice != 2);


return 0;
}


I need to display the highest grade which I was able obtain and display it, but how can I display the name thats before the grade value in the gradeBook.txt file?

Please help me anyone? I'm so confused???

Answer

You don't need to store the information in your program.

Hint: You only need to store the following:

  • Highest grade value
  • Student name with highest grade value
  • Lowest grade value
  • Student name with lowest grade value
  • Sum of grades (used by average)
  • Quantity of grades (used by average)

Here's the algorithm:

While reading name and grade succeeds do:
  compare to highest value -- if greater, replace highest grade with new grade
  compare to lowest value -- if lower, replace lowest grade with new grade.
  increment number of grades
  add grade to sum.

Remember to replace the name along with the grade value.

If you need more help, try searching the internet "stackoverflow c++ highest grade average".

Comments