Doing a calculation in a function and calling it, wrong results

Why is the area and perimeter not being calculated properly?
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  #include <iostream>
#include <fstream>

using namespace std;

void welcome(void);
void farewell(void);
void displayRectangle(double length, double width, double area, double perim);
void calculateRectangle(double length, double width, double area, double perim);
bool getRectangle(ifstream &inFile, double &length, double &width);

int main()
{
    ifstream inFile;
    double length, width;
	double area, perim;

    welcome();
    //inFile.open("rectngles.txt"); // <== incorect file name!
    inFile.open("rectangles.txt");
    if(!inFile)
    {
        cout << "\a\a~*~ ERROR opening the input file! ~*~\n";
        return 1; // or you could use exit(1);
    }
    while (getRectangle(inFile, length, width))
    {
        calculateRectangle(length, width, area, perim);
        displayRectangle(length, width, area, perim);
    }
    inFile.close();
    farewell();

    return 0;
}

/**~*~*
 This function displays general information
 about the program
*~**/
void welcome(void)
{
    cout << "WELCOME to the RECTANGLE calculator!\n\n"
         << "This program will output the\n"
         << "\tperimeter and\n"
         << "\tarea\n"
         << "of several rectangles.\n\n";
    return;
}

/**~*~*
 This function displays the end-of-the-program
 message
*~**/
void farewell(void)
{
    cout << "\n\n"
         << "\t ~~*~~ The END ~~*~~ \n\n"
         << "\t        ~~*~~ \n"
         << "\t      Thank you\n\tfor using my program!\n";
    return;
}

/**~*~*
 This function calculates
 the area and the perimeter of a rectangle
 *~**/
void calculateRectangle(double length, double width, double area, double perim)
{
    perim = 2 * (length + width);
    area  = length * width;
    return;
}

/**~*~*
 This function displays the length and the width of a rectangle
 and its area and perimeter
*~**/
void displayRectangle(double length, double width, double area, double perim)
{
    cout << "\nRESULTS: ";
    cout << "\tLength = " << length;
    cout << ", Width  = " << width  << endl;
    cout << "\t\tPerimeter = " << perim << endl;
    cout << "\t\t     Area = " << area << endl;
    return;
}

/**~*~*
 This function reads the length and the width of a rectangle
 from a file. It returns true in case of success, false otherwise.
*~**/
bool getRectangle(ifstream &inFile, double &length, double &width)
{
    return inFile >> length >> width;
}


My problem is (I think) with the calculateRectangle function. The area and perimeter are completely random each time I run the program, like if it was using random floats and I can't figure out why.
The file rectangles.txt looks like this:
10 20
10 5
2 100
30 50
9 7
2 3
9 9
Any hints/tips would be appreciated.
Last edited on
In calculateRectangle(...) perim and area are local variables not visible outside of the function. If you want to modify those variable you need to pass them as reference:
1
2
3
4
5
void calculateRectangle(double length, double width, double &area, double &perim) // Note: &
// Note 2: change the prototype as well
{
...
}
In C and C++, the parameters are passed by value. That means the arguments you passed to the functions are just copies, no matter what you did in the function, the original value won't change.

However, in C you could pass a pointer, so that you could change the value of the pointer points to. In C++, besides pointer, you could use a reference, that will give you the power to change the original value.
Topic archived. No new replies allowed.