C++ Help This program practices dynamic arrays to manage a set of students and t
ID: 3690826 • Letter: C
Question
C++ Help
This program practices dynamic arrays to manage a set of students and their courses.
Input:
File courses.txt contains a list of student names, the number of courses taken, and the names of the courses. The first line of the file contains the number of students. It looks as follows:
12
John Milligan
3 CIS100 CIS105 MAT113
Jill Kerning
5 CIS100 CIS105 MAT232 BIO100 ENG101
Aaron Spencer
4 CIS201 CIS225 MAT232 ENG101
Damon Hill
2 CIS334 CIS400
Kaitlyn Stamen
4 CIS100 CIS10 MAT113 BIO100
Debbie Martin
5 CIS100 CIS105 MAT232 CHY112 ENG101
Greg Nolan
2 CIS334 CIS450
Lynn Sanders
4 CIS334 CIS450 MAT250 BIO100
Alicia Thomas
4 CIS226 CIS450 MAT232 CHY112
Alan Turner
5 CIS100 CIS105 MAT232 BIO100 ENG101
Paul Henley
5 CIS100 CIS105 CIS334 ENG101 MAT232
Tim Copeland
1 CIS450
Create a dynamic array of structs of the size provided in the first line of the file. The struct definition should contain a C++ string name, an integer number of courses, and a C++ string pointer. Read the data into the dynamic array. Note that once the number of courses is read, the code can create the dynamic array of course names for the student.
Processing:
Allow the user the following options.
a. Display all students and courses
b. Enter a student name and report a list of courses taken by the student
c. Enter a course number and report a list of all students taking this course.
Should look something like this:
Explanation / Answer
Use the below C++ program to achive your assignment:
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <iomanip>
using namespace std;
struct Classes
{
string names; //Holds names of the students
string *courses; //Number of students
int num; //Holds the courses the student is taking
};
int main(int argc, const char * argv[])
{
fstream fin;
fin.open ("courses.txt");
int size; //Number of students
fin >> size;
Classes * a = new Classes[size]; //Array of struct which holds a students name and classes
char el;
fin.get(el);
for (int ct = 0; ct < size; ct++)
{
getline (fin, a[ct].names);
fin >> a[ct].num;
a[ct].courses = new string [a[ct].num]; //Array that holds a student courses
for ( int ct2=0; ct2 < a[ct].num; ct2++)
{
fin >> a[ct].courses[ct2];
}
fin.get(el);
}
cout << "Enter menu choice or Q to quit: n";
cout << "D to display all students and courses n";
cout << "S to display courses for a student n";
cout << "C to display students taking a course n";
char choice; //Variable giving the user an option
cin >> choice;
while (choice != 'Q')
{
if (choice == 'D')
{
cout << left << setw(20) << "Name" << setw(50) << "Courses" << "n";
for (int ct = 0; ct < size; ct++)
{
cout << left << setw(20) << a[ct].names;
for (int ct2 = 0; ct2 < a[ct].num; ct2++)
{
cout << a[ct].courses[ct2] << " ";
}
cout << "n";
}
cout << "n";
}
if (choice == 'S')
{
string sname; //Searches Name
cout << "Enter students name: ";
cin.get(el);
getline(cin, sname );
bool found = false;
int i = 0; //Counter Variable
while (!found && i < size)
{
if (a[i].names == sname)
found = true;
else
i++;
}
if (found)
{
cout << "n" << sname << " is taking: ";
for (int ct = 0; ct < a[i].num; ct++)
{
cout << a[i].courses[ct] << " ";
}
}
cout << "n";
}
if (choice == 'C')
{
string courses; //Searches Course
bool enrolled = false;
cout << endl << "Enter course name: ";
cin >> courses;
cout << endl << "Student taking " << courses << ":" << endl;
for (int ct = 0; ct < size; ct++)
{
for (int ct2 = 0; ct2 < a[ct].num; ct2++)
{
if (a[ct].courses[ct2] == courses)
{
enrolled = true;
cout << a[ct].names << endl;
}
}
}
if (enrolled == false)
{
cout << endl << "No one is enrolled in " << courses << "." << endl << endl;
}
else
{
cout << endl;
}
}
char x; //Variable used to pick up the endl
cin.get (x);
cin >> choice;
}
system("pause");
}
course.txt
12
John Milligan
3 CIS100 CIS105 MAT113
Jill Kerning
5 CIS100 CIS105 MAT232 BIO100 ENG101
Aaron Spencer
4 CIS201 CIS225 MAT232 ENG101
Damon Hill
2 CIS334 CIS400
Kaitlyn Stamen
4 CIS100 CIS10 MAT113 BIO100
Debbie Martin
5 CIS100 CIS105 MAT232 CHY112 ENG101
Greg Nolan
2 CIS334 CIS450
Lynn Sanders
4 CIS334 CIS450 MAT250 BIO100
Alicia Thomas
4 CIS226 CIS450 MAT232 CHY112
Alan Turner
5 CIS100 CIS105 MAT232 BIO100 ENG101
Paul Henley
5 CIS100 CIS105 CIS334 ENG101 MAT232
Tim Copeland
1 CIS450
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.