FInding Rectangle area with this method.

I need help here. I didn't get the right output. I am trying to make program using this method but it didn't return right output. Anything wrong here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
include <iostream>
using namespace std;
void count (float length, float width, float area);
int main()
{
	float length, width, area;
		cout << "Enter length: ";
	           cin >> length;
		cout << "Enter width: ";
		   cin >> width;
		cout << "Area of rectangle: " << area;
	return 0;
}

 void count (float length, float width, float area)
{
	area = length * width;
}
	
yes.
you never actually call the function count.
the function count is misnamed. That is ok but why name a function that computes area 'count'. I mean I can name my function that computes the Fibonacci series 'factorial_generator' too, but why troll your fellow coders?
you have a local variable named area and a function variable named area and it is legal but easy to confuse them. Here, main.area has never been given a value, count.area has been assigned...
count is a void function and all the parameters are passed by value, so it won't ever DO anything (all work done in count is discarded).

it would work with these changes:

void count (float length, float width, float &area) //now area changes the input variable (for area only, not for length or width) due to the & (passed by reference). do this to the header and body of the function.

cout << "Area of rectangle: " << count(length, width, area);

Last edited on
Hi... you meant like this? Doesn't work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
void count (float length, float width, float& area);
int main()
{
	float length, width, area;
		cout << "Enter length: ";
	           cin >> length;
		cout << "Enter width: ";
		   cin >> width;
		cout << "Area of rectangle: " << count (length, width, area);
	return 0;
}

 void count (float length, float width, float& area)
{
	area = length * width;
}
	
Last edited on
Topic archived. No new replies allowed.