0

:?: Hi i am in my first C++ class and have already written two simple C++ programs. Im working on another using functions.

I have to write 3 functions to calculate the area, cost, and retail price for wooden boxes and a main function to have the user input information for the program and output the results.

main - prompts user for the type and dimensions of the box. I am also supposed to print out the money values with currency notation i used #include <iomanip>

here is what i have. I am still getting 8 errors and 4 warnings.

// David Shaw
// Lab 3 - Functions
// 02/18/05
#include <iostream>
#include <iomanip>
using namespace std;

float boxSurfaceArea (float, float, float);
float boxCost (int, float);
float boxPrice (int, float, float);


int main()
{
	// variables
	int type;
	float length;
	float width;
	float height;
	float surfaceArea;
	float cost;
	float price;
	

	do 
	{

	// User input
	cout<<"Enter a number, (1, 2, 3, or 4) to select a boxType.\n " << endl;
	cin>>type; 
	}
	
	switch ( type ) 
	{
	
	case 1:
	cout<<"Enter the length: ";
	cin>>length; 

	cout<<"Enter the width: ";
	cin>>width;

	cout<<"Enter the height:";
	cin>>height;

	cout<<"Cost to produce the box is $"<<cost<<endl;
	cout<<"Price of the box is $"<<price<<endl;
	cout<<"The Area of the box is"  << surfaceArea << "sq. inches"<< endl;
		break;

	case 2:
	cout<<"Enter the length: ";
	cin>>length; 

	cout<<"Enter the width: ";
	cin>>width;

	cout<<"Enter the height:";
	cin>>height;

	cout<<"Cost to produce the box is $"<<cost<<endl;
	cout<<"Price of the box is $"<<price<<endl;
	cout<<"The Area of the box is"  << surfaceArea << "sq. inches"<< endl;
		break;

	case 3:
	cout<<"Enter the length: ";
	cin>>length; 

	cout<<"Enter the width: ";
	cin>>width;

	cout<<"Enter the height:";
	cin>>height;

	cout<<"Cost to produce the box is $"<<cost<<endl;
	cout<<"Price of the box is $"<<price<<endl;
	cout<<"The Area of the box is"  << surfaceArea << "sq. inches"<< endl;
	 break;

	case 4:
	cout<<"Enter the length: ";
	cin>>length; 

	cout<<"Enter the width: ";
	cin>>width;

	cout<<"Enter the height:";
	cin>>height;

	cout<<"Cost to produce the box is $"<<cost<<endl;
	cout<<"Price of the box is $"<<price<<endl;
	cout<<"The Area of the box is"  << surfaceArea << "sq. inches"<< endl;
		break;
	}


	while (type != 0);

	


return 0;
}
// David Shaw 
// Lab 3 - boxSurfaceArea Function


float boxSurfaceArea(float length, float width, float height) //function for area of box. 


{
	
	float surfaceArea = 2 * ( (length * width) 
					+ (width * height) 
					+ (length * height) );
	
	
	return surfaceArea;
}
// David Shaw
// Lab 3 - boxPrice Function


float boxPrice (int type, float area, float cost) //Function prototype

{
	
		switch (type) {

		case 1: 
			price = (cost + (.25 * area)); 
			break;

		case 2:
			price = (.50 * area) + 5.25;
			break;
		case 3: 
			price = cost + (.36 * area) + 7.50;
			break;

		case 4: 
			price = cost + (.32 * area) + 6.25;

			break;

		while (type != 0)
	}

	
	return price;
}
// David Shaw
// Lab 3 - boxCost Function


float boxCost (int type, float area)
{
	float cost = 0;

	switch (type) 
	{

	case 1: 
			cost = (.11 * area)//Wood cost
				+ (.09 * area) //Labor cost
				 + (2.25); //Hardware cost 
			break;

	case 2: 
			cost = (.13 * area)//Wood cost
				 + (.11 * area) //Labor cost
				 + (3.15); //Hardware cost 
			break;

	case 3: 
			cost = (.14 * area)//Wood cost
				 + (.26 * area) //Labor cost
				 + (4.00); //Hardware cost 
			break;
	case 4: 
			cost = (.17 * area)//Wood cost
				 + (.22 * area) //Labor cost
				 + (4.95);//Hardware cost 
			break; 


	}
	while (type != 0)

	return cost;
}

Any help would be greatly appreciated.

3
Contributors
3
Replies
6
Views
12 Years
Discussion Span
Last Post by DaveSS
1

Dave, you need to change your input loop and call your functions when you want to calculate area, cost and price. On cursory look, this is what I would recommend ...

// David Shaw
// Lab 3 - Functions
// 02/18/05

#include <iostream>
///#include <iomanip>  // not needed!

using namespace std;

float boxSurfaceArea (float, float, float);
float boxCost (int, float);
float boxPrice (int, float, float);


int main()
{
	// variables
	int type;
	float length;
	float width;
	float height;
	float surfaceArea;
	float cost;
	float price;
	
  type = 1;
	while (type > 0) 
	{
  	// User input
  	cout << "Enter zero to exit or ..." << endl;  // added instruction!!
  	cout<<"Enter a number, (1, 2, 3, or 4) to select a boxType:" << endl;
  	cin>>type;
  
    if (type == 0)  break; 
	
  	cout<<"Enter the length: ";
  	cin>>length; 

  	cout<<"Enter the width: ";
  	cin>>width;

  	cout<<"Enter the height:";
  	cin>>height;

  	surfaceArea = boxSurfaceArea(length, width, height); // call to function missing
  	cost = boxCost (type, surfaceArea);          // dito!
  	price = boxPrice (type, surfaceArea, cost);  // dito!
	
    cout<<"Cost to produce the box is $"<<cost<<endl;
  	cout<<"Price of the box is $"<<price<<endl;
  	cout<<"The Area of the box is "  << surfaceArea << " sq. inches"<< endl;
  	cout<<endl;   // one more new line, cosmetic! 
  }	// while loop
	
  return 0;
}

float boxSurfaceArea(float length, float width, float height) //function for area of box. 
{
	
	float surfaceArea = 2 * ( (length * width) 
					+ (width * height) 
					+ (length * height) );
	
	return surfaceArea;
}


float boxPrice (int type, float area, float cost) //Function prototype
{
  float price;    // price needs to be declared!
	
		switch (type) {

		case 1: 
			price = (cost + (.25 * area)); 
			break;

		case 2:
			price = (.50 * area) + 5.25;
			break;
		case 3: 
			price = cost + (.36 * area) + 7.50;
			break;

		case 4: 
			price = cost + (.32 * area) + 6.25;

			break;

		//while (type != 0)  // missing ; and what is this doing here?
	}

	return price;
}


float boxCost (int type, float area)
{
	float cost = 0;

	switch (type) 
	{

	case 1: 
			cost = (.11 * area)//Wood cost
				+ (.09 * area) //Labor cost
				 + (2.25); //Hardware cost 
			break;

	case 2: 
			cost = (.13 * area)//Wood cost
				 + (.11 * area) //Labor cost
				 + (3.15); //Hardware cost 
			break;

	case 3: 
			cost = (.14 * area)//Wood cost
				 + (.26 * area) //Labor cost
				 + (4.00); //Hardware cost 
			break;
	case 4: 
			cost = (.17 * area)//Wood cost
				 + (.22 * area) //Labor cost
				 + (4.95);//Hardware cost 
			break; 


	}
	while (type != 0)

	return cost;
}

Made a few corrections. You may want to put a check in for inputs that go past expected values, and give the user a hint what the dimensions are (inches etc.). Otherwise looks good, and runs well with Dev-C++!

1

Thanks for the help. Next time i will ask my teacher more and post errors if i still cant figure out a problem.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.