Reputation: 129
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???
Upvotes: 0
Views: 405
Reputation: 664
In case you're still stuck. As an exercise, modify for 2 students with same grade... ;)
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using std::cout;
using std::cin;
int main() {
int choice, intBuffer, subtotalGrades, countGrades,
average, highGrade, lowGrade = 100;
choice = intBuffer = subtotalGrades = countGrades = average = highGrade = 0;
std::string nameBuffer, studentHigh, studentLow;
cout << "\tStudent Grade Book Info Program....\n";
cout << "\tPlease Select an Option: (1 or 2) \n" << "\n"
<< "\t1. Review Grades"
<< "\n\t2. Quit"
<< "\n\tChoose: ";
cin >> choice;
if (choice == 2) {
return 0;
}
else {
std::ifstream gFile("gradeBook.txt", std::ios::in);
if (!gFile) {
cout << "\tError... Could not open gradeBook.txt\n";
return 1;
}
while (gFile) {
gFile.ignore();
getline(gFile, nameBuffer);
gFile >> intBuffer;
if (intBuffer > highGrade) {
highGrade = intBuffer;
studentHigh = nameBuffer;
}
else if (intBuffer < lowGrade) {
lowGrade = intBuffer;
studentLow = nameBuffer;
}
subtotalGrades += intBuffer;
countGrades++;
}
gFile.close();
}
average = subtotalGrades / countGrades;
// Report section.
cout << "\t" << std::setw(20) << "Student" << std::setw(30) << "Grade" << "\n";
cout << "\t" << std::setw(20) << studentHigh << std::setw(30) << highGrade << "\n";
cout << "\t" << std::setw(20) << studentLow << std::setw(30) << lowGrade << "\n";
cout << "\t" << std::setw(20) << "Average" << std::setw(30) << average << "\n";
return 0;
}
Upvotes: 1
Reputation: 57729
You don't need to store the information in your program.
Hint: You only need to store the following:
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".
Upvotes: 0