Finding Area and Perimeter w/ Functions

I've been stumped on this for a little bit now, error code keeps saying that "'display': function does not take 2 arguments".

#include <iostream>
using namespace std;
double getInput(double l, double w);
double calcAreaPerimeter();
void display();
int main()
{
double length, width, area, perimeter;
getInput(length, width);
calcAreaPerimeter();
display();
system("pause");
return 0;
}

double getInput(double l, double w)
{
double len, wid;
cout << "Enter the length: " << endl;
cin >> len;
cout << "Enter the width: " << endl;
cin >> wid;
return len,wid;
}

double calcAreaPerimeter()
{
double len, wid, per, are;
per = ((len * 2) + (wid * 2));
are = (len * wid);
return per, wid;
}

void display()
{
cout << "The area is " << area << " and the perimeter is " << perimeter << endl;
}
Hello MattDaRatt,

While I look over your code:


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Andy
Hello MattDaRatt,

In "main" you define double length{}, width{}, area{}, perimeter{};
First you should ALWAYS initialize your variables.

Looking at your first function you define double getInput(double l, double w), But never use "l" and "w" in the function. Also try to avoid the letters lower case "L" and "O". In some type styles they can easile be mistaken for the numbers (1) and (0). You can very easily use: double getInput(double length, double width).

Your return statement return len, wid; does not work. A function can only return 1 thing. The exceptions would be a vector or other container class or a tuple, but I doubt that you have studied tuples yet.

What you are most likely returning is just "wid".

For the "getInput" function you could use void getInput(double& length, double& width). Passing the variables by reference will change these values back in "main". Then there would be no reason for a return value.

The function "calcAreaPerimeter" has the same problems. The difference is that the function should take 4 parameters. "lenght" and "width" can pe passsed by value, but "area" and "perimeter" would need to be passed by reference. You could also make this a void function.

For:
1
2
3
4
void display()
{
    cout << "The area is " << area << " and the perimeter is " << perimeter << endl;
}

"area" and "perimeter" are never defined anywhere. When "main" looses scope and the function gains scope it does not know about the variables defined in "main" The function needs 2 parameters to work.

Andy
There are other issues.

1
2
3
4
5
6
7
double calcAreaPerimeter()
{
double len, wid, per, are;
per = ((len * 2) + (wid * 2));
are = (len * wid);
return per, wid;
}


No.

This function can only return one value of type double - not 2 are you are trying to do. The same with getInput(). For a function to return more than one value, consider passing via a ref.

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
#include <iostream>
using namespace std;

void getInput(double&, double&);
void calcAreaPerimeter(double, double, double&, double&);
void display(double, double);

int main()
{
	double length {}, width {}, area {}, perimeter {};

	getInput(length, width);
	calcAreaPerimeter(length, width, perimeter, area);
	display(perimeter, area);
}

void getInput(double& len, double& wid)
{
	cout << "Enter the length: ";
	cin >> len;

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

void calcAreaPerimeter(double len, double wid, double& per, double& are)
{
	per = ((len * 2) + (wid * 2));
	are = (len * wid);
}

void display(double perimeter, double area)
{
	cout << "The area is " << area << " and the perimeter is " << perimeter << '\n';
}

Topic archived. No new replies allowed.