Part A: Write a c++ program that prompts the user to enter information about cou
ID: 3838692 • Letter: P
Question
Part A: Write a c++ program that prompts the user to enter information about course enrollments, and writes this data to file coursedata.txt, located in the folder Test on drive C:. Use CSIT courses currently being offered, but include only those taught by one of the following professors: Buzi, Hu, Maloney, Scialdone, Zubairi. For each course, enter the subject code and number, the instructor's last name, and the enrollment - for example:
CSIT 201
Zubairi
30
CSIT 221
Buzi
16
CSIT 241
Maloney
24
...
CSIT 463
Buzi
22
Note that the data should be written to the file in increasing order by course number.
Part B: Write a program that reads the data from the file produced in Part A, and produces a report showing the courses taught by each instructor, along with their enrollments, and the total enrollment for that instructor. This report should be written to the file coursereport.txt, located in the folder Test on drive C:. It should be in order by instructor last name - for example:
Buzi
CSIT 221 16
CSIT 341 25
CSIT 463 22
Total: 63
...
Zubairi
CSIT 201 30
CSIT 251 23
CSIT 425 15
Total: 68
Explanation / Answer
I am working on Ubuntu. Please change file names as per requirement.
Part1:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char data[100];
char check;
ofstream outfile;
outfile.open("coursedata.txt");
while(1){
cout << "Enter Course Code: " ;
cin.ignore();
cin.getline(data,100);
outfile << data << endl;
cin.clear();
cout << "Enter Instructor's last name: " ;
cin.getline(data,100);
outfile << data << endl;
cin.clear();
cout << "Enter enrollment: " ;
cin.getline(data,100);
outfile << data << endl;
cout << "To enter one more record type y else enter any character: " ;
cin >> check;
if(!(check == 'y')) break;
}
outfile.close();
return 0;
}
Part2:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
using namespace std;
int main(){
string prof[] = {"Buzi","Hu","Maloney","Scialdone","Zubairi"};
string courseCode;
string instructor;
string enrollment;
ifstream infile;
infile.open("coursedata.txt");
ofstream outfile;
outfile.open("coursereport.txt");
int i,count;
for(i=0;i<5;i++){
count = 0;
infile.clear();
infile.seekg(0);
outfile << prof[i] << endl;
while(getline(infile,courseCode)){
getline(infile,instructor);
getline(infile,enrollment);
if(!prof[i].compare(instructor)){
count += atoi(enrollment.c_str());
outfile << courseCode + " " + enrollment << endl;
}
}
outfile << "Total: " << count << endl;
}
infile.close();
outfile.close();
return 0;
}