Instructions: use this code and do the following modifications: - Change the pro
ID: 3716912 • Letter: I
Question
Instructions:
use this code and do the following modifications:
- Change the program to use instead of just one file (main.cpp), three files (main.cpp, *.h, and *.cpp) - This is called Encapsulation of the Code
- Add 1 more function, that allows changing the number of computers in a lab.
#include <iostream>
#include <string>
using namespace std;
/*
...
...
*/
struct Lab {
string name;
int nrComp;
bool avail;
};
void readLab(struct Lab & );
void displayLab(struct Lab);
void compareLab(struct Lab,struct Lab);
int main()
{
struct Lab ourLab; // declaration of variable of the struct type
struct Lab anotherLab;
readLab(ourLab); //call
displayLab(ourLab);//call
readLab(anotherLab);
displayLab(anotherLab);//call
copareLab(ourLab,anotherLab);//call to compare number of computers in nthe labs
}
void readLab(struct Lab &aLab)
{
char answer;
cout << "The name of the lab (no spaces) ";
cin >> aLab.name;
cout << "The number of computers in the lab ";
cin >> aLab.nrComp;
cout << "Is the lab available after hours (Y/N)? ";
cin >> answer;
if (answer == 'Y')
aLab.avail = true;
else
aLab.avail = false;
}
void displayLab(struct Lab aLab)
{
if (aLab.avail)
cout << aLab.name << " has " << aLab.nrComp << " computers and it is open after hours" << endl;
else
cout << aLab.name << " has " << aLab.nrComp << " computers and it is not open after hours" << endl;
}
void compareLab(struct Lab aLab,struct Lab bLab)
{
if (aLab.nrcomp>bLab.nrcomp)
cout<<aLab.name<<" has more computers than Lab"<<"blab.name"<<endl;
else
if (aLab.nrComp==bLab.nrComp)
cout<<"the lab has equal number of computers"<<"bLab.name"<<endl;
else
cout<<alab.name<<"it has more computers than lab"<<endl;
}
Explanation / Answer
// Main program file
#include <iostream>
#include <string>
#include "ProgramLab.h"
using namespace std;
int main()
{
struct Lab ourLab; // declaration of variable of the struct type
struct Lab anotherLab;
readLab(ourLab); //call
displayLab(ourLab);//call
readLab(anotherLab);//call
displayLab(anotherLab);//call
compareLab(ourLab,anotherLab);//call to compare number of computers in nth labs
changeNoComputers(ourLab);//call
displayLab(ourLab);//call
changeNoComputers(anotherLab);//call
displayLab(anotherLab);//call
compareLab(ourLab,anotherLab);//call to compare number of computers in nth labs
}
// ProgramLab.h file
#include <iostream>
#include <string>
#include "Lab.h"
using namespace std;
void readLab(struct Lab &aLab)
{
char answer;
cout <<endl<< "The name of the lab (no spaces) :: ";
cin >> aLab.name;
cout << "The number of computers in the lab :: ";
cin >> aLab.nrComp;
cout << "Is the lab available after hours (Y/N)? :: ";
cin >> answer;
if (answer == 'Y')
aLab.avail = true;
else
aLab.avail = false;
}
void changeNoComputers(struct Lab &aLab)
{
char answer;
cout << endl <<"Enter the modified number of computers in " << aLab.name << " :: ";
cin >> aLab.nrComp;
}
void displayLab(struct Lab aLab)
{
if (aLab.avail)
cout << endl << aLab.name << " has " << aLab.nrComp << " computers and it is open after hours" << endl;
else
cout << endl << aLab.name << " has " << aLab.nrComp << " computers and it is not open after hours" << endl;
}
void compareLab(struct Lab aLab,struct Lab bLab)
{
if (aLab.nrComp>bLab.nrComp)
cout<< endl<< aLab.name<<" has more computers than "<<bLab.name<<endl;
else if (aLab.nrComp==bLab.nrComp)
cout<< endl << aLab.name<<" and "<<bLab.nrComp<<" has equal number of computers"<<endl;
else
cout<< endl << aLab.name<<" has less computers than "<< bLab.name <<endl;
}
// Lab.h file
#include <iostream>
#include <string>
using namespace std;
struct Lab {
string name;
int nrComp;
bool avail;
};
/****************** OUTPUT OF PROGRAM ********************
The name of the lab (no spaces) :: computerScience
The number of computers in the lab :: 42
Is the lab available after hours (Y/N)? :: Y
computerScience has 42 computers and it is open after hours
The name of the lab (no spaces) :: Eletrical
The number of computers in the lab :: 34
Is the lab available after hours (Y/N)? :: Y
Eletrical has 34 computers and it is open after hours
computerScience has more computers than Eletrical
Enter the modified number of computers in computerScience :: 47
computerScience has 47 computers and it is open after hours
Enter the modified number of computers in Eletrical :: 58
Eletrical has 58 computers and it is open after hours
computerScience has less computers than Eletrical
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.