In: Computer Science
6.27 Ch 2 Program: Painting a wall (C++)
(4)Calculate the cost of the paint. Paint comes in six colors (Red,
Yellow, Green, Blue, Black,White) Assign numbers 0-5 to these paint
colors ie Red =0 and White =5. The costs for each of these paints
is as follows:(2 pts) Now your program will be submission
ready.
Main.cpp
#include <iostream>
#include <cmath>
using namespace std;
#include "paint.h"
int main() {
double wallHeight;
double wallWidth;
double wallArea;
double gallonsPaintNeeded;
int cansNeeded,colorNum;
Color paintColor;
double costArray[numOfColors];
cout << "Enter wall height (feet):
"<<endl;
cin >> wallHeight;
cout << "Enter wall width (feet):
"<<endl;
cin >> wallWidth;
cout<< "Enter paint color as an
integer"<<endl;
cout<<"Red - Enter
0"<<endl;
cout<<"Blue - Enter 1"<<endl;
cout<<"Green - Enter 2"<<endl;
cout<<"Yellow- Enter 3"<<endl;
cout<<"Black - Enter 4"<<endl;
cout<<"White - Enter 5"<<endl;
cout<<"Your color ?"<<endl;
cin>>colorNum;
paintColor= (Color)colorNum; // cast the integer to
type Color
// Calculate and output wall area
// FIXME (1): Calculate the wall's area
cout << "Wall area: " << wallArea <<
" square feet" << endl; // FIXME (1): Finish the
output statement
// FIXME (2): Calculate and output the amount of paint
in gallons needed to paint the wall
cout << "Paint needed: " <<
gallonsPaintNeeded << " gallons" << endl;
// FIXME (3): Calculate and output the number of 1
gallon cans needed to paint the wall, rounded up to nearest
integer
cout << "Cans needed: " << cansNeeded
<<" can(s)" << endl;
//FIX ME (3) populate the cost Array and calculate the
total cost of the paint
cout << "TotalCost: $" << totalCost<<" for
"<<cansNeeded;
switch (paintColor){
case Red: cout<<" Red";
break;
case Blue: cout<<" Blue";
break;
case Green: cout<<" Green";
break;
case Yellow: cout<<" Yellow";
break;
case Black: cout<<" Black";
break;
case White: cout<<" White";
break;
}
cout<<" can(s)" << endl;
return 0;
}
paint.h
#include<fstream>
const double squareFeetPerGallons = 350.0;
const double
gallonsPerCan =
1.0;
const int numOfColors =6;
enum Color {Red, Blue, Green, Yellow, Black,White};
double calculateWallArea(double height, double width){
// height times width
}
double calculatePaintAmount(double wallArea){
//wallarea divided by square feet per gallons
}
int calculateNumberOfCans(double paintAmount){
return ceil(paintAmount/gallonsPerCan); // round up the number of
cans needed
}
void getColorCost(double costArray[]){
//open the data file and read into array
}
double calculateCost(Color paintColor, double costArray[],double
numOfCans ){
// the cost of paint given the color and number of cans
}
cost.txt
1.99
2.99
0.99
2.99
0.49
0.99
// paint.h
#include<fstream>
#include <cmath>
using namespace std;
const double squareFeetPerGallons = 350.0;
const double gallonsPerCan = 1.0;
const int numOfColors =6;
enum Color {Red, Blue, Green, Yellow, Black,White};
double calculateWallArea(double height, double width){
return(height*width);
}
double calculatePaintAmount(double wallArea){
return(wallArea/squareFeetPerGallons);
}
int calculateNumberOfCans(double paintAmount){
return ceil(paintAmount/gallonsPerCan); // round up the number of cans needed
}
void getColorCost(double costArray[]){
ifstream fin("cost.txt"); // provide the full path to file
if(fin.is_open())
{
int i=0;
while((!fin.eof()) && (i < numOfColors))
{
fin>>costArray[i];
i++;
}
fin.close();
}else
{
for(int i=0;i<numOfColors;i++)
{
costArray[i] = 0;
}
}
}
double calculateCost(Color paintColor, double costArray[],double numOfCans ){
// the cost of paint given the color and number of cans
switch(paintColor)
{
case Red:
return(numOfCans*costArray[0]);
break;
case Blue:
return(numOfCans*costArray[1]);
break;
case Green:
return(numOfCans*costArray[2]);
break;
case Yellow:
return(numOfCans*costArray[3]);
break;
case Black:
return(numOfCans*costArray[4]);
break;
case White:
return(numOfCans*costArray[5]);
break;
default:
return(0);
break;
}
}
//end of paint.h
// main.cpp : C++ program to calculate the cost of painting a wall
#include <iostream>
#include <cmath>
using namespace std;
#include "paint.h"
int main() {
double wallHeight;
double wallWidth;
double wallArea;
double gallonsPaintNeeded, totalCost;
int cansNeeded,colorNum;
Color paintColor;
double costArray[numOfColors];
cout << "Enter wall height (feet): "<<endl;
cin >> wallHeight;
cout << "Enter wall width (feet): "<<endl;
cin >> wallWidth;
cout<< "Enter paint color as an integer"<<endl;
cout<<"Red - Enter 0"<<endl;
cout<<"Blue - Enter 1"<<endl;
cout<<"Green - Enter 2"<<endl;
cout<<"Yellow- Enter 3"<<endl;
cout<<"Black - Enter 4"<<endl;
cout<<"White - Enter 5"<<endl;
cout<<"Your color ?"<<endl;
cin>>colorNum;
paintColor= (Color)colorNum; // cast the integer to type Color
// Calculate and output wall area
wallArea = calculateWallArea(wallHeight,wallWidth);
cout << "Wall area: " << wallArea << " square feet" << endl; // FIXME (1): Finish the output statement
// Calculate and output the amount of paint in gallons needed to paint the wall
gallonsPaintNeeded = calculatePaintAmount(wallArea);
cout << "Paint needed: " << gallonsPaintNeeded << " gallons" << endl;
// Calculate and output the number of 1 gallon cans needed to paint the wall, rounded up to nearest integer
cansNeeded = calculateNumberOfCans(gallonsPaintNeeded);
cout << "Cans needed: " << cansNeeded <<" can(s)" << endl;
// populate the cost Array and calculate the total cost of the paint
getColorCost(costArray);
totalCost = calculateCost(paintColor,costArray,cansNeeded);
cout << "TotalCost: $" << totalCost<<" for "<<cansNeeded;
switch (paintColor){
case Red: cout<<" Red";
break;
case Blue: cout<<" Blue";
break;
case Green: cout<<" Green";
break;
case Yellow: cout<<" Yellow";
break;
case Black: cout<<" Black";
break;
case White: cout<<" White";
break;
}
cout<<" can(s)" << endl;
return 0;
}
//end of program
Output: