Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <cstring>
- #define SIZE 30
- using namespace std;
- typedef struct
- {
- char name[31];
- int grade1;
- int grade2;
- double average;
- } STUDENTREC;
- int read_grade(STUDENTREC stud_arr[], int index);
- void list(const STUDENTREC stud_arr[], int size);
- int main(void)
- {
- int action, index = 0;
- bool quit = false;
- STUDENTREC students[SIZE];
- while(!quit)
- {
- cout <<"\n\t\t******************************************";
- cout <<"\n\t\t**\tStudent Records Menu\t\t**";
- cout <<"\n\t\t******************************************\n\n";
- cout <<"\t\t<1> Read Grade.\n";
- cout <<"\t\t<2> List.\n";
- cout <<"\t\t<3> Quit.\n\n";
- cout <<"\tPlease enter your option --> ";
- cin >> action;
- cout <<"\n\n";
- if(action == 1)
- index = read_grade(students, index);
- else if(action == 2)
- list(students, index);
- else if(action == 3)
- quit = true;
- else
- cout <<"\tWrong selection.\n";
- }
- }
- int read_grade(STUDENTREC stud_arr[], int index)
- {
- if(index >= SIZE)
- cout << "Array is full.\n";
- else
- {
- cout <<"\tPlease enter the student name : ";
- fflush(stdin);
- cin.get(stud_arr[index].name, 31);
- cout <<"\tPlease enter the student grade 1: ";
- cin >> stud_arr[index].grade1;
- cout <<"\tPlease enter the student grade 2: ";
- cin >> stud_arr[index].grade2;
- stud_arr[index].average = (double)(stud_arr[index].grade1 + stud_arr[index].grade2) / 2.0;
- cout <<"\tThe average of the student grades is "
- << fixed << setprecision(2) << stud_arr[index].average << endl;
- index++;
- }
- return index;
- }
- void list(const STUDENTREC stud_arr[], int size)
- {
- if(size <= 0)
- {
- cout << "\tNo record.\n";
- return;
- }
- cout <<"Name\t\t\tGrade1\t\tGrade2\t\tAverage\n";
- for(int i = 0; i < size; i++)
- {
- if (strlen(stud_arr[i].name) < 15)
- cout << stud_arr[i].name << "\t\t"
- << setw(3) << stud_arr[i].grade1 << "\t\t"
- << setw(3) << stud_arr[i].grade2 << "\t\t"
- << setw(6) << fixed << setprecision(2)
- << stud_arr[i].average << endl;
- else
- cout << stud_arr[i].name << "\t"
- << setw(3) << stud_arr[i].grade1 << "\t\t"
- << setw(3) << stud_arr[i].grade2 << "\t\t"
- << setw(6) << fixed << setprecision(2)
- << stud_arr[i].average << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement