Returns from Function

I am new to c++ and need to write a program using multiple functions that will essentially carry out basic cash register functions. Below is the code I have so far. The biggest issue I am seeing right now is that in sum_prices, it correctly calculates the total, but then when it goes into find_discount it always says the discount is 0. Does this mean the total is not returning correctly? How would I get the value of total to return to be used in the later functions? Thanks in advance for any help!

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <cstdlib>
#include <iostream>
#include <iomanip>

char find_choice();
int credit_prompt();
int get_prices();
double sum_prices(int num_prices);
double find_discount(double total);
double discount_amt(double discount_dec, double total);

using namespace std;

int main()
{
	
	find_choice();
	get_prices();


}

char find_choice()
{
	char payment;
	
	cout << "Welcome to Buy Alot\nType of Payment is A-Cash or B-Credit Card\nEnter A or B and hit ENTER to accept:" << endl;
	cin >> payment;

	if(payment == 'A' || payment == 'a')
		cout << "You chose to pay with cash.\n";
	else if(payment == 'B' || payment == 'b')
		credit_prompt();
	else
		cout << "Invalid payment type.\n";

	return payment;
}

int credit_prompt()
{
	int cc_num;
	
	cout << "Enter a 5 digit credit card number.\n";
	cin >> cc_num;
	if(cc_num > 99999 || cc_num < 10000)

	{

		cout << "Invalid card number entered.\n";

		return 0;

	}
	return cc_num;
}

int get_prices()
{
	int num_prices;
	cout << "Enter how many prices to sum:\n";
	cin >> num_prices;
	if(num_prices < 0)

	{

		cout << "Invalid number of prices.\n";

		return 0;

	}

	sum_prices(num_prices);
	return num_prices;
}

double sum_prices(int num_prices)
{
	double price;
	double total;

	cout << "Enter " << num_prices << " prices: \n";
	for(int i = 0; i < num_prices; i++)
	{
		cin >> price;
		total += price;
	}
	cout << "Total: " << total;
	find_discount(total);
	return total;
}

double find_discount(double total)
{
	double discount_dec;
	double discount_per = discount_dec*100;

	if(total >= 100 && total < 1000)

	{

		discount_dec = 0.02;
		cout << "Percent: " << discount_per;

	}

	else if(total >= 1000 && total < 10000)

	{

		discount_dec = 0.05;
		cout << "Percent: " << discount_per;

	}

	else if(total >= 10000)

	{

		discount_dec = 0.10;
		cout << "Percent: " << discount_per;

	}

	else

	{

		discount_dec = 0;
		cout << "Percent: " << discount_per;

	}
	discount_amt(discount_dec, total);	
	return discount_per;
}

double discount_amt(double discount_dec, double total)
{
	double discount = 0;

	discount = total * discount_dec;
	cout << "Discount: " << discount;
	return discount;
}
Line 95: discount_dec is uninitialized (garbage).

Line 96: You're calculating discount_per based on discount_dec. Since discount_dec was uninitialized, the result is going to be garbage. GIGO = Garbage In, Garbage Out.

Line 133: You call discount_amt(), but ignore the return value.

Line 89: Ditto for find_discount.
Last edited on
Okay, so if i just do discount_dec = 0 during declaration, would that make the variable initialized and no longer garbage? Also, when you say I ignore the return value for discount_amt() and find_discount, does that mean I would just have to call the functions after the return? I'm having trouble understanding how functions work as a whole. I do not understand how to return the total in sum_prices and then use that value in future functions
line 80: total is not initialized.

line 20: main() should always return 0.

As an example of using return values, lets look at discount_amt. discount_amt does its calculation and returns discount at line 143. That's fine, except that at line 133 where it's called, you ignore the return value from the function. Line 133 should be written:
133
134
135
136
137
  double disc_amt;
  disc_amt = discount_amt (discount_dec, double total);
// ^^^^^^^^^^ assign the return value of the function to a variable
  total = total - disc_amt;
  cout << "Total after discount = " << total < endl;


At line 134, you probably want to be returning total rather than discount_per. You've already calculated the discount amount. You no longer need the percentage.

Each of the other places where you call a function that returns a value should follow the above pattern of assigning the return value to a variable.

A general note, you don't usually display values in calculation functions. A calculation function should do its work and return the result, leaving it to the caller to do what it wants with the result. That's not to say that couts in calculation functions aren't useful for debugging.

so if i just do discount_dec = 0 during declaration, would that make the variable initialized and no longer garbage?

Yes, however your calculation of discount_per is faulty. You do that once at line 96. If you initialize discount_dec to 0, discount_per will also be 0 as you never change it when you change discounct_dec.

I would rewrite find_discount as follows:
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//  Returns amount of discount
double find_discount(double total)
{   double discount_dec = 0.0;
	double discount_per;
    
	if(total >= 100 && total < 1000)
	    discount_dec = 0.02;
	else if(total >= 1000 && total < 10000)
	    discount_dec = 0.05;
	else if(total >= 10000)
	    discount_dec = 0.10;
	discount_per = discount_dec * 100.0;  // moved from line 96 so calculation is correct
	cout << "Percent: " << discount_per;  
	return discount_amt(discount_dec, total);	
}


Last edited on
Topic archived. No new replies allowed.