function call issue?

Feb 24, 2016 at 1:36am
Program is supposed to calculate shipping(using void functions for both standard and premium memberships) and then display total cost. I keep getting 0 for the shipping. Can someone help me figure out how to get this baby working?


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


//function prototypes
void displayStandard(double sub, double shipCost);
void displayPremium(double sub, double shipCost);

int main()
{
	double subTotal = 0.0;
	char membership = ' ';
	double ship = 0.0;

	cout << "(A) Standard" << endl;
	cout << "(B) Premium" << endl;
	cout << "Enter your membership (A or B): " << endl;
	cin >> membership;

	while (toupper(membership) != 'A' && toupper(membership) != 'B')
	{
		cout << "Please enter A or B: " << endl;
		cin >> membership;
	}

	
		cout << "Please enter subtotal: " << endl << endl;
		cin >> subTotal;

		membership = (toupper(membership));

		if (membership == 'A')
		{
			displayStandard(subTotal, ship);

			cout << fixed << setprecision(2);
			cout << "Standard shipping:  " << ship << endl;
			cout << "Your total is: " << ship + subTotal;
		}

		else
		{

			displayPremium(subTotal, ship);

			cout << fixed << setprecision(2);
			cout << "Premium shipping: " << ship << endl;
			cout << "Your total is: " << ship + subTotal;
		}

	




	system("pause");
	return 0;
}//end of main

//*****Function Definitions*****

void displayStandard(double sub, double shipCost)
{
	double shipping = 0.0;
	
	if (sub <= 100)
		shipping = 12.99;
	else
		shipping = 4.99;
}

void displayPremium(double sub, double shipCost)
{
	double shipping = 0.0;
	
	if (sub <= 49.99)
		shipping = 4.99;
	else
		shipping = 0;
}
Feb 24, 2016 at 1:44am
Line 68,70: shipping is a local variable. It goes out of scope (is lost) when the function exits.

You really want to modifying shipCost in the caller. To do that though, you have to pass shipCost by reference, not by value as you're doing.
Feb 24, 2016 at 1:48am
Thanks! got it

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
//*****Function Definitions*****

void displayStandard(double sub, double &shipCost)
{
	double shipping = 0.0;
	
	if (sub <= 100)
	{
		shipping = 12.99;
		shipCost = shipping;
	}
	else
	{
		shipping = 4.99;
		shipCost = shipping;
	}
}

void displayPremium(double sub, double &shipCost)
{
	double shipping = 0.0;
	
	if (sub <= 49.99)
	{
		shipping = 4.99;
		shipCost = shipping;
	}
	else
		shipping = 0;
}
Last edited on Feb 24, 2016 at 1:49am
Feb 24, 2016 at 2:01am

If i wanted the displayPremium to return FREE! rather than 0 for the else, what would need to be done?

1
2
3
4
5
6
7
8
9
10
11
12
void displayPremium(double sub, double &shipCost)
{
	double shipping = 0.0;
	
	if (sub <= 49.99)
	{
		shipping = 4.99;
		shipCost = shipping;
	}
	else
		shipCost = 0;
}
Last edited on Feb 24, 2016 at 2:01am
Feb 24, 2016 at 2:13am
1
2
3
4
5
6
7
8
9
10
11
12
void displayPremium(double sub, double &shipCost)
{
	double shipping = 0.0;
	
	if (sub <= 49.99)
	{
		shipping = 4.99;
		shipCost = shipping;
	}
	else
		cout << "FREE!" << endl;
}
Feb 24, 2016 at 2:24am
Thanks DeathLeap but it would still have premium shipping 0.00

I had to add an else if in the main:
1
2
3
4
5
else if (membership == 'B' && subTotal > 49.99)
		{
			cout << "Premium shipping: " << "FREE!" << endl;
			cout << "Your total is: " << subTotal << endl;
		}
Topic archived. No new replies allowed.