Question: Introduction In this lab, you are going to implement a student grade recording system. The system supports functions including initializaion, display, appending (ID, name, and
Introduction
In this lab, you are going to implement a student grade recording system. The system supports functions including initializaion, display, appending (ID, name, and grade), lookup, and calculation of mean grade. To finish the lab, you need to self-study C++ File I/O and C++ String.
Description
The recording system maintains all information in a text file named data.txt. Each line in the text file stores student's ID, name, and grade, respectively.
The system should support the following four functions:
- init: to save all of the information given at the beginning of the main function into the file.
- display: to print out all of the content of the file.
- append: to append one student's information to the end of the file.
- lookup: to lookup one student's recording by the name. If find successfully, print out his/her information.
- cal_mean: to count the number of students in the file and to calculate the mean of their grades.
Implementation
- Start your coding with the skeleton code.
- You don't need to change the function headers.
- Complete append, lookup, and cal_mean. The implementation of init and display is given in the skeleton codes.
- You can assume that only 5 students' information need to be initialized, students' names are different, and there would be no more than 10 entries in the file even after appending.
Expected Output
Below is a sample run of the program.
Initialization success! Please enter: 1 to display all the entries 2 to append a new entry 3 to lookup an entry 4 to calculate the average grade 0 to exit: 1 ID Name Grade 1 Bill 90.5 2 Jack 95.5 3 Ron 85.5 4 Mark 93.5 5 Donald 87.5 Please enter: 1 to display all the entries 2 to append a new entry 3 to lookup an entry 4 to calculate the average grade 0 to exit: 2 Please enter the id you want to append: 6 Please enter the name you want to append: Harry Please enter the grade you want to append: 90.5 Please enter: 1 to display all the entries 2 to append a new entry 3 to lookup an entry 4 to calculate the average grade 0 to exit: 1 ID Name Grade 1 Bill 90.5 2 Jack 95.5 3 Ron 85.5 4 Mark 93.5 5 Donald 87.5 6 Harry 90.5 Please enter: 1 to display all the entries 2 to append a new entry 3 to lookup an entry 4 to calculate the average grade 0 to exit: 3 Please enter the name you want to look up: Peter Cannot find Peter Please enter: 1 to display all the entries 2 to append a new entry 3 to lookup an entry 4 to calculate the average grade 0 to exit: 3 Please enter the name you want to look up: Ron 3 Ron 85.5 Find Ron successfully! Please enter: 1 to display all the entries 2 to append a new entry 3 to lookup an entry 4 to calculate the average grade 0 to exit: 4 Find 6 students in total. Mean of their grades is 90.5 Please enter: 1 to display all the entries 2 to append a new entry 3 to lookup an entry 4 to calculate the average grade 0 to exit: 0
Hints
- When you would like to add something into a file, to avoid overwritting issue, you can firstly read all of the content existing in the file to the memory, e.g., arrays.
- For lookup, you can refer to the example on p.11 of the self-study lecture notes of C++ String.
#include#include using namespace std; #define MAX_NUM_STU 10 //maximum number of students #define NUM_INIT 5 //number of initial students //Save initial data into the file. void init(const int id[], const string name[], const float grade[], const string filename){ ofstream ofs(filename); int i; if (ofs){ ofs << "ID" << "\t" << "Name" << "\t" << "Grade" << endl; for (i=0; i > cmd; switch (cmd){ case 0: return 0; case 1: display(filename); break; case 2: cout << "Please enter the id you want to append: "; cin >> test_id; cout << "Please enter the name you want to append: "; cin >> test_name; cout << "Please enter the grade you want to append: "; cin >> test_grade; append(test_id, test_name, test_grade, filename); break; case 3: cout << "Please enter the name you want to look up: "; cin >> test_name; find = lookup(test_name, filename); if (find) cout << "Find " << test_name << " successfully!" << endl; else cout << "Cannot find " << test_name << endl; break; case 4: cal_mean(filename); break; default: cout << "Wrong command!" << endl; break; } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
