A new house has been constructed in the shape of a rectangle and its backyard needs to be fenced. We need a program that will calculate the following data: area and perimeter of the house, perimeter of the fence, and the total cost of fencing.
The fence is 1.5 times the length and 1/4 times the width.
Fence can only be purchased in panels. Each panel is 8 feet wide and is not sold in fractional units. Therefore, if we need ¼ of a panel, we must purchase one whole panel. When the program is run, the user will enter the length and width of the house, and also the cost of each panel.
Here's what my instructor gave me to help guide me through it
#include
… using …
// Function prototypes
void getData(double& length, double& width, double& panelCost);
// This function is in charge of receiving all input from the user. It will read the dimensions of a
// rectangular house and also the cost of each panel of fencing.
double houseArea(double length, double width);
// Receives the dimensions of a rectangular house and returns its area.
double housePerimeter(double length, double width);
// Receives the dimensions of a rectangular house and returns its perimeter.
double fencePerimeter(double length, double width);
// Receives the dimensions of a rectangular house and returns the total number of feet of fencing
// required for the backyard.
void fencingCost(double length, double width, double panelCost, int& panels, double& cost);
// Receives the dimensions of a rectangular house and also the cost of each panel.
// The function calculates the number of panels needed and also the cost of fencing.
int main()
{
double length, width, area, perimeter; // Data for the house double fencePerim, panelCost, fenceCost; // Data for the fence int panels;
getData(length, width, panelCost); // Input the required data
area = houseArea(length, width); // Calculate area of the house
perimeter = housePerimeter(length, width); // Calculate perimeter of the house
fencePerim = fencePerimeter(length, width); // Calculate how many feet of fencing is required
// Calculate the number of panels and cost of the fence fencingCost(length, width, panelCost, panels, cost);
include <iostream>
include <cmath>
using namespace std;
void getData (double& length, double& width, double& panelCost);
// This function is in charge of receiving all input from the user. It will read the dimensions of a
// rectangular house and also the cost of each panel of fencing.
double houseArea (double length, double width);
// Receives the dimensions of a rectangular house and returns its area.
double housePerimeter (double length, double width);
// Receives the dimensions of a rectangular house and returns its perimeter.
double fencePerimeter (double length, double width);
// Receives the dimensions of a rectangular house and returns the total number of feet of fencing
// required for the backyard.
void fencingCost (double length, double width, double panelCost, int& panels, double& cost);
// Receives the dimensions of a rectangular house and also the cost of each panel.
// The function calculates the number of panels needed and also the cost of fencing.
int main()
{
double length, width, area, perimeter; // Data for the house
double fencePerim, panelCost, fenceCost; // Date for the fence
int panels;
getData (length, width, panelCost); // Input the required data
area = houseArea (length, width); // Calculate area of the house
perimeter = housePerimeter (length, width); // Calculate perimeter of the house
fencePerim = fencePerimeter (length, width); // Calculate how many feet of fencing is required
panels (length, width, fencePerimeter); // Calculate the number of panels and cost of the fence
fencingCost (length, width, panelCost, panels, cost);
cout << "Area of the house = ";
cin >> houseArea;
cout << "Perimeter of the house = ";
cin >> housePerimeter;
cout << "Perimeter of the fence = ";
cin >> fencePerimeter;
cout << "Number of panels needed = ";
cin >> panels;
cout << "Total cost of fencing = ";
cin >> fencingCost;
First of all, you should use code tags. It makes code more readable. To do this highlight your code, and click the first button under the format menu.
Second, it would help to know what the errors are and where they occur.
prog6.cpp:16: error: expected constructor, destructor, or type conversion before â<â token
prog6.cpp: In function âint main()â:
prog6.cpp:52: error: âpanelsâ cannot be used as a function
prog6.cpp:53: error: âcostâ was not declared in this scope
prog6.cpp:55: error: âcoutâ was not declared in this scope
prog6.cpp:56: error: âcinâ was not declared in this scope
prog6.cpp: In function âvoid getData(double&, double&, double&)â:
prog6.cpp:74: error: âcinâ was not declared in this scope
prog6.cpp: In function âdouble houseArea(double, double)â:
prog6.cpp:81: error: assignment of function âdouble houseArea(double, double)â
prog6.cpp:81: error: cannot convert âdoubleâ to âdouble ()(double, double)â in assignment
prog6.cpp: In function âdouble housePerimeter(double, double)â:
prog6.cpp:86: error: assignment of function âdouble housePerimeter(double, double)â
prog6.cpp:86: error: cannot convert âdoubleâ to âdouble ()(double, double)â in assignment
prog6.cpp: In function âdouble fencePerimeter(double, double)â:
prog6.cpp:91: error: assignment of function âdouble fencePerimeter(double, double)â
prog6.cpp:91: error: cannot convert âdoubleâ to âdouble ()(double, double)â in assignment
prog6.cpp: In function âvoid fencingCost(double, double, double, int&, double&)â:
prog6.cpp:96: error: invalid operands of types âdouble ()(double, double)â and âintâ to binary âoperator/â
prog6.cpp:97: error: assignment of function âvoid fencingCost(double, double, double, int&, double&)â
prog6.cpp:97: error: cannot convert âdoubleâ to âvoid ()(double, double, double, int&, double&)â in assignment
The easiest errors can be solved the first two lines. Your included header files should be preceded by #.
Also, in the fencingCost function, you're using a variable called "cost" which you haven't declared.
Another problem is that you are trying to call the panels function when you don't have a prototype or implementation for it. Try to fix these. You'll still have some errors, but it will be easier to get the rest after your code is cleaned up a bit. Repost you're code after you've fixed errors.
Correct all of your functions which return a double value in this way. This is a very basic error. Do you have a textbook with examples of functions in it?
In your function fencingCost:
1 2 3 4 5
void fencingCost (double length, double width, double panelCost, int& panels, double& cost)
{
panels = fencePerimeter / 8;// you need to call the function fencePerimeter() here.
fencingCost = panelCost * panels;// cost is supposed to be the value being calculated here.
}
These corrections should greatly reduce the # of errors in your program. Good luck in your studies.
Thank you guys so much for your help! I've gone through and deleted/added a lot to my code and I think I'm pretty much done with it. Only problem is..even though I'm sure this is a horrible rookie mistake...cannot figure out how to declare cost. I know fenceCost must be tied to it in some way, but can't figure out how. Or why I couldn't just use fenceCost instead of cost. Here are the codes I think are important to declaring cost.
fenceCost = fencingCost (length, width, panelCost, panels, cost);
cost = fenceCost;
// Calculate the number of panels and cost of the fence
Well just to be safe, here's my whole code so far.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{ double length, width, area, perimeter; // Data for the house double fencePerim, panelCost, fenceCost; // Date for the fence
int panels;
getData (length, width, panelCost); // Input the required data
cout << "Enter the length of the property in ft: ";
cin >> length;
cout << "Enter the width of the property in ft: ";
cin >> width;
cout << "Enter the cost of each panel of fence: ";
cin >> panelCost;
area = houseArea (length, width); // Calculate area of the house
perimeter = housePerimeter (length, width); // Calculate perimeter of the house
fenceCost = cost;
fencingCost (length, width, panelCost, panels, cost);
// Calculate the number of panels and cost of the fence
cout << "Area of the house = " << area << endl;
cout << "Perimeter of the house = " << perimeter << endl;
cout << "Perimeter of the fence = " << fencePerim << endl;
cout << "Number of panels needed = " << panels << endl;
cout << "Total cost of fencing = " << fenceCost << endl;
You still need to keep the function prototypes above the main function. Your code tags weren't done right either. You need to highlight your entire code and click the format button that looks like this:
<>
This might just be a typo in your post, but you need a closing brace for the fencingCost function.