Building Lot Are with functions
Like this?
1 2 3 4
|
cout << "Please enter the width in feet (ie: 100): ";
cin >> width;
cout << "Thank you, please enter the length in feet (ie: 200): ";
cin >> length;
|
That's the idea. You have getLength() and getWidth() as separate functions so lines 1-2 would go in getWidth() and lines 3-4 would go in getLength().
so it's actually more like this then:
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
|
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes.
double getLength();
double getWidth();
double getArea(double length, double width);
void displayData(double length, double width, double area);
double getArea(double length, double width)
{
double area = length * width;
return 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();
cout << "Thank you, please enter the length in feet (ie: 200): ";
cin >> length;
// Get the rectangle's width.
width = getWidth();
cout << "Please enter the width in feet (ie: 100): ";
cin >> width;
// Get the rectangle's area.
area = getArea(length, width);
// Display the rectangle's data.
displayData(length, width, area);
return 0;
}
|
But I'm still getting this:
C:\Users\nichan79\AppData\Local\Temp\ccRNA1wf.o chapter 6 program.cpp:(.text+0x47): undefined reference to `getLength()'
C:\Users\nichan79\AppData\Local\Temp\ccRNA1wf.o chapter 6 program.cpp:(.text+0x7f): undefined reference to `getWidth()'
C:\Users\nichan79\AppData\Local\Temp\ccRNA1wf.o chapter 6 program.cpp:(.text+0x10a): 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\ccRNA1wf.o: bad reloc address 0x18 in section `.xdata'
C:\Users\nichan79\Desktop\collect2.exe [Error] ld returned 1 exit status
No, getLength() and getWidth() are separate functions.
// Delete lines 25-26 and 30-31
// After line 40:
41 42 43 44 45 46 47 48 49 50 51 52 53
|
double get_length()
{ double len;
cout << "Please enter the length in feet (ie: 200): ";
cin >> len;
return len;
}
double get_width ()
{ double wid;
cout << "Please enter the width in feet (ie: 100): ";
cin >> wid;
return wid;
}
|
Line 37: You have no implementation for displayData().
I got it now here's how I fixed the same thing. Thank all of you for helping!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
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;
}
|
Can you show the full completed code please?
Topic archived. No new replies allowed.