Basic Function Problem

New to C++... Taking my first introduction class... We're supposed to make a function that calculates a discount when X amount of items are purchased. For example, we're using meals...

Meal Type|Cost Each|Discount|Order More Than For Discount|
Breakfast_____5.50____10%_____________________20

The code is supposed to calculate all of these things and put it out in a nice output format.... Here is my code:

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

//Bunch of global identifiers

double calc_discount(int quantity, double cost_each, double discount, int more_than);

int breakfast, lunch, dinner;

const double break_costeach=5.50, break_disc=.10, lunch_costeach=9.50, lunch_disc=.15, dinner_costeach=16.50, dinner_disc=.12;

double break_cost, lunch_cost=lunch*lunch_costeach, dinner_cost=dinner*dinner_costeach, break_costAD=break_cost*break_disc, lunch_costAD=lunch_cost*lunch_disc, dinner_costAD=dinner_cost*dinner_disc, break_discount, lunch_discount, dinner_discount;
const int break_mt=10, lunch_mt=15, dinner_mt=8;

int main()
{

        break_cost=(breakfast*break_costeach);

        cout<<setiosflags(ios::fixed|ios::showpoint)
                <<setprecision(2);

        cout<<"Enter the number of breakfasts ordered."<<endl;
                cin>>breakfast;
                break_discount=calc_discount(breakfast, break_costeach, break_disc, break_mt);
        cout<<"Enter the number of lunches ordered."<<endl;
                cin>>lunch;
                lunch_discount=calc_discount(lunch, lunch_costeach, lunch_disc, lunch_mt);
        cout<<"Enter the number of dinners ordered."<<endl;
                cin>>dinner;
                dinner_discount=calc_discount(dinner, dinner_costeach, dinner_disc, dinner_mt);

//This is for the output
        cout<<"                My Favorite Catering Service               "<<endl<<"*******************************************************************"<<endl;
        cout<<"Meal        Quantity        Cost        Discount        Cost After Discount"<<endl;
        cout<<break_cost<<endl;
        return 0;
}
//This is the function definition
double calc_discount(int quantity, double cost_each, double discount, int more_than)
{
double disc;

if(quantity>more_than)
        quantity*cost_each*discount;
else
        quantity*cost_each;
return disc;


I'm not sure what the problem is, but for some reason, when it outputs break_cost, it simply reads it as 0.00, regardless of the quantity entered. Keep in mind that the final output is only set up to output the break_cost and nothing else.
Last edited on
It's on line 19:

break_cost=(breakfast*break_costeach);

Breakfast is currently 0 since you didn't init it to anything, therefore it is always 0.
Thanks a ton. I managed to get the break_cost, lunch_cost, and dinner_cost to output perfectly by moving the X_cost identifiers under the couts. I'm still having a problem, though. For some reason, my function is not calculating discounts at all.... What am I doing wrong?
Topic archived. No new replies allowed.