Working with void functions

Mar 29, 2016 at 9:34pm
Not real sure why my totals aren't coming out right?
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
#include <iostream>
#include <iomanip>

using namespace std;

    double item = 0.0;
    double total = 0.0;
    double totBill = 0.0;
    double tax = 0.0;
    double tip = 0.0;

    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()
    {
        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;
    }
    void Tcash();

int main()
{
    {//displays menu
    display();
    }

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

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

    //item input
    cin >> item;
    total += item
}
    tax = total * .065;
    tip = total * .20;
    totBill = total + tax + tip;
//displays the bill
{
    cost();
}

    return 0;
}
Mar 29, 2016 at 9:39pm
Line 6. What makes you think that you can reliably equality-compare item to integer values?

Line 78. What is the value of item now? How does that affect the total?
Mar 29, 2016 at 9:41pm
well it should come out 6.00 but it comes out 9.00 when I enter item 1 and then exit?
Last edited on Mar 29, 2016 at 9:42pm
Mar 29, 2016 at 9:50pm
are you declaring item numbers as ints? this doesn't work either....
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
#include <iostream>
#include <iomanip>

using namespace std;

    double item = 0.0;
    double total = 0.0;
    double totBill = 0.0;
    double tax = 0.0;
    double tip = 0.0;

    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()
    {
        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;
    }
    void Tcash();

int main()
{
    {//displays menu
    display();
    }

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

    //assigns dollar value to items
    if (item = 1.0)
       {
        item = 6.00;
       }
    else if (item = 2.0)
       {
        item = 4.50;
       }
    else if (item = 3.0)
       {
        item = 3.75;
       }
    else if (item = 4.0)
       {
        item = 5.50;
       }
    else if (item = 5.0)
       {
        item = 2.80;
       }
    else if ( item = 6.0)
       {
        item = 1.00;
       }
    else if ( item = 7.0)
       {
        item = 2.00;
       }

    //item input
    cin >> item;
    total += item;
}
    tax = total * .065;
    tip = total * .20;
    totBill = total + tax + tip;
//displays the bill
{
    cost();
}

    return 0;
}
Mar 29, 2016 at 9:56pm
well it should come out 6.00 but it comes out 9.00 when I enter item 1 and then exit?

Of course,
1. You type "1" and line 78 reads it into item
2. total = total + item = 0 + 1 = 1
3. You type "8" and line 78 reads it into item
4. total = total + item = 1 + 8 = 9
5. Loop has completed and total==9
Mar 29, 2016 at 10:07pm
so if the if else staements do not change the values?
Mar 29, 2016 at 10:10pm
They do, but on line 79 you overwrite all that with whatever the user types.
Mar 29, 2016 at 10:27pm
When I place cin >> item before the else if statements the loop doesn't stop? Not asking you to do it but can you give me a hint what I need to do?
Mar 29, 2016 at 10:32pm
What if you had two variables? One (int) for input values and another (double) for item price?
Mar 29, 2016 at 10:40pm
ahhh i will try that
Mar 29, 2016 at 10:41pm
same 9
Mar 29, 2016 at 10:55pm
I got it now had to move the loop brackets around as well, thank you
Topic archived. No new replies allowed.