Create a program that calculates the cost of building a desk. The main() function calls four other functions. Pass all variables so that the function makes copies of any variables they receive:
a. A function to accept as input from the keyboard the number of drawers in the desk. This function returns the number of drawers to the main program.
b. A function to accept as input the type of wood –‘m’ for mahogany, ‘o’ for oak, or ‘p’ for pine
c. A function that receives the drawer number and wood type, and calculate the cost of the desk based on the following:
This function returns the cost of the main() function.
d. A function to display the final price.
#include <iostream>
#include <fstream>
using namespace std;
const int SURCHARGE=30;
const int PINE=100;
const int OTHERS=180;
const int OAK=140;
char deskType();
int numDeskDrawers();
int calculate(int num_drawers,char wood_type);
void display(int num_drawers,char wood_type);
int main( )
{
int n = numDeskDrawers();
char h = deskType();
display(n,h);
return 0;
}
int numDeskDrawers(){
cout<<"Enter the number of drawers:\n";
int n;
cin>>n;
return n;
}
char deskType(){
cout<<"Enter the type of wood like 'm' for mahogany, 'o'' for oak, or 'p' for pine:\n";
char n;
cin>>n;
return n;
}
int calculate(int num_drawers,char wood_type){
int total;
if (wood_type=='o'){
total = OTHERS * num_drawers + SURCHARGE;
}
else if(wood_type=='p'){
total = PINE * num_drawers + SURCHARGE;
}
else {
total = num_drawers * OTHERS + SURCHARGE;
}
return total;
}
void display(int num_drawers,char wood_type){
cout<<"The cost of building a desk is: "<<calculate(num_drawers,wood_type )<<endl;
}
Comments
Leave a comment