Question: C++ main.cpp #include #include #include #include Student.h #include Course.h using namespace std; int main() { ifstream inFile; cout < < Welcome to my Course Roster
C++
main.cpp
#include
#include "Student.h" #include "Course.h"
using namespace std;
int main() { ifstream inFile; cout << "Welcome to my Course Roster Program " << endl; while (true) { Course course = initCourse(); inFile.open("Students.txt"); if (!inFile) { cout << "File did not open! Program Exiting!!" << endl; return 0; } readStudent(inFile, course); inFile.close();
cout << "Here is the course roster: " << endl; printRoster(course); cout << endl;
char lastName[20];
cout << "Enter the last name of the student to drop : "; cin.getline(lastName, 20);
if (strcmp(lastName, "exit") == 0) { cout << " Thank you for using my Student Roster program!!" << endl; break; }
dropStudent(lastName, course);
cout << " Here is the course roster: " << endl; printRoster(course); cout << endl;
Student highestGPAStudent = findStudentHighestGPA(course);
cout << "The student with the highest GPA: " << endl; printStd(highestGPAStudent);
cout << " Thank you for using my Student Roster program " << endl; }
return 0; } Course.cpp
//The implementation file for Course.h //It loads students from a text file. #include
#include "Course.h" #include "Student.h" using namespace std;
//initiaizes a course variable and returns it. The array is empty //and the number of students is set to 0. Course initCourse() { Course course; course.numStudents = 0; return course; }
//reads a student from the file and calls AddStudent void readStudent(ifstream& inFile, Course& course) { char line[100]; while (inFile.getline(line, sizeof(line))) { char* firstName = strtok(line, ";"); char* lastName = strtok(nullptr, ";"); double gpa = atof(strtok(nullptr, ";")); Student student = initStudent(firstName, lastName, gpa); addStudent(student, course); } }
//adds a student to the roster, and increment count //modify this function to add the student sorted by name //do not use any sorting functions. //See this example in zyBooks Section 14.7 [https://learn.zybooks.com/zybook/PCCCS161BSpring23/chapter/14/section/7] void addStudent(Student student, Course &course) { course.roster[course.numStudents].gpa = student.gpa; strcpy(course.roster[course.numStudents].last, student.last); strcpy(course.roster[course.numStudents].first, student.first); course.numStudents++;
}
//prints a whole Course roster calling student print. //use a for loop and print all the students //must call the printStd function in Student.h void printRoster(Course course) { for (int i = 0; i < course.numStudents; i++) { cout << course.roster[i].first << ";" << course.roster[i].last << ";" << course.roster[i].gpa << endl; } }
void dropStudent(char *lastname, Course &course); Student findStudentHighestGPA(Course course); */ void dropStudent(char* lastname, Course& course) { for (int i = 0; i < course.numStudents; i++) { if (strcasecmp(lastname, course.roster[i].last) == 0) { // Shift the remaining students in the roster for (int j = i; j < course.numStudents - 1; j++) { course.roster[j] = course.roster[j + 1]; } course.numStudents--; cout << "Student '" << lastname << "' successfully dropped from the roster." << endl; return; } } cout << "Student '" << lastname << "' not found in the roster." << endl; } // Function to find the student with the highest GPA in the roster Student findStudentHighestGPA(Course course) { Student highestGPAStudent = course.roster[0]; for (int i = 1; i < course.numStudents; i++) { if (course.roster[i].gpa > highestGPAStudent.gpa) { highestGPAStudent = course.roster[i]; } } return highestGPAStudent; } Course.h
#ifndef COURSE_H_ #define COURSE_H_
#include "Student.h" #include
using namespace std;
struct Course { Student roster[20]; int numStudents; };
// Function Prototypes Course initCourse(); void readStudent(ifstream& inFile, Course& course); void addStudent(Student student, Course& course); void dropStudent(char* lastname, Course& course); Student findStudentHighestGPA(Course course); void printRoster(Course course);
#endif
Student.cpp
//The implementation file for student.h #include
#include "Student.h"
using namespace std;
//creates a student, initializes with information and returns it. Student initStudent(char *first, char *last, double gpa) { Student student; strcpy(student.first, first); strcpy(student.last, last); student.gpa = gpa; return student; }
//returns the last name of the student in the given char array through the pointer. void getLastName(char *studentName, Student student) { strcpy(studentName, student.last); }
//returns the student's gpa double getGPA(Student student) { return student.gpa; }
//prints a single student information void printStd(Student student) { cout << student.first << ";" << student.last << ";" << student.gpa << endl; }
Student.h
//The student struct #ifndef STUDENT_H_ #define STUDENT_H_
struct Student { char first[20]; char last[20]; double gpa; };
//Function Prototypes Student initStudent(char *first, char *last, double gpa); void getLastName(char *studentName, Student student); double getGPA(Student student); void printStd(Student student);
#endif
Students.txt
Henry;Nguyen;3.5 Brenda;Stern;2.0 Lynda;Robison;3.2 Sonya;King;3.9 Gayathri;Iyer;3.5 Glen;Sasek;3.7 Priya;Goel;3.8
this is example of how it should look
| First sample run without modifying addStudent:
Welcome to my Course Roster Program
Here is the course roster: Henry;Nguyen;3.5 Brenda;Stern;2 Lynda;Robison;3.2 Sonya;King;3.9 Gayathri;Iyer;3.5 Glen;Sasek;3.7 Priya;Goel;3.8
Enter the last name of the student to drop:King
Here is the course roster: Henry;Nguyen;3.5 Brenda;Stern;2 Lynda;Robison;3.2 Gayathri;Iyer;3.5 Glen;Sasek;3.7 Priya;Goel;3.8
The student with the highest GPA: Priya;Goel;3.8
Thank you for using my Student Roster program!! |
| Second sample run after modifying addStudent:
Welcome to my Course Roster Program
Here is the course roster: Priya;Goel;3.8 Gayathri;Iyer;3.5 Sonya;King;3.9 Henry;Nguyen;3.5 Lynda;Robison;3.2 Glen;Sasek;3.7 Brenda;Stern;2
Enter the last name of the student to drop:Iyer
Here is the course roster: Priya;Goel;3.8 Sonya;King;3.9 Henry;Nguyen;3.5 Lynda;Robison;3.2 Glen;Sasek;3.7 Brenda;Stern;2
The student with the highest GPA: Sonya;King;3.9
Thank you for using my Student Roster program!! |
i cannot get it to print the Second sample run after modifying addStudent:
Priya;Goel;3.8
Gayathri;Iyer;3.5
Sonya;King;3.9
Henry;Nguyen;3.5
Lynda;Robison;3.2
Glen;Sasek;3.7
Brenda;Stern;2
this how my program runs,
Welcome to my Course Roster Program
Here is the course roster: Henry;Nguyen;3.5 Brenda;Stern;2 Lynda;Robison;3.2 Sonya;King;3.9 Gayathri;Iyer;3.5 Glen;Sasek;3.7 Priya;Goel;3.8
Enter the last name of the student to drop : KING Student 'KING' successfully dropped from the roster.
Here is the course roster: Henry;Nguyen;3.5 Brenda;Stern;2 Lynda;Robison;3.2 Gayathri;Iyer;3.5 Glen;Sasek;3.7 Priya;Goel;3.8
The student with the highest GPA: Priya;Goel;3.8
Thank you for using my Student Roster program
Here is the course roster: Henry;Nguyen;3.5 Brenda;Stern;2 Lynda;Robison;3.2 Sonya;King;3.9 Gayathri;Iyer;3.5 Glen;Sasek;3.7 Priya;Goel;3.8
Enter the last name of the student to drop :
can someone fix it so it looks like the given example
- void addStudent(Student student, Course &course);
- Here you will modify the existing addStudent function. Right now the function adds students to the end of the list.
Here is the course roster:
Henry;Nguyen;3.5
Brenda;Stern;2
Lynda;Robison;3.2
Sonya;King;3.9
Gayathri;Iyer;3.5
Glen;Sasek;3.7
Priya;Goel;3.8
- Modify the function so it adds students sorted by lastname - you should not use any sorting algorithm. Insert the students in the right position, by finding the position and shifting the other students.
- Once you have modified this function, rerun the program to make sure it reads from the file and inserts students in the right position. Your output should look like this:
| Priya;Goel;3.8 Gayathri;Iyer;3.5 Sonya;King;3.9 Henry;Nguyen;3.5 Lynda;Robison;3.2 Glen;Sasek;3.7 Brenda;Stern;2 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
