The first question has been answered, but there is now a second part to it. (The
ID: 3628630 • Letter: T
Question
The first question has been answered, but there is now a second part to it. (The question is below the program) Here is the program that has been answered:#include<iostream>
#include<string>
using namespace std;
class Module
{
private:
string moduleName;
string moduleCode;
string lecturer;
int nrStudents;
public:
Module()
{
moduleName=lecturer=" ";
moduleCode="0000000";
nrStudents=0;
}
Module(string N ,string C,string L,int num)
{
moduleName=N;
moduleCode=C;
lecturer=L;
nrStudents=num;
}
void setmoduleName(string N)
{
moduleName=N;
}
void setmoduleCode(string N)
{
moduleCode=N;
}
void setlecturer(string N)
{
lecturer=N;
}
void setnrStudents(int N)
{
nrStudents=N;
}
string printmoduleName()
{
return moduleName;
}
string printmoduleCode()
{
return moduleCode;
}
string printlecturer()
{
return lecturer;
}
int printnrStudents()
{
return nrStudents;
}
};
int main()
{
Module obj1,obj2("classes","9876","4 lectures",9);
string name,code,lec;
int num;
cout <<"Enter name of module:";
getline(cin,name);
cout <<"Enter code: ";
cin >> code;
cin.ignore(10000,' ');
cout << "Enter name of lecturer: ";
getline(cin,lec);
cout << "Enter number: ";
cin >> num;
obj1.setmoduleName(name);
obj1.setmoduleCode(code);
obj1.setlecturer(lec);
obj1.setnrStudents(num);
cout <<endl;
cout << "Module name: " << obj1.printmoduleName() <<endl;
cout << "Module code: " << obj1.printmoduleCode()<<endl;
cout << "Lecturer: " << obj1.printlecturer()<<endl;
cout << "Students: " << obj1.printnrStudents()<<endl;
}
Now the question is:
Overload the stream extraction operator >> for the class Module in Question 1 to input values for an object of class module either from the keyboard or from a file. Also overload the stream insertion operator << to output the data for a module either on the screen or to a file. Use separate compilation and write a program that inputs the name of a lecturer. The program should then find all the modules taught by that lecturer in a file named Modules.dat, and put those objects into an array. Assume that the file will never contain data for more than 20 modules. Use the array to determine the total number of students taught by the lecturer, as well as the average number of students per module taught by the lecturer. Display the names
of the modules, the total number of students taught by the lecturer, and the average number of students per module taught by the lecturer on the screen. Use the overloaded stream extraction operator >> to extract objects from file Modules.dat. Use the following data for Modules.dat:
Introduction to Programming I
COS1511
Mrs Schoeman
534
Introduction to Programming I
COS1511
Mrs du Plessis
2534
Introduction to Programming II
COS1512
Mrs Schoeman
534
Introduction to Programming II
COS1512
Mrs du Plessis
2534
Explanation / Answer
please rate - thanks
by separate compilation do you mean a separate program? if so that's a separate post and you need to be more specific
example show for each lecturer or input lecturer name and then just for that lecturer
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class Module
{friend ostream &operator<<( ostream &, const Module & );
friend istream &operator>>( istream &,Module & );
private:
string moduleName;
string moduleCode;
string lecturer;
int nrStudents;
public:
Module()
{
moduleName=lecturer=" ";
moduleCode="0000000";
nrStudents=0;
}
Module(string N ,string C,string L,int num)
{
moduleName=N;
moduleCode=C;
lecturer=L;
nrStudents=num;
}
void setmoduleName(string N)
{
moduleName=N;
}
void setmoduleCode(string N)
{
moduleCode=N;
}
void setlecturer(string N)
{
lecturer=N;
}
void setnrStudents(int N)
{
nrStudents=N;
}
string printmoduleName()
{
return moduleName;
}
string printmoduleCode()
{
return moduleCode;
}
string printlecturer()
{
return lecturer;
}
int printnrStudents()
{
return nrStudents;
}
};
ostream& operator<<( ostream &output, const Module &num )
{output<<"Module Name "<<num.moduleName<<endl;
output<<"Module Code "<<num.moduleCode<<endl;
output<<"Lecturer "<<num.lecturer<<endl;
output<<"Number of Students "<<num.nrStudents<<endl;
return output;
}
istream& operator>>( istream &in,Module &num )
{getline(in,num.moduleName);
in >> num.moduleCode;
in.ignore(10000,' ');
getline(in,num.lecturer);
in >> num.nrStudents;
in.ignore(10000,' ');
return in;
}
int main()
{
ifstream in;
in.open("modules.dat");
if(in.fail()) //is it ok?
{ cout<<"modules.dat did not open please check it ";
system("pause");
return 1;
}
Module obj1[20],obj2("classes","9876","4 lectures",9);
int num,i=0;
in>>obj1[i];
while(in)
{
cout<<obj1[i];
i++;
in>>obj1[i];
}
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.