*Pass by value, reference

I need to write 4 functions including main to display a report with the size of room in square feet, cost of carpet,cost to install the carpet. I am having trouble with using the last function to compute the costs using data from the other functions and then going back to main to report 3 values.

#include <iostream>
#include <iomanip>


using namespace std;
void getInput(double &, double &, double &);
double computeArea(double , double ) ;
void computeCosts(double&,double &);

	const int INCHESPERSQUAREFOOT=144; //constant to calculate area
	

	int main(){             //main function to call all nessesary functions and display results
		
		double width, 
			length, 
			carpetPerfootcost,
			area,
			instalcost,
			totalCarpetcost;
		
		cout<<"Ace Carpet Company Cost estimate";
		cout<<endl;
			
		//call getInput and get the users width, length and cost of the carpet
		getInput(width, length, carpetPerfootcost);
			
		//call compute area to preform the nessecary calculations and return the area
	
			
		//call compute costs to 
		computeCosts(totalCarpetcost,instalcost);
		
			cout<<setprecision(2)<<fixed;
			cout<<"The area in square feet is: "<<computeArea(width, length);
			cout<<"The cost of the carpet is: "<<totalCarpetcost<<endl;
			cout<<"The cost to install the carpet is "<<instalcost<<endl;
	return 0;
	}		

	void getInput(double &width, double &length, double &carpetPerfootcost)
		{	
	
			cout<<"please enter the width: "<<endl;
			cin>>width; 
			while(width<=0)
				{cout<<"Error, please enter a width with a postive value: ";
				cin>>width;}
		
			cout<<"please enter the length: "<<endl;
			cin>>length;
			while(length<=0)
				{cout<<"Error, please enter a length with a postive value: ";
				cin>>length;}
			
			cout<<"please enter the price of the carpet: "<<endl;
			cin>>carpetPerfootcost;
		    while(carpetPerfootcost<=0)
				{cout<<"Error, please enter a price with a postive value: ";
				cin>>carpetPerfootcost;}
		}

	double computeArea(double width, double length)
		{	
		
			return (width*length)/(INCHESPERSQUAREFOOT);
		}

void computeCosts(double &totalCarpetcost, double &instalcost)
	{	
		const double COSTPERFOOTINSTAL=5.75;
		 totalCarpetcost=carpetPerfootcost*area;       instalcost=area*COSTPERFOOTINSTAL;}

Recommended Answers

All 8 Replies

assign the value returned by computeArea() to variable area at main
then pass the variables carpetPerfootcost and area and to the last function

assign the value returned by computeArea() to variable area at main
then pass the variables carpetPerfootcost and area and to the last function

assign it after I call the area function? I guess I deleted the call before I posted but have it up now. I assigned it after the call and passed the two variables in the last function but I am getting 0 values for the report.

assign it after I call the area function?

Did you do it like this at main.

//call compute area to preform the nessecary calculations and return the area
area = computeArea(width, length);

I assigned it after the call and passed the two variables in the last function but I am getting 0 values for the report.

If you've already done something like this at the last function,

void computeCosts(double &totalCarpetcost, double &instalcost, double carpetPerfootcost,double area )

Did you already fix the function prototype to match the function, checked the values of area and carpetPerfootcost at main(before function call),at the function?

I Tried the program on my end and got it to work

Did you do it like this at main.

//call compute area to preform the nessecary calculations and return the area
area = computeArea(width, length);

If you've already done something like this at the last function,

void computeCosts(double &totalCarpetcost, double &instalcost, double carpetPerfootcost,double area )

Did you already fix the function prototype to match the function, checked the values of area and carpetPerfootcost at main(before function call),at the function?

I Tried the program on my end and got it to work

Oh thank you so much. It runs now but the values are a little off.For example like when I enter 50 and 50 with the price as 5.00 the area comes out as 17.36 (which is right) but the cost of the carpet comes out to 86.81 (should be 86.80) and the install cost comes out as 99.83 (should be 99.82). I can't remember how to fix that, something about timesing by like 100000 and taking the absolute value or something?

Again thank you so much for your help.

and the install cost comes out as 99.83

actually computations comes out correctly since it accurately computes for the answer which is 99.826388888... and is rounded of as 99.83

actually computations comes out correctly since it accurately computes for the answer which is 99.826388888... and is rounded of as 99.83

your right, not sure why I said it should be different. The 86.81 comes out as 86.8 on a calculator though.

try the built in calculator of your computer to see more precise computations

try the built in calculator of your computer to see more precise computations

Ok, I am going to call it good, hey thanks again for all your help. Take it easy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.