Functions Help - ASAP PLEASE

I'm trying to create a program that uses functions to determine the length, width then the final area of something but having some trouble. I run into the value being 0 in the end and not sure what I'm doing wrong! I have to assign the value 0 to the doubles as you will see but if I do not assign 0 it also doesn't run the program and states they are undeclared and it's quite frustrating I've tried everything, please point out my errors?

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

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

int main() {
	double length = 0;
	double width = 0;
	double area = 0;
	getLength(length);
	getWidth(width);
	getArea(area, length, width);
	displayData(length, width, area);
	return 0;
}
double getLength(double length) 
{
	cout << "Please enter the length\n";
	cin >> length;
	return length;
}
double getWidth(double width)
{
	cout << "Please enter the width\n";
	cin >> width;
	return width;
}
void displayData(double length, double width, double area) {
	cout << "Area: " << area << endl;
}
double getArea(double area, double length, double width)
{
	area = length * width;
	return area;
}
Last edited on
Your using the same names (length, width, area) in main as you are in your functions. It should work but it's going to be confusing to debug.
I see two issues:

1) Your functions return a value, but that value is not stored anywhere. Try to store the returned value in a variable.

1
2
3
length = getLength();
width = getWidth();
area = getArea(length, width);


2) Since the functions getLength() and getWidth() ask the user for the data, you don't need to pass any arguments. The functions can be updated as:

1
2
double getLength();
double getWidth();


Similarly, the arguments for getArea() and displayData() should be limited only to values those functions use. No need to pass extra data that the function doesn't utilize.
Last edited on
Topic archived. No new replies allowed.