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;
}
|