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