Function with parameters

Pages: 12
Values arent passing correctly

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
#include <iostream>
#include <iomanip>

using namespace std;


    void display()
    {
    cout << "***     BASEBALL GAME SNACK MENU     ***" << endl;
    cout << "----------------------------------------------" << endl;
    cout << "1   Hamburger 	        $6.00" << endl;
    cout << "2   Hotdog		$4.50" << endl;
    cout << "3   Peanuts	        $3.75" << endl;
    cout << "4   Popcorn	        $5.50" << endl;
    cout << "5   Soda		$2.80" << endl;
    cout << "6   Chips		$1.00" << endl;
    cout << "7   Water		$2.00" << endl;
    cout << "8   end order  "<< endl << endl;

    }


    void cost(double total, double tax, double tip, double totBill)

    {
    cout << "The total is: " << setprecision(2) << fixed << total << endl;
    cout << "Tip comes to: " << setprecision(2) << fixed << tip << endl;
    cout << "Tax on your purchase is: " << setprecision(2) << fixed << tax << endl;
    cout << "Your total bill is: " << setprecision(2) << fixed << totBill << endl;
    }

    //calculates amtTendered
    void Tcash(double totBill, double amtTendered)
    {
            cout << "enter amount tendered: " << endl;
    cin >> amtTendered;
    if (amtTendered >= totBill)
    {
        cout << "Your change is: " << amtTendered - totBill << endl;
    }
    else if (amtTendered < totBill)
    {
        cout << "Not enough tendered!!!!" << endl;
    }

    }

int main()
{
    double item;
    double price;
        item += price;
    double total;
        price += total;
    double tax;
        tax = total * .065;
    double tip;
        tip = total * .20;
    double totBill;
        totBill = total + tax + tip;
    double amtTendered;


    //displays menu
    display();

     while ( item !=8)
     {
     //displays enter item until 8 is selected
    cout << "Enter Menu Item:" << endl;
    cout << "----------------" << endl;
    cin >> item;

     }


    //assigns dollar value to items
    if (item == 1)
       {
        price = 6.00;
       }
    else if (item == 2)
       {
        price = 4.50;
       }
    else if (item == 3)
       {
        price = 3.75;
       }
    else if (item == 4)
       {
        price = 5.50;
       }
    else if (item == 5)
       {
        price = 2.80;
       }
    else if ( item == 6)
       {
        price = 1.00;
       }
    else if ( item == 7)
       {
        price = 2.00;
       }
    else if (item == 8)
    {
        price = 0;
    }

    void cost(double total, double tax, double tip, double totBill);



    //displays the bill
    cost(total, tax,  tip,  totBill);

    //displays and calculates cash tendered
    Tcash( totBill,  amtTendered);
}



Hi,
Can you give more details about your assignment?
Does your assignment contain output samples?
No the ouputs should be as described I get the outputs but the values come back as 0 not sure what I am doing wrong?
There is so many thing wrong with your program, you should start by fixing the warnings that should be generated by your code:

main.cpp||In function ‘int main()’:|
main.cpp|68|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
main.cpp|79|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
main.cpp|83|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
main.cpp|87|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
main.cpp|91|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
main.cpp|95|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
main.cpp|99|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
main.cpp|103|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
main.cpp|107|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
main.cpp|53|warning: ‘item’ is used uninitialized in this function [-Wuninitialized]|
main.cpp|53|warning: ‘price’ is used uninitialized in this function [-Wuninitialized]|
main.cpp|55|warning: ‘total’ is used uninitialized in this function [-Wuninitialized]|
main.cpp|120|warning: ‘amtTendered’ may be used uninitialized in this function [-Wmaybe-uninitialized]|


By the way the major problems have nothing to do with your functions. The major problems are mostly in your main() function.
Last edited on
ok I will try to go thru and do those
Ok, I see no errors now, I had tw models forgot this was unitialized. here is what I have now?

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
#include <iostream>
#include <iomanip>

using namespace std;


    void display()
    {
    cout << "***     BASEBALL GAME SNACK MENU     ***" << endl;
    cout << "----------------------------------------------" << endl;
    cout << "1   Hamburger 	        $6.00" << endl;
    cout << "2   Hotdog		$4.50" << endl;
    cout << "3   Peanuts	        $3.75" << endl;
    cout << "4   Popcorn	        $5.50" << endl;
    cout << "5   Soda		$2.80" << endl;
    cout << "6   Chips		$1.00" << endl;
    cout << "7   Water		$2.00" << endl;
    cout << "8   end order  "<< endl << endl;

    }


    void cost(double total, double tax, double tip, double totBill)

    {
    cout << "The total is: " << setprecision(2) << fixed << total << endl;
    cout << "Tip comes to: " << setprecision(2) << fixed << tip << endl;
    cout << "Tax on your purchase is: " << setprecision(2) << fixed << tax << endl;
    cout << "Your total bill is: " << setprecision(2) << fixed << totBill << endl;
    }

    //calculates amtTendered
    void Tcash(double totBill, double amtTendered)
    {
            cout << "enter amount tendered: " << endl;
    cin >> amtTendered;
    if (amtTendered >= totBill)
    {
        cout << "Your change is: " << amtTendered - totBill << endl;
    }
    else if (amtTendered < totBill)
    {
        cout << "Not enough tendered!!!!" << endl;
    }

    }

