63 169
Assignments Done
99,3%
Successfully Done
In July 2018

Answer to Question #48010 in C++ for joney

Question #48010
A construction company has many new houses. Each house has an N rooms. The company would like to buy carpet for these rooms of all houses. The carpet is available with three colors: Red, Blue, and Green. The cost of the squared meter of the carpet for the Red, Blue, and Green colors is 10.5, 12.75, and 15 squared meter respectively. The company would like to get some estimation for the cost of buying the carpet for their houses. Specifically, they want to get a report for the cost of each house, and a final report for the cost of all houses (rooms). (keep reading)
Write a complete C program for this company considering the following:
The program should prompt the user to enter whether he wants to calculate for more houses or not (Keep reading),
If the answer is Y or y
then the program should prompt the user to enter the number of the rooms of the current house, then it should prompt the user to enter the length and width of each room in order to calculate its area, and to enter the color of the carpet for each room (R or r for RED, B or b for Blue, G or g for Green), (Keep reading), then, the computer generates a report about the carpet cost of this house, and then prompts the user again to enter whether he has more houses or not.
if the answer is N or n
if no calculations took place then print on the screen “No actions took place”,
else the program generates the report for the final cost of all houses and exit.
Otherwise, the program should print on the screen bad input, and prompt the user again to enter his response.

Consider the following run:
Do you have more houses: Y
Enter number of rooms: 2
Enter length and width of room1: 2.5 3
Enter the carpet color of room1: R
Enter length and width of room2: 3.75 4.2
Enter the carpet color of room2: G

Cost Report of House# 1

Color Area Cost
-----------------------------------
Red 7.5 78.75
Blue 0.0 0.0
Green 15.75 236.25
-----------------------------------
Total cost of House 1 is 315

Do you have more houses: G
Bad input
Do you have more houses: y
Enter number of rooms: 4
Enter the length and width of room1: 3 3.5
Enter the carpet color of room1: R
Enter the length and width of room2: 3.75 4.4
Enter the carpet color of room2: G
Enter the length and width of room3: 4 4
Enter the carpet color of room3: R
Enter the length and width of room4: 3. 5 4.2
Enter the carpet color of room4: B

Cost Report of house# 2

Color Area Cost
-----------------------------------
Red 26.5 278.25
Blue 14.7 187.425
Green 16.5 247.5
-----------------------------------
Total cost of room 2 is 0.0

Do you have more houses: N

The final report of total cost of all houses
Color Area Cost
-----------------------------------
Red 34.0 357.0
Blue 14.7 187.425
Green 32.25 483.75
-----------------------------------
Total cost of all houses 1028.175

Thank you for using our program

------------------------------------------------

Consider another run:

Do you have more houses: N

No action took place

Thank you for using our program

Expert's answer
#include <stdlib.h>
#include <iostream>  
#include <stdio.h>
#include <cstdlib>  
#include <string>
using namespace std;
struct Carpet
{
float square;
float cost;
    Carpet(float _cost)
{
     cost = _cost; square = 0;
}
float GetCost()
{
return cost*square;
}
Carpet() {square = 0;};
};
class House
{
int numberOfRooms;
public:
Carpet red;
Carpet blue;
Carpet green;
int houseNumber;
float cost;
House()
{
cost  = 0;
red.cost = 10.5;
blue.cost = 12.75;
green.cost = 15;
}
void AddCost(float square, int color)
{
if (color == 1) red.square += square;
if (color == 2) blue.square += square;
if (color == 3) green.square += square;
}
void GetInfo()
{
cout<<"Color Area Cost"<<endl;
cout<<"-----------------------------------"<<endl;
cout<<"Red "<<red.square<<" "<<red.cost * red.square<<endl;
   cout<<"Blue "<<blue.square<<" "<<blue.cost * blue.square<<endl;
   cout<<"Green "<<green.square<<" "<<green.cost * green.square<<endl;
cout<<"-----------------------------------"<<endl;
cout<<"Total cost of House "<<houseNumber<<" is "<<red.cost*red.square + blue.cost*blue.square + green.cost*green.square<<endl;
}
};
int main()
{
Carpet red(10.5);
Carpet blue(12.75);
Carpet green(15);
    House houses[100];
int numberHouses = 0;
float length; float width;
int roomNumber;
char c;
while(true)
{
do
{
cout<<"Do you have more houses: ";
cin>>c;
if (c != 'N' && c != 'n' && c != 'Y' && c != 'y') cout<<"Bad input"<<endl;
} while(c != 'N' && c != 'n' && c != 'Y' && c != 'y');
if (c == 'N' || c == 'n') break;

++numberHouses;
houses[numberHouses -1].houseNumber = numberHouses;
cout<<"Enter number of rooms: ";
cin>>roomNumber;
char symbol;
for (int i = 0; i < roomNumber; i++)
{
cout<<"Enter the length and width of room"<<i+1<<": ";
cin>>length>>width; cin.clear();
cout<<"Enter the carpet color of  of room"<<i+1<<": ";
cin>>symbol; cin.clear();
if (symbol == 'R') {red.square += width * length; houses[numberHouses -1].AddCost( width * length, 1);}
if (symbol == 'B') {blue.square += width * length; houses[numberHouses -1].AddCost( width * length, 2);}
if (symbol == 'G') {green.square += width * length; houses[numberHouses -1].AddCost( width * length, 3);}
}
cout<<endl;
cout<<"Cost Report of house# "<<numberHouses<<endl;
cout<<endl;
houses[numberHouses -1].GetInfo();
cout<<endl<<endl<<endl;
}


if (numberHouses == 0 ) cout<<"No action took place"<<endl;
else
{
cout<<endl;
cout<<"The final report of total cost of all houses "<<endl<<endl;
   cout<<"Color Area Cost"<<endl<<endl;
   cout<<"-----------------------------------"<<endl;
cout<<"Red "<<red.square<<" "<<red.cost * red.square<<endl;
cout<<"Blue "<<blue.square<<" "<<blue.cost * blue.square<<endl;
cout<<"Green "<<green.square<<" "<<green.cost * green.square<<endl;
   cout<<"-----------------------------------"<<endl;
cout<<"Total cost of all houses "<<red.cost*red.square + blue.cost*blue.square + green.cost*green.square<<endl<<endl;
cout<<"Thank you for using our program"<<endl;
cout<<"-----------------------------------"<<endl;
}
system("pause");
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions