Question: (DEBUGGING PROGRAM) Write a program to read from a file called courses.dat and store the information into a singly linked list. The courses.dat file contains
(DEBUGGING PROGRAM)
Write a program to read from a file called courses.dat and store the information into a singly linked list.
The courses.dat file contains the following information Course number, year, semester, grades and credits.
The program must be interactive (menu-driven) and provide the following functionality:
1: Print Transcipt (All course information, total credits, total quality points, gpa)
2: Add a course
3: Delete a course
4: Print the transcript for specified semester
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
(PROGRAM)
#include
using namespace std;
struct Course { int courseNumber; int year; int semester; int grades; int credits; Course *next; };
// Function to read courses from file and add to linked list void readFromFile(Course *&head) { ifstream file("courses.dat"); if (!file.is_open()) { cout << "Error opening file" << endl; exit(EXIT_FAILURE); }
int courseNumber, year, semester, grades, credits; while (file >> courseNumber >> year >> semester >> grades >> credits) { Course *newNode = new Course; newNode->courseNumber = courseNumber; newNode->year = year; newNode->semester = semester; newNode->grades = grades; newNode->credits = credits; newNode->next = head; head = newNode; } file.close(); }
// Function to add a course to linked list void addCourse(Course *&head) { int courseNumber, year, semester, grades, credits; cout << "Enter course number: "; cin >> courseNumber; cout << "Enter year: "; cin >> year; cout << "Enter semester: "; cin >> semester; cout << "Enter grades: "; cin >> grades; cout << "Enter credits: "; cin >> credits;
Course *newNode = new Course; newNode->courseNumber = courseNumber; newNode->year = year; newNode->semester = semester; newNode->grades = grades; newNode->credits = credits; newNode->next = head; head = newNode; }
// Function to delete a course from linked list void deleteCourse(Course *&head, int courseNumber) { Course *prev = nullptr; Course *curr = head; while (curr != nullptr && curr->courseNumber != courseNumber) { prev = curr; curr = curr->next; }
if (curr == nullptr) { cout << "Course not found" << endl; return; }
if (prev == nullptr) { head = head->next; } else { prev->next = curr->next; } delete curr; }
// Function to print transcript void printTranscript(Course *head) { int totalCredits = 0; int totalQualityPoints = 0; Course *curr = head; while (curr != nullptr) { cout << "Course Number: " << curr->courseNumber << endl; cout << "Year: " << curr->year << endl; cout << "Semester: " << curr->semester << endl; cout << "Grades: " << curr->grades << endl; cout << "Credits: " << curr->credits <
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