int main()
{
    double item = 0.0;
    double price = 0.0;
        item += price;
    double total = 0.0;
        price += total;
    double tax = 0.0;
        tax = total * .065;
    double tip = 0.0;
        tip = total * .20;
    double totBill = 0.0;
        totBill = total + tax + tip;
    double amtTendered = 0.0;


    //displays menu
    display();

     while ( item !=8)
     {
     //displays enter item until 8 is selected
    cout << "Enter Menu Item:" << endl;
    cout << "----------------" << endl;
    cin >> item;

     }


    //assigns dollar value to items
    if (item == 1)
       {
        price = 6.00;
       }
    else if (item == 2)
       {
        price = 4.50;
       }
    else if (item == 3)
       {
        price = 3.75;
       }
    else if (item == 4)
       {
        price = 5.50;
       }
    else if (item == 5)
       {
        price = 2.80;
       }
    else if ( item == 6)
       {
        price = 1.00;
       }
    else if ( item == 7)
       {
        price = 2.00;
       }
    else if (item == 8)
    {
        price = 0;
    }

    void cost(double total, double tax, double tip, double totBill);



    //displays the bill
    cost(total, tax,  tip,  totBill);

    //displays and calculates cash tendered
    Tcash( totBill,  amtTendered);

    return 0;
}

So your algorithm is that you select items (or add to cart) until you press 8. You count every item in the cart to get total price then you calculate tax, tip, totBill etc and output the result?
yes price should price += total and so on and all my output is 0?
line 58,67: Do not use a double for item. Doubles are approximations. item should be an int.

Line 52: This line is useless. price is 0, item is 0. Adding the two together is still going to be 0.
You're doing calculations before you have any meaningful values. Besides, why are you adding price to item? They are two different things.

line 54,56,58,60: These lines are in the wrong place. These calculations need to be after line 109 when you have some meaningful input.

line 111: Function prototype doesn't belong here. Function prototypes belong after line 4, but you don;t even need it since your function is in front of main.

Line 33: Why are you passing amtTendered in as an argument? It's not used outside this function. It should be a local variable inside this function.

Last edited on
Ok, item I get 67 is my while loop though?
All as follows except for loop but same result

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
#include <iostream>
#include <iomanip>

using namespace std;


    void display()
    {
    cout << "***     BASEBALL GAME SNACK MENU     ***" << endl;
    cout << "----------------------------------------------" << endl;
    cout << "1   Hamburger 	        $6.00" << endl;
    cout << "2   Hotdog		$4.50" << endl;
    cout << "3   Peanuts	        $3.75" << endl;
    cout << "4   Popcorn	        $5.50" << endl;
    cout << "5   Soda		$2.80" << endl;
    cout << "6   Chips		$1.00" << endl;
    cout << "7   Water		$2.00" << endl;
    cout << "8   end order  "<< endl << endl;

    }


    void cost(double total, double tax, double tip, double totBill)

    {
    cout << "The total is: " << setprecision(2) << fixed << total << endl;
    cout << "Tip comes to: " << setprecision(2) << fixed << tip << endl;
    cout << "Tax on your purchase is: " << setprecision(2) << fixed << tax << endl;
    cout << "Your total bill is: " << setprecision(2) << fixed << totBill << endl;
    }

    //calculates amtTendered
    void Tcash(double totBill, double amtTendered)
    {
            cout << "enter amount tendered: " << endl;
    cin >> amtTendered;
    if (amtTendered >= totBill)
    {
        cout << "Your change is: " << amtTendered - totBill << endl;
    }
    else if (amtTendered < totBill)
    {
        cout << "Not enough tendered!!!!" << endl;
    }

    }

int main()
{
    int item = 0;
    double price = 0.0;
    double total = 0.0;
    double tax = 0.0;
    double tip = 0.0;
    double totBill = 0.0;
    double amtTendered = 0.0;


    //displays menu
    display();

     while ( item !=8)
     {
     //displays enter item until 8 is selected
    cout << "Enter Menu Item:" << endl;
    cout << "----------------" << endl;
    cin >> item;

     }


    //assigns dollar value to items
    if (item == 1)
       {
        price = 6.00;
       }
    else if (item == 2)
       {
        price = 4.50;
       }
    else if (item == 3)
       {
        price = 3.75;
       }
    else if (item == 4)
       {
        price = 5.50;
       }
    else if (item == 5)
       {
        price = 2.80;
       }
    else if ( item == 6)
       {
        price = 1.00;
       }
    else if ( item == 7)
       {
        price = 2.00;
       }
    else if (item == 8)
    {
        price = 0;
    }

    void cost(double total, double tax, double tip, double totBill);



    price += total;
    tax = price * .065;
    tip = (total + tax) * .15;
    totBill = total + tax + tip;

    //displays the bill
    cost(total, tax,  tip,  totBill);

    //displays and calculates cash tendered
    Tcash( totBill,  amtTendered);

    return 0;
}



Firstly, your input part is broken.
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
while ( item !=8)
     {
     // displays enter item until 8 is selected 
cout << "Enter Menu Item:" << endl;
    cout << "----------------" << endl;
    cin >> item;
     }
    //assigns dollar value to items
   if (item == 1)
       {
        price = 6.00;
       }
    else if (item == 2)
       {
        price = 4.50;
       }
    else if (item == 3)
       {
        price = 3.75;
       }
    else if (item == 4)
       {
        price = 5.50;
       }
    else if (item == 5)
       {
        price = 2.80;
       }
    else if ( item == 6)
       {price = 1.00;
       }
    else if ( item == 7)
       {
        price = 2.00;
       }
    else if (item == 8)
    {
        price = 0;
    }


Try this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while ( item !=8)
{
     // Display enter item until 8 is selected
cout << "Enter Menu Item: ";
    cin >> item; cout << endl;

    switch(item)
    {
        case 1 : total += 6.00; break;
        case 2 : total += 4.50; break;
        case 3 : total += 3.75; break;
        case 4 : total += 5.50; break;
        case 5 : total += 2.80; break;
        case 6 : total += 1.00; break;
        case 7 : total += 2.00; break;
    }
}
Last edited on
Ok I have read about switched but we have never learned them can i just ad if statement inside the loop would that fix it?
It did work tho ) but I would like to know the answer with what I have learned if there is one?
> Ok I have read about switched but we have never learned them

If you haven't learnt about it just forget it. Putting your if statements inside the while loop should fix the problem too.

But notice the difference :
total += 6.00;

Instead of :
price = 6.00;
can i just ad if statement inside the loop would that fix it?

Did you try moving the if() statements into the loop? Did it work?


can i just ad if statement inside the loop

Yes.

Line 106: As stated before, a function prototype doesn't belong here.

Line 110: Why are you adding total to price? You have this backwards. You should be adding price to total. And, you should be doing this inside your while loop.

I believe I did but not sure which model I was working on at the time
too many errors too many trials lol
> I would like to know the answer with what I have learned if there is one?

Can you please clarify what you mean by that?
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
#include <iostream>
#include <iomanip>

using namespace std;


void display()
{   cout << "***     BASEBALL GAME SNACK MENU     ***" << endl;
    cout << "----------------------------------------------" << endl;
    cout << "1   Hamburger 	        $6.00" << endl;
    cout << "2   Hotdog		$4.50" << endl;
    cout << "3   Peanuts	        $3.75" << endl;
    cout << "4   Popcorn	        $5.50" << endl;
    cout << "5   Soda		$2.80" << endl;
    cout << "6   Chips		$1.00" << endl;
    cout << "7   Water		$2.00" << endl;
    cout << "8   end order  "<< endl << endl;
}


void cost(double total, double tax, double tip, double totBill)
{   cout << "The total is: " << setprecision(2) << fixed << total << endl;
    cout << "Tip comes to: " << setprecision(2) << fixed << tip << endl;
    cout << "Tax on your purchase is: " << setprecision(2) << fixed << tax << endl;
    cout << "Your total bill is: " << setprecision(2) << fixed << totBill << endl;
}

    //calculates amtTendered
void Tcash (double totBill)
{   double  amtTendered;

    cout << "enter amount tendered: " << endl;
    cin >> amtTendered;
    if (amtTendered >= totBill)
    {   cout << "Your change is: " << amtTendered - totBill << endl;
    }
    else if (amtTendered < totBill)
    {
        cout << "Not enough tendered!!!!" << endl;
    }

    }

int main()
{   int item = 0;
    double total = 0.0;
    double tax = 0.0;
    double tip = 0.0;
    double totBill = 0.0;
    
    //displays menu
    display();

    while (item !=8)
    {   //displays enter item until 8 is selected
        cout << "Enter Menu Item:" << endl;
        cout << "----------------" << endl;
        cin >> item;
    
        //assigns dollar value to items
        switch(item)
        {
        case 1: total += 6.00; break;
        case 2: total += 4.50; break;
        case 3: total += 3.75; break;
        case 4: total += 5.50; break;
        case 5: total += 2.80; break;
        case 6: total += 1.00; break;
        case 7: total += 2.00; break;
        case 8: break;
        default:    cout << "Invalid selection" << endl;
                    continue;
        }
    }
    tax = total * .065;
    tip = (total + tax) * .15;
    totBill = total + tax + tip;

    //displays the bill
    cost(total, tax,  tip,  totBill);

    //displays and calculates cash tendered
    Tcash (totBill);
    system ("pause");
    return 0;
}

Pages: 12