Need some guidance

Hello, I need some guidance on functions. I have this assignment to do and I've written all of it but the end result where it displays the length, width, and area is coming out in big numbers, and I'm not getting any errors from the compiler. So I'm not sure where I went wrong.

This is the question:

When it is complete, the program will ask the user to enter
the width and length of a rectangle and then display the rectangle’s area. The program
calls the following functions, which have not been written:
• getLength – This function should ask the user to enter the rectangle’s length and
then return that value as a double .
• getWidth – This function should ask the user to enter the rectangle’s width and then
return that value as a double .
• getArea – This function should accept the rectangle’s length and width as arguments
and return the rectangle’s area. The area is calculated by multiplying the
length by the width.
• displayData – This function should accept the rectangle’s length, width, and area
as arguments and display them in an appropriate message on the screen.

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

double getLength();
double getWidth();
double getArea(double, double);
double displayData(double, double, double);

int main()
{
    double length;
    double width;
    double area;

    cout << "This program calculates the area of a rectangle.\n" << endl;
    getLength();
    getWidth();
    getArea(length, width);
    displayData(length, width, area);
}

double getLength()
{
    double length;

    cout << "Enter the length of the rectangle" << endl;
    cin >> length;
    return length;
}

double getWidth()
{
    double width;

    cout << "Enter the width of the rectangle" << endl;
    cin >> width;
    return width;
}

double getArea(double length, double width)
{
    double area;

    return area = length * width;
}

double displayData(double length, double width, double area)
{
    cout << "The length of the rectangle is " << length << endl;

    cout << "The width of the rectangle is " << width << endl;

    cout << "The area of the rectangle is " << area << endl;
}


This is what outputs to the console:

This program calculates the area of a rectangle.

Enter the length of the rectangle
10
Enter the width of the rectangle
10
The length of the rectangle is 1.97671e-307 //*************************
The width of the rectangle is 1.97655e-307 // <-- My issue *
The area of the rectangle is 3.33743e+255 //***************************
Lines 11-13: These are uninitialized variables (garbage).

Line 16: You call getLength(), but you ignore the return value.

Line 17: ditto

Line 18: You call getArea() with garbage values. As before, you ignore the return value.

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




Well the reason I declared those variables and didn't initialize them is because I'm confused on that part. Where does the returned value get stored? How would I call it when I need it in the getArea() function?

I guess that's the whole reason for my mistakes on all the lines you mentioned :S
@AbtractionAnon,

well thank you for the help. I now understand where I went wrong and what I was missing. I'ts working perfectly now!

(-:
Topic archived. No new replies allowed.