Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Headers
- #include <iostream> // cout, cin
- #include <cstdlib> // exit()
- #include <string> // strings
- #include <fstream> // file processing
- #include <iomanip> // 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 MAX_STUDENTS);
- 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);
- char getLetterGrade(double grade);
- int getLongestNameLength(string students[], int studentCount);
- int main()
- {
- // You will need some variables here
- string students[MAX_STUDENTS]; // You need an array of strings for the student names
- int grades[MAX_STUDENTS][MAX_GRADES]; // You need a two dimensional array of ints for the grades of the students
- int studentCount=0; // You need one to keep up with the actual number of students
- int choice=0; // You need a variable to hold the choice of the user for the menu; choice is a char b/c it recognizes the value as a char and not a number
- studentCount = loadStudentNamesGrades(students, grades, FILENAME, MAX_STUDENTS);
- // Loop selections until user says to quit
- do {
- // present menu and get user's choice
- cout << "Select 1 for the average." << endl;
- cout << "Select 2 for the maximum." << endl;
- cout << "Select 3 for the minimum." << endl;
- cout << "Select 4 to quit the program" << endl;
- // Process the choice
- cin >> choice;
- switch (choice)
- {
- case 1: displayAverages(students, grades, studentCount);
- break;
- case 2:displayMax(students, grades, studentCount);
- break;
- case 3:displayMin(students, grades, studentCount);
- break;
- case 4: //break closes the program
- break;
- default:
- cout << "Invalid choice. Try again." << endl;
- }//note that cases and switches are chars to find the location of the character
- cout << endl;
- } while (choice != 4);
- cout << "You quit the program." << endl; //end message & program
- system("pause");
- 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 <= numStudents <= maxStudents
- NOTE: students[] and grades[] are meant to be parralel arrays. students[0] and grades[0] are the same student
- ************************************************************/
- int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES], string fileName, int MAX_STUDENTS)
- {
- ifstream inFile;
- string firstName, lastName;
- int numStudents=0;
- inFile.open("NamesGrades.txt");
- if (!inFile) //Creates a message if is does not load the file
- {
- throw "This is not working";
- }
- for (int row = 0; row < MAX_STUDENTS && (inFile >> firstName >> lastName); row++, numStudents++)
- {
- inputFile >> "NamesGrades.txt";
- }
- return numStudents; // for stub out purposes, change this in your code
- }
- /***********************************************************
- 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
- ************************************************************/
- void displayAverages(string students[], int grades[][MAX_GRADES], int studentCount)
- {
- double total = 0;
- double average = 0;
- for (int row = 0; row < studentCount; row++)
- {
- for (int col = 0; col < MAX_GRADES; col++)
- {
- total += (grades[row][col]); //student names are located in the row
- }
- average = total / MAX_GRADES;
- cout << "Here is the average for student " << setw(20) << students[row] << setw(20) << average << setw(20) << getLetterGrade(average) << endl;//cout for each student
- total = 0;//reset total to 0 before it loops again}
- average = 0; // to reset the average to 0 before it loops again
- }
- }
- /***********************************************************
- 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
- ************************************************************/
- void displayMax(string students[], int grades[][MAX_GRADES], int studentCount)
- {
- for (int row = 0; row < studentCount; row++)
- {
- int max = 0;
- for (int col = 0; col < MAX_GRADES; col++)
- {
- if (grades[row][col] > max)
- max = grades[row][col];
- }
- cout << "The student with the highest grade is " << students[row] << setw(20) << "at"<< max << getLetterGrade(max) << 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
- ************************************************************/
- void displayMin(string students[], int grades[][MAX_GRADES], int studentCount)
- {
- for (int row = 0; row < studentCount; row++)
- {
- int min = 0;
- for (int col = 0; col < MAX_GRADES; col++)
- {
- if (grades[row][col] < min)
- min = grades[row][col];
- }
- cout << "The student with the lowest grade is " << students[row] << setw(20) <<"at" << min << getLetterGrade(min) << endl;
- }
- }
- /***********************************************************
- getLetterGrade converts a numerical grade to a letter grade
- PARAM: grade is the numerical grade to convert. Expected range is 0 <= grade <= 100
- PRE: grade contains a value in the correct range
- POST: The corresponding letter grade of the numerical grade is returned
- ************************************************************/
- char getLetterGrade(double grade)//passes the numerical grade and converts it to a letter grade
- {
- char letterGrade = 0;
- if (grade >= grade && grade <= 100)
- {
- letterGrade = 'A';
- }
- else if (grade >= 80 && grade <= 89)
- {
- letterGrade = 'B';
- }
- else if (grade >= 70 && grade <= 79)
- {
- letterGrade = 'C';
- }
- else if (grade >= 60 && grade <= 69)
- {
- letterGrade = 'D';
- }
- else
- {
- letterGrade = 'F';
- }
- return letterGrade;
- }
- /***********************************************************
- 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
- ************************************************************/
- int getLongestNameLength(string students[], int studentCount)
- {
- int longestName = 0;
- for (int row = 0; row < studentCount; row++)//loops through the names and compares them to find the longest
- {
- if (students[row].length() > longestName) //I like length over size b/c it reminds me of my intro python stuff.
- {
- longestName = students[row].length();
- }
- }
- return longestName;
- }1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement