Can someone help please. I had help but it\'s not working. PLEASE DO BOTH PARTS!
ID: 3862256 • Letter: C
Question
Can someone help please. I had help but it's not working. PLEASE DO BOTH PARTS! Please make sure to read the entire problem and do both parts
PLEASE DO NOT write it out I have a hard time reading people's hand writing please just type it out. Thanks for any and all help I appreciate it!
Here is the problem:
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
#include<fstream>
#include<iostream>
using namespace std;
#include <iostream>
using namespace std;
//Class Defined for course
class Course
{
private:
//data members of class
int courseNo;
string courseName;
string instructorName;
int numberofStu;
public:
//To accept data
void getdata()
{
cout<<" Enter Subject Name: ";
cin>>courseName;
cout<<" Enter Subject Code: ";
cin>>courseNo;
cout<<" Enter Instructor Name: ";
cin>>instructorName;
cout<<" Enter Number of students: ";
cin>>numberofStu;
}//End of function
//To display data
void display()
{
cout<<" Subject Name: "<<courseName;
cout<<" Subject Code: "<<courseNo;
cout<<" Instructor Name: "<<instructorName;
cout<<" Number of students: "<<numberofStu;
}//End of function
//To write data to file
void write(int no, Course co[])
{
fstream f;
//Open file in write mode
f.open("coursedata.txt",ios::in|ios::out|ios::binary);
//If file does not exit show error
if(!f)
{
cerr<<"Cannot open file !";
return;
}//End of if
//Loops till number of students entered by the user
for(int x = 0; x < no; x++)
//Write data to file
f.write((char*)&co[x],sizeof(co[x]));
//close file
f.close();
}//end of function
//Read data from file
void read(int no, Course co[])
{
fstream f;
//Open file in read mode
f.open("coursedata.txt",ios::in|ios::out|ios::binary);
f.seekg(0); //The question is about this statement;
//Loops till number of students entered by the user
for(int j=0;j<no;j++)
{
//Read data from file and store in class object
f.read((char*)&co[j],sizeof(co[j]));
}//End of loop
//Close file
f.close();
}//End of function
//Sort the file according to course number
void SortNo(int no, Course co[])
{
int Temp, x, y;
//Loops till number of students entered by the user
for(x = 0; x < no; x++)
{
for(y = 0; y < no - x - 1; y++)
{
//Compares and swap
if(co[y].courseNo > co[y + 1].courseNo)
{
Temp = co[y].courseNo;
co[y].courseNo = co[y + 1].courseNo;
co[y + 1].courseNo = Temp;
}//End of if
}//End of inner for loop
}//End of outer for loop
}//end of function
//Sort data according to instructor name
void SortString(int no, Course co[])
{
int x, y;
Course Temp;
//Loops till number of students entered by the user
for(x = 0; x < no; x++)
{
for(y = 0; y < no - x - 1; y++)
{
//Compares and swap
if(co[x].instructorName.compare(co[y].instructorName) <= 0)
{
Temp = co[y];
co[y] = co[y + 1];
co[y + 1] = Temp;
}//End of if
}//End of inner loop
}//End of outer loop
}//End of function
//Counts the total according to instructor name
void countTotal(int no, Course co[])
{
int tot, x, y;
//Loops till number of students entered by the user
for(x = 0; x < no; x++)
{
tot = 0; //y = x + 1;
cout<<" Instructor Name: "<<co[x].instructorName;
for(y = x; y < no; y++)
{
//Checks if instructor name is same
if(co[x].instructorName.compare(co[y].instructorName) == 0)
{
cout<<endl<<co[y].courseName<<" "<<co[y].courseNo<<" "<<co[y].numberofStu;
//counts total of students
tot += co[y].numberofStu;
}//End of if
else
break;
}//End of inner loop
cout<<" Total: "<<tot;
x = y;
}//End of outer loop
}//End of function
};//End of class
//Main function
int main()
{
int no;
//Accepts number of students
cout<<" Enter number of course you want? ";
cin>>no;
Course co[50];
cout<<" Enter "<<no<<" course details ";
//Loops till number of students entered by the user
for(int i=0;i<no;i++)
{
//Accepts data for class objects
co[i].getdata();
}//End of loop
//Sort the object according to course number
co[0].SortNo(no, co);
//Writes data to file
co[0].write(no, co);
//Reads data from file
co[0].read(no, co);
//Sorts data according to instructor name
co[0].SortString(no, co);
//Loops till number of students entered by the user
for(int i=0;i<no;i++)
{
//Displays course information
co[i].display();
}//End of loop
//Counts total number of students according to instructor name
co[0].countTotal(no, co);
}//End of main
Output:
Enter number of course you want? 5
Enter 5 course details
Enter Subject Name: cc
Enter Subject Code: 12
Enter Instructor Name: Pyari
Enter Number of students: 10
Enter Subject Name: cc
Enter Subject Code: 23
Enter Instructor Name: Ramesh
Enter Number of students: 5
Enter Subject Name: cc
Enter Subject Code: 11
Enter Instructor Name: Pyari
Enter Number of students: 10
Enter Subject Name: cc
Enter Subject Code: 22
Enter Instructor Name: Ramesh
Enter Number of students: 5
Enter Subject Name: cc
Enter Subject Code: 10
Enter Instructor Name: Pyari
Enter Number of students: 10
Subject Name: cc
Subject Code: 12
Instructor Name: Pyari
Number of students: 10
Subject Name: cc
Subject Code: 23
Instructor Name: Pyari
Number of students: 10
Subject Name: cc
Subject Code: 10
Instructor Name: Pyari
Number of students: 10
Subject Name: cc
Subject Code: 11
Instructor Name: Ramesh
Number of students: 5
Subject Name: cc
Subject Code: 22
Instructor Name: Ramesh
Number of students: 5
Instructor Name: Pyari
cc 12 10
cc 23 10
cc 10 10
Total: 30
Instructor Name: Ramesh
cc 11 5
cc 22 5
Total: 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.