Help with functions, please.

This is going to be somewhat of a long read, please bare with me.

First of all, these are the instructions to the program I'm supposed to make:
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
You will need a few things from the user. 
You should ask for the price per sq. foot. 
You should also ask for the dimensions of the house (length and width). 
You must then ask the user for a down payment percentage. 
The down payment must be at least 20%, if it's not, reject the input and ask the user to enter something a little higher
 (otherwise it would take forever to payoff)!

Here are the functions I want:

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

SetupConsole will force the console to display the down payment in normal currency form 
(i.e. showing exactly two digits after the decimal point).

GetUserInformation should ask the user for all the necessary details to 
calculate the down payment including the length and height of the house.
Note that you will have to use global variables to store the user information!
 
CalcSqFt should calculate the total square footage for the house.

CalcDownPayment should take the down payment percentage, the square footage, and the price per square foot. 
CalcDownPayment will then return the amount needed for the down payment.

DisplayResult should take the down payment and display it to the user. 


And so far, this is what my code looks like:

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
#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;
int nWidth;
int nLength;
double dDownPercent;

int main()
{
    SetupConsole(void);
    GetUserInformation(void);
    CalcSqFt(nWidth, nLength);

    cout << CalcSqFt << endl;
    return 0;
}

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;
    }

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

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


However, I have a couple of problems/questions:
(1) When I call the CalcSqFt function to display to me the nWidth x nLength, I always get an output value of 1 and I have no idea why.
(2) If you look closely at the functions I'm to use, you'll notice that CalcSqFt is supposed to be dSqFt and dDownPayment is supposed to be CalcDownPayment. How do I make them equal each other?
Last edited on
Since you are calling all the functions from the main function, all the variables, include ing nWidth and nLength should first be declared in the main function, not as global variables.

Then pass them by reference to GetUserInformation( &nWidth, &nLength), like so.

Then CAlcSqFt will return the value.

Also, if there are no parameters for a function, you do not need to put (void), just ()

Another thing, in your function prototypes, you do not need to put the variable names as the parameters. You only need to put the type. For example, CalcDownPayment(double, int, double). This is because functions can be called with more than one set of variables.

And make sure that the parameters listed in the prototypes match the function definitions.
Your problem here is in these two lines:


1
2
3
CalcSqFt(nWidth, nLength);

cout << CalcSqFt << endl;


These should be abbreviated into a single call:


cout << CalcSqFt(nWidth, nLength) << endl;

The whole idea of a function is to use a function call as if it were a variable.

Xen
@TheJJJunk: thank you for the tips. However, as I'm just following the guidelines my professor has set out for us, I don't really want to mess with them just yet.

@Xenphibian, thank you! That did the trick.

However, I now have another problem. In the function double CalcDownPayment(double dDownPercent, int dSqFt, double dPricePerSqFt);, dSqFt should have the value of int CalcSqFt(int nWidth, int nLength);. How do I make it so?
I'm not sure if I'm making myself clear, so I'm going to try to phrase this again: how do I make the value of dSqFt from the function ouble CalcDownPayment(double dDownPercent, int dSqFt, double dPricePerSqFt); equal to the value of the function int CalcSqFt(int nWidth, int nLength);?
This is what I'm doing to try and make them the same:
int dSqFt = CalcSqFt(nWidth, nLength);

However, when I double check on the console for the answer to be the same, following this code:
1
2
cout << CalcSqFt(nWidth, nLength) << endl;
    cout << dSqFt << endl;


I'll get the proper answer for the 1st cout, but the 2nd cout will always be 0. Help?
I have everything figured out, except how to make void DisplayResult(double dDownPayment); show the user what his down payment should be.

Here's the final code, with that bit of a problem:
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);
    cout << "Okay, you will need " << DisplayResult << " to place for a down payment" << endl;

}

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)
    {
        return(dDownPayment);
    }
Nevermind, figured it out.

For those of you who are curious as to what the final product looks like:

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;
    }
Topic archived. No new replies allowed.