Void Functions

I have to use void functions for my class. I'm wondering why my output isn't coming out correctly? I keep getting 75 for each line of output. Your points depends on the user monthly purchase amount. The math is in void functions below.

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

void getStandard(double total);
void getPlus(double total);
void getPremium(double total);

int main()
{
    //variable
    double userTotal;
    
    //ask for user input
    cout << fixed << setprecision(2) << endl;
    cout << "Please enter your total monthly purchase amount ($0.00): ";
    cin >> userTotal;
    //get totals for each membership
    cout << fixed << setprecision(0) << endl;
    getStandard(userTotal);
    cout << "Standard membership points this month: " << userTotal << endl;
    getPlus(userTotal);
    cout << "Plus membership points this month: " << userTotal << endl;
    getPremium(userTotal);
    cout << "Premium membership points this month: " << userTotal << endl;
}
void getStandard(double total)
{
    double standardTotal;

    if (total < 75)
    {
        standardTotal = total * .05;
    }
    if (total >= 75 && total <= 149.99)
    {
        standardTotal = total * .075;
    }
    else
    {
        standardTotal = total * .10;
    }
}
void getPlus(double total)
{
    double plusTotal;

    if (total < 150)
    {
        plusTotal = total * .06;
    }
    if( total >= 150)
    {
        plusTotal = total * .13;
    }
    
}
void getPremium(double total)
{
    double premiumTotal;

    if (total < 200)
    {
        premiumTotal = total * .04;
    }
    if (total >= 200)
    {
        premiumTotal =total * .15;
    }

}
You are making your calculations with locally scoped variables in each function that go out of scope when each function returns. userTotal retains the same value in main whether you call your functions or not.
Hello Awak3nDreams,

Since you have to use "void" functions I get the idea that the assignment is about passing by reference.

Therefor the variables "standardTotal", "plusTotal" and "premiumTotal" should be defined in "main" and passed by reference so that the variables in "main" will change and you can use those variables in the "cout" statements insted of "userTotal" which never changes.

Instead of using magic numbers in the functions, (0.4, 0.15) ets., define constant variables for these. You can either define them in the function or as global variables to the whole file. I would consider putting at the top of the code where they can easily be found if a change is needed.

Andy
Hello Awak3nDreams,

You did not offer any test data with expected output, so I am not sure if everything is working correctly, but I did manage this output:

Please enter your total monthly purchase amount ($0.00): 100

Standard membership points this month: 8
Plus membership points this month: 6
Premium membership points this month: 4


 Press Enter to continue:



Please enter your total monthly purchase amount ($0.00): 150

Standard membership points this month: 15
Plus membership points this month: 20
Premium membership points this month: 6


 Press Enter to continue:



Please enter your total monthly purchase amount ($0.00): 200

Standard membership points this month: 20
Plus membership points this month: 26
Premium membership points this month: 30


 Press Enter to continue:



Please enter your total monthly purchase amount ($0.00): 225

Standard membership points this month: 23
Plus membership points this month: 29
Premium membership points this month: 34


 Press Enter to continue:


Is this what you are looking for?

Andy
That is the output I'm looking for!
Hello Awak3nDreams,

To give you a hint: Everything you need is already there you just need to use it differently.

In "main":
1
2
3
getStandard(userTotal, standardTotal);

cout << "Standard membership points this month: " << standardTotal << '\n';  // <--- Not "userTotal". This variable never changes. 


To give you an idea. After the prototypes I added:
1
2
3
constexpr double FIVE_PERCENT{ 0.05 };
constexpr double SEVEN_AND_A_HALF_PERCENT{ 0.075 };
constexpr double TEN_PERCENT { 0.10 };

And in the function "getStandard" I used them this way:
1
2
3
4
5
6
7
8
9
10
11
12
if (total < 75)
{
    standardTotal = total * FIVE_PERCENT;
}
if (total >= 75 && total <= 149.99)
{
    standardTotal = total * SEVEN_AND_A_HALF_PERCENT;
}
else
{
    standardTotal = total * TEN_PERCENT;
}

Now if you need to make a change there is only 1 place that needs changed. The names can be changed to anything you like. Just a suggestion.

Andy
As an example of using reference:

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

void getStandard(double total, double& standard);

int main()
{
	double userTotal {}, standard {};

	cout << fixed << setprecision(2) << endl;
	cout << "Please enter your total monthly purchase amount ($0.00): ";
	cin >> userTotal;

	getStandard(userTotal, standard);

	cout << fixed << setprecision(0);
	cout << "\nStandard membership points this month: " << standard << '\n';
}

void getStandard(double total, double& standardTotal)
{
	if (total < 75)
		standardTotal = total * .05;
	else if (total >= 75 && total <= 149.99)
		standardTotal = total * .075;
	 else
		standardTotal = total * .10;
}

I got it. Thanks guys!
Registered users can post here. Sign in or register to post.