Going from using global variables to using references instead. Proofread, please?

This is what my program looks like using global variables:

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
#include <iostream>

using namespace std;

void SetupConsole(void);
void GetUserInformation(void);
int CalcSqFt(int nWidth, int nLength);
double CalcDownPayment(double dDownPercent, int dSqFt, double dPricePerSqFt);
void DisplayResult(double dDownPayment);

double dPricePerSqFt;
double dDownPercent;
int nWidth;
int nLength;

int main()
{
    SetupConsole();
    GetUserInformation();
    CalcSqFt(nWidth, nLength);
    int dSqFt = CalcSqFt(nWidth, nLength);
    CalcDownPayment(dDownPercent, dSqFt, dPricePerSqFt);
    double dDownPayment = CalcDownPayment(dDownPercent, dSqFt, dPricePerSqFt);
    DisplayResult(dDownPayment);

}

void SetupConsole(void)
    {
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);
    }

void GetUserInformation(void)
    {
        cout << "Hello, enter the price (in $) per sq foot: " << endl;
        cin >> dPricePerSqFt;

        cout << "Enter the width and length: " << endl;
        cin >> nWidth >> nLength;

        cout << "Enter the down payment percentage: " << endl;
        cin >> dDownPercent;

            while (dDownPercent < 20)
                {
                    cout << "Oops, that's too small! Enter something bigger: " << endl;
                    cin >> dDownPercent;
                }
            if (dDownPercent >= 20)
                {
                    return;
                }
    }

int CalcSqFt(int nWidth, int nLength)
    {
        return(nWidth * nLength);
    }

double CalcDownPayment(double dDownPercent, int dSqFt, double dPricePerSqFt)
    {
        return((dSqFt * dPricePerSqFt * dDownPercent) / 100);
    }

void DisplayResult(double dDownPayment)
    {
        cout << "Okay, you will need " << dDownPayment << " to place for a down payment" << endl;
    }


And this is what my program looks like using references:

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
#include <iostream>

using namespace std;

void SetupConsole(void);
void GetUserInformation(double &dDownPercent, int &nWidth,
                        int &nLength, double &dPricePerSqFt);
int CalcSqFt(int nWidth, int nLength);
double CalcDownPayment(double dDownPercent, int dSqFt, double dPricePerSqFt);
void DisplayResult(double dDownPayment);

int main()
{
    double dDownPercent;
    int nWidth;
    int nLength;
    double dPricePerSqFt;

    SetupConsole();
    GetUserInformation(dDownPercent, nWidth, nLength, dPricePerSqFt);
    CalcSqFt(nWidth, nLength);
    int dSqFt = CalcSqFt(nWidth, nLength);
    CalcDownPayment(dDownPercent, dSqFt, dPricePerSqFt);
    double dDownPayment = CalcDownPayment(dDownPercent, dSqFt, dPricePerSqFt);
    DisplayResult(dDownPayment);
}

void SetupConsole(void)
    {
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);
    }

void GetUserInformation(double &dDownPercent, int &nWidth,
                        int &nLength, double &dPricePerSqFt)
    {
        cout << "Hello, enter the price (in $) per sq foot: " << endl;
        cin >> dPricePerSqFt;

        cout << "Enter the width and length: " << endl;
        cin >> nWidth >> nLength;

        cout << "Enter the down payment percentage: " << endl;
        cin >> dDownPercent;

            while (dDownPercent < 20)
                {
                    cout << "Oops, that's too small! Enter something bigger: " << endl;
                    cin >> dDownPercent;
                }
            if (dDownPercent >= 20)
                {
                    return;
                }
    }

int CalcSqFt(int nWidth, int nLength)
    {
        return(nWidth * nLength);
    }

double CalcDownPayment(double dDownPercent, int dSqFt, double dPricePerSqFt)
    {
        return((dSqFt * dPricePerSqFt * dDownPercent) / 100);
    }

void DisplayResult(double dDownPayment)
    {
        cout << "Okay, you will need $" << dDownPayment;
        cout << " to place for a down payment." << endl;
    }


So, am I doing it right or am I missing something here? Please help!

Edit: proofread in the sense that I'm asking if my syntax look correct for using references. The program itself compiles and works, though!
Last edited on
proofread in the sense that I'm asking if my syntax look correct for using references. The program itself compiles and works, though!


You kind of just answered your own question there.

If there were syntax errors, the compiler would give you errors.

If you were using references incorrectly, the program wouldn't work as you expected.


Since it both compiles and works, that means you got the syntax and logic right. Congrats.


The only problem I see are lines 21 and 23:

1
2
3
4
    CalcSqFt(nWidth, nLength);  // <- this does nothing.  Get rid of it
    int dSqFt = CalcSqFt(nWidth, nLength);
    CalcDownPayment(dDownPercent, dSqFt, dPricePerSqFt);  // <- ditto
    double dDownPayment = CalcDownPayment(dDownPercent, dSqFt, dPricePerSqFt);



But those aren't "wrong" so much as they're just wasteful. You call a calculation function but aren't doing anything with the return value, so it's basically a wasted call.
@Disch: first, thank you for the reply. I was just making sure because the whole concept of references is something we just learned a day ago so I was just a little foggy on it.

And regarding the other topic you bring up, I thought I had to call the function and then once the function is called, it's stored in main. And since it's stored in main, I can now make something else equal to that function. Obviously, that's wrong since that's essentially what you're doing when you're making a variable equal to the calling of the function. Do I have that right?

Again, thank you very much.
C++ is not "functional" (like in math.)

The = operator is read as the assignment operator, (it does not quite set things equal.)
The operator literally executes the code on its right (in this case the function), then it stores that value into the variable on the left.

When you call the function as Disch pointed out on line 1 and 3, you are invoking it in void context. That is, it might as well be a void function as you are not utilizing its return value. Sometimes we want this, most often if the function has "side effects." But here your function only does calculation(s) and then returns a value; throwing away that value is a waste of CPU cycles. (Imagine that I ask you to do some calculation, then you ask if I want the answer, and I say "No...?")
Last edited on
@Mathhead200: thank you, that helped a lot!
For more info:

This is what C++ is: http://en.wikipedia.org/wiki/Imperative_programming
also often called: http://en.wikipedia.org/wiki/Procedural_programming

C++ is not this (but I suspect you were thinking this way): http://en.wikipedia.org/wiki/Declarative_programming
also often called: http://en.wikipedia.org/wiki/Functional_programming
Topic archived. No new replies allowed.