Please help me!!!!! Write a C++ program that can help the instructor manage the
ID: 3778641 • Letter: P
Question
Please help me!!!!!
Write a C++ program that can help the instructor manage the information of all students in a class. ((You need to design two Classes)).
The first class one representing a student that includes relevant data fields such as first name, last name, SIU ID, score. You also need to create a file storing the information all students, named “students.txt”. Suppose each row of the file contains the information of one students in the following format and you can determine their values by your own.
Kang Chen 777777 88
The second Class represents the classroom, which contains the instructor name, location and time of the class, an array of students, and a series of member functions helping managing the class. You suppose the class has at most 30 students or use the dynamic std::vector to alleviate this limit. The series of member functions include:
Default constructor: The default constructor will set the instructor name to “Kang Chen”, the location to “Hall0101”, the class time to “MFW1PM-1:50PM”, and read the students.txt and store student information in the array of students.
ListCalssInfo: The function will print instructor name, class location and time, and the number of students in the class to console.
AddAStudent: The function will ask the instructor to input the information of the student. It will check whether a student with the same name (compare both first and last name) already exists. If yes, the inputted information will be used to update the student’s information. Otherwise a new student will be created.
DropAStudent: The function will ask the instructor to input the first and last name of the student. If such a student exists, the student’s information will be deleted. Otherwise, a prompt will be printed to the instructor saying ‘not exist’.
ListAllStudents: The function will list the information of all students one row by one row.
SaveStudentInfo: The function will write the information of all students stored in the array of students to the “students.txt” file. Each row of the file should contain the information of one student.
ListOneStudent: The function will ask the instructor to input the first and last name of the student. If such a student exists, it will list the information of the student. Otherwise, a prompt will be printed to the instructor saying ‘not exist’.
UpdateAStudent: The function will modify the information of a student. The program will ask the instructor to input the first and last name of the student. If such a student exists, it will further ask the instructor to further input other information of the student. Otherwise, a prompt will be printed to the instructor saying ‘not exist’.
CalculateStatistic: The function will calculate and print the maximal score, minimal score, average score, andmedium score of all students to the console.
In your main function, you need to declare an object of the classroom class, and print and execute a menu like the following:
ListCalssInfo
AddAStudent
DropAStudent
ListAllStudents
SaveStudentInfo
ListOneStudent
UpdateAStudent
CalculateStatistic
Exit
Your program should allow the user to repetitively select an option from the menu. When an option is selected, the corresponding member function of your classroom object should be called to finish the corresponding task. Note that if “9 Exit” is selected, you have to call the SaveStudentInfo function before exit the problem. As a result, the updated information is always stored.
Explanation / Answer
#include <iostream>
#include <string.h>
#include <sstream>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
class student {
public:
char first_name[16], last_name[16];
int SIU_ID;
float score;
};
class classroom {
public:
student st;
string instructor_name,location,Time;
char **students ;
int n;
classroom() {
FILE *file;
n = 0;
instructor_name = "Kang Chen";
location = "Hall0101";
Time = "MFW1PM-1:50PM";
students = new char *[10];
for(int i=0; i<=10; i++)
students[i] = new char[128];
file = fopen("students.txt", "r");
while(fgets(students[n], sizeof(students), file))
n++;
}
void ListCalssInfo() {
cout<<" "<<instructor_name<<" "<<location<<" "<<Time<<" ";
cout<<" Number of students : "<<n<<" ";
}
void AddAStudent() {
char studentname[128];
cout<<"Enter student information First Name : ";
cin>>st.first_name;
cout<<"Last Name : "; cin>>st.last_name;
cout<<"Student ID : "; cin>>st.SIU_ID;
cout<<"Score : "; cin>>st.score;
strcpy(studentname, st.first_name);
strcat(studentname, " ");
strcat(studentname, st.last_name);
int flag = 0,i;
char buf[10], buf1[10];
for(i=0; i<n; i++) {
if(strstr(students[i], studentname)!= NULL){ // exist, update the student information
flag = 1;
sprintf(studentname, "%s %d %f", studentname, st.SIU_ID, st.score);
strcpy(students[i], studentname);
}
}
if(flag == 0) { // Does not exist , add the new student
sprintf(studentname, "%s %d %f", studentname, st.SIU_ID, st.score);
strcpy(students[i], studentname);
n = n+1;
}
}
void DropAStudent(){
char studentname[128];
cout<<"Enter student information First Name : ";
cin>>st.first_name;
cout<<"Last Name : "; cin>>st.last_name;
cout<<"Student ID : "; cin>>st.SIU_ID;
cout<<"Score : "; cin>>st.score;
strcpy(studentname, st.first_name);
strcat(studentname, " ");
strcat(studentname, st.last_name);
int flag = 0,i;
char buf[10], buf1[10];
for(i=0; i<n; i++) {
if((strstr(students[i], studentname))!= NULL){
flag = 1;
delete students[i];
n = n-1;
}
}
if(flag == 0) {
sprintf(studentname, "%s %d %f", studentname, st.SIU_ID, st.score);
strcpy(students[i], studentname);
n = n+1;
}
}
void ListAllStudents(){
for(int i=0; i<n; i++)
cout<<students[i]<<" ";
}
void SaveStudentInfo(){ // updated information save into file
FILE *file;
file = fopen("students.txt", "r");
for(int i=0; i<n; i++){
fprintf(file, students[i]);
}
}
void ListOneStudent() {
char studentname[128];
cout<<"Enter student information First Name : ";
cin>>st.first_name;
cout<<"Last Name : "; cin>>st.last_name;
sprintf(studentname, "%s %s", st.first_name, st.last_name);
for(int i=0; i<n; i++){
if((strstr(students[i], studentname))!= NULL)
cout<<students[i]<<" ";
}
}
void UpdateAStudent() {
char studentname[128];
cout<<"Enter student information First Name : ";
cin>>st.first_name;
cout<<"Last Name : "; cin>>st.last_name;
cout<<"Student ID : "; cin>>st.SIU_ID;
cout<<"Score : "; cin>>st.score;
sprintf(studentname, "%s %s", st.first_name, st.last_name);
int flag = 0,i;
for(i=0; i<n; i++) {
if(strstr(students[i], studentname)!= NULL){ // exist, update the student information
flag = 1;
sprintf(studentname, "%s %d %f", studentname, st.SIU_ID, st.score);
strcpy(students[i], studentname);
}
}
}
void CalculateStatistic() {
float sum = 0;
for(int i=0; i<n; i++)
sum = sum + st.score;
cout<<"Total Score : "<<sum;
}
};
int main()
{
int option;
char ch;
classroom clsrm;
do {
cout<<"1. ListCalssInfo 2. AddAStudent 3. DropAStudent 4. ListAllStudents 5. SaveStudentInfo 6. ListOneStudent 7. UpdateAStudent 8. CalculateStatistic 9. Exit Choose option : ";
cin>>option;
switch(option){
case 1: clsrm.ListCalssInfo();
break;
case 2: clsrm.AddAStudent();
break;
case 3: clsrm.DropAStudent();
break;
case 4: clsrm.ListAllStudents();
break;
case 5: clsrm.SaveStudentInfo();
break;
case 6: clsrm.ListOneStudent();
break;
case 7: clsrm.UpdateAStudent();
break;
case 8: clsrm.CalculateStatistic();
break;
case 9: return 0;
}
cout<<"You wants more operation (Y/y) : ";
cin>>ch;
}while(ch == 'y' || ch =='Y');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.