Building Lot Are with functions

Pages: 12
I was wondering if this looks about right? I'm starting to get the hang of C++ but I need someone to double check my code.

Specifications of the program:
• Interactively input the width and length in feet using a function called from
main().
• Compute the area of the lot in acres using a separate function. This function can
be designed as a void function with reference parameters or with value
parameters and a return statement. This is your choice.
• The results must be displayed from the main() function.

Here is the code I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream> 

using namespace std; 

int calculate(int width,int length) 

{ 
int areaFeet; 
int areaAcres; 
areaFeet = width * length; 
areaAcres = areaFeet / 43560; 
return areaAcres; 
} 

int main()
[code]
{
int width;
int length;
cout << "Please enter the width in feet\n";
cin >> width;
cout << "Thank you, please enter the length in feet\n";
cin >> length;
float area;
area = calculate(width,length);
cout << "The area of the lot is " << area;
cin << width;
return 0;
}[/code]


Here is the newest code I've done for this still getting error messages (posted after new code):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream> 
using namespace std; 

// Function prototypes. 
double getLength(); 
double getWidth(); 
double getArea(); 
void displayData(double length, double width, double area); 

int main() 
{ 
double length, // The rectangle's length 
width, // The rectangle's width 
area; // The rectangle's area 

// Get the rectangle's length. 
length = getLength(); 

// Get the rectangle's width. 
width = getWidth(); 

// Get the rectangle's area. 
area = getArea(length, width); 

// Display the rectangle's data. 
displayData(length, width, area); 

return 0; 
} 


double getArea(double length, double width) 
{ 
double area = length * width; 
return area; 
}


here are the error messages:

C:\Users\nichan79\Desktop\rectangle area.cpp In function 'int main()':
24 29 C:\Users\nichan79\Desktop\rectangle area.cpp [Error] too many arguments to function 'double getArea()'
8 8 C:\Users\nichan79\Desktop\rectangle area.cpp [Note] declared here


Because of a few requests for the finished code I decided to place it here. I hope it helps others to design their own version of it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <cstdlib>
using namespace std;

//Function prototypes
double getLength();
double getWidth();
double getArea( double length, double width );
void displayData(double length, double width, double area);

//Program begins with a main function
int main()
{
//Declare variables
double length, // The rectangle's length
width, // The rectangle's width
area; // The rectangle's area

// Get the rectangle's width.
width = getWidth();
  // Get the rectangle's length.
length = getLength();


// Get the rectangle's area.
area = getArea( length, width );

// Display the rectangle's data.
displayData(length, width, area);
  
//Pause the system for a while
system("pause");
return 0;
}
//Function definition to getWidth
double getWidth()
{
cout<<"------------------------------"<<endl;
cout<<"Building lot area calculator"<<endl;
cout<<"------------------------------"<<endl;
//Prompt and read input
cout << "Please enter the width in feet(example,100): ";
double width;
cin >> width;
return width;
}

double getLength()
{
cout << "Enter length in feet: ";
double length;
cin >> length;

return length;
}
double getArea( double length, double width )
{
// Converts square feet to acres
return ((length * width)/43560);  
}

//Display output

void displayData(double length, double width, double area)

{

cout<<"A building lot of "<<width<<" feet by "<<length<<" feet has "<<area<<" acres."<<endl;

}
Last edited on
Your assignment requires two functions other than main but you are missing the first one.

Also, please be sure to surround your code so that it is [code]between code tags[/code] to give it syntax highlighting and line numbers.
Last edited on
is there a quick way to fix it?
Yes, just reread the first bullet point of the assignment.
ok I can do the width and length as separate functions or have the area as a separate function?
Last edited on
I don't see where the following is being done:
• Interactively input the width and length in feet using a function called from main().


Also please use code tags when posting code.
Last edited on
code tags?
I can do the width and length as separate functions right?

Yes.

calculate returns an int and areaAcres is also an int. This means the calculation of areaAcres is going to be truncated.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
Hint: You can edit your previous post, highlight your code and press the <> formatting button.

When you post a message, look to the right of the text box where you type. There are several format buttons. Highlight your code and clock the <> button to add code tags.

Your calculate function won't return the right answer because it's returning an int instead of a float. Also the calculations are done as integers. You need to return a float and be sure that the division is done in floating point also:
1
2
3
4
5
6
float calculate(int width,int length) {
int areaFeet;
float areaAcres;
areaFeet = width * length;
areaAcres = areaFeet / 43560.0;
return areaAcres;

}
Thanks I'l be back after lunch to show my finished code. Thanks
Last edited on
i can't get it to work with two extra functions that are called within the main function.
Not sure what you have at this point.
If you want furhter help, please post your current code.
it's pretty much the same way it was before because every time I try changing the code I end up with error messages. It possibly could be my compiler.
Doubtful that it's your compiler.

The code is not "pretty much the same" if you now have two functions per your previous post.

Post your current code and the exact error messages you're getting.
ok I edited my original post with what I am currently working on
At line 7 you declare getArea() as a function taking no parameters, but it reality it takes 2. Change line 7 to double getArea(double length, double width);
dhayden, I tried that and the error messages are this:

C:\Users\nichan79\AppData\Local\Temp\ccGRKcik.o chapter 6 program.cpp:(.text+0xe): undefined reference to `getLength()'
C:\Users\nichan79\AppData\Local\Temp\ccGRKcik.o chapter 6 program.cpp:(.text+0x20): undefined reference to `getWidth()'
C:\Users\nichan79\AppData\Local\Temp\ccGRKcik.o chapter 6 program.cpp:(.text+0x85): undefined reference to `displayData(double, double, double)'
c:\program files (x86)\dev-cpp\mingw64\x86_64-w64-mingw32\bin\ld.exe C:\Users\nichan79\AppData\Local\Temp\ccGRKcik.o: bad reloc address 0x0 in section `.pdata'
C:\Users\nichan79\Desktop\collect2.exe [Error] ld returned 1 exit status


Man I completely goofed this one up I am supposed to have :

Interactive input of width and length NOT done using a function called
from main()
b. Area calculation must be done using a separate function. NOTE: this
function must return its results somehow to the main() function
c. Code is a chapter 2 solution, that is, without functions
d. The conversion from square feet to acres

----------------------------------------------
Building lot area calculator
----------------------------
Please enter the width in feet (example, 100): 400
Enter length in feet: 300
A building lot of 400 feet by 300 feet has 2.75482 acres.
Last edited on
That's because the code you posted calls getLength(), getWidth() and displayData(), but it doesn't define them. You need to add those functions.
So all I need to do is simple cout's for each function and I should have it?
So all I need to do is simple cout's for each function and I should have it?

No, you need to do cin if you want to get input from the user.
cout is a good idea of course to tell the user what to input.
Pages: 12