unknown error

For some reason my code seems to add extra drinks in the tea and juice categories even if i only type one. if anyone can find the error please let me know. help or advice is greatly appreciated.




#include <iostream>
#include <iomanip>

using namespace std;

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
 int main ()
{
    int coffee, juice, soda, tea, Manager_Special;
    char choice;
    int total_drinks;
        
    do
    {
        cout << "Welcome to My Coffee Shop! We Offer:" << endl;
        cout << "C - Coffee ($1.89)" << endl;
        cout << "J - Juice ($2.99)" << endl;
        cout << "S - Soda ($3.99)" << endl;
        cout << "T - Tea ($4.99)" << endl;
        cout << "M - Manager Special ($5.99)" << endl;
        cout << "X - Done With Placing Order" << endl;
        cout << "What drink would you like? Select C, J, S, T, M (or X to finish with order)" <<endl;
        cin >> choice;
        
        switch (choice)
        {
                
            case 'C':
                cout << "Thank you. You have ordered the Coffee.\n" << endl;
                coffee++;
                break;
            case 'c':
                cout << "Thank you. You have ordered the Coffee.\n" << endl;
                coffee++;
                break;
            case 'J':
                cout << "Thank you. You have ordered the Juice.\n" << endl;
                juice++;
                break;
            case 'j':
                cout << "Thank you. You have ordered the Juice.\n" << endl;
                juice++;
                break;
            case 'S':
                cout << "Thank you. You have ordered the Soda.\n" << endl;
                soda++;
                break;
            case 's':
                cout << "Thank you. You have ordered the Soda.\n" << endl;
                soda++;
                break;
            case 'T':
                cout << "Thank you. You have ordered the Tea.\n" << endl;
                tea++;
                break;
            case 't':
                cout << "Thank you. You have ordered the Tea.\n" << endl;
                tea++;
                break;
            case 'M':
                cout << "Thank you. You have order the Manager Special.\n" << endl;
                Manager_Special++;
                break;
            case 'm':
                cout << "Thank you. You have order the Manager Special.\n" << endl;
                Manager_Special++;
                break;
            case 'X':
                break;
            case 'x':
                break;
            default:
                cout << "You did not enter C, J, S, T, M, or X!\n" << endl;
                break;
        }
    }
    while (choice != 'x' && choice != 'X');

    total_drinks = coffee + juice + soda + tea + Manager_Special;
    double total = (coffee * 1.89) + (juice * 2.99) + (soda * 3.99) + (tea * 4.99) + (Manager_Special * 5.99);
    cout << "You have ordered " << total_drinks << " drinks."<< " Here is your order: " << endl;
    cout << "Beverage" << setw(25) << "Quantity"<< endl;
if (coffee> 0)
	{
	
	cout << "Coffee" << setw (20) << coffee << endl;
}

if (juice> 0)
	{
	
	cout << "Juice" << setw (21) << juice << endl;
}

if (soda> 0)
	{
	
	cout << "Soda" << setw (22) << soda << endl;
}

if (tea> 0)
	{
	
	cout << "Tea" << setw (23) << tea << endl;
}  

if (Manager_Special> 0)
	{
	
	cout << "Manager Special" << setw (11) << Manager_Special << endl;
}  
cout << "Total: " << total << endl;

cout << "Done. Have a great day!" << endl;
    return 0;
} 
Last edited on
Hi,

First up please always use code tags: http://www.cplusplus.com/articles/z13hAqkS/

Next, what does the compiler say? Hint make sure you have all the warning options turned on. Using cpp.sh (uses g++ compiler), turn on all 3 -Wall -Wextra -pedantic


 In function 'int main()': 
65:18: warning: 'Manager_Special' may be used uninitialized in this function [-Wmaybe-uninitialized] 
57:6: warning: 'tea' may be used uninitialized in this function [-Wmaybe-uninitialized] 
49:7: warning: 'soda' may be used uninitialized in this function [-Wmaybe-uninitialized] 
37:8: warning: 'juice' may be used uninitialized in this function [-Wmaybe-uninitialized] 
29:9: warning: 'coffee' may be used uninitialized in this function [-Wmaybe-uninitialized]


It is a golden rule for coding - Always initialise your variable to something :+D

With this:

1
2
3
4
5
6
7
8
case 'C':
cout << "Thank you. You have ordered the Coffee.\n" << endl;
coffee++;
break;
case 'c':
cout << "Thank you. You have ordered the Coffee.\n" << endl;
coffee++;
break;


Make you life easier by using either of topper or tolower functions, that way you won't have to do your logic twice, one for each of lower or upper case chars.

Alternatively you can let the code fall through from case to case:

1
2
3
4
5
case 'C':
case 'c':
cout << "Thank you. You have ordered the Coffee.\n" << endl;
coffee++;
break;



If you ever find yourself writing the same or very similar code over and over then there is a better way :+)

Good Luck !!
im using dev c++ and i dont see where the options are for the warnings it says though when looking through the compiler logs that there are no warnings or errors.
Hi,

The very first item when I Googled "DevC++ compiler options" :

http://www.letu.edu/people/stevearmstrong/COSC%201303/The%20DevC++%20Compiler.htm


Gogle and wiki are your friends :+)

Compiler warnings are also your friends, they tell where problems in your code are. Even though it's a warning, it will most likely result in your program not working in some way.

Not sure what the default warnings for DevC++ are, probably something minimal - so that might be why you had no warnings.

There are even more warnings not turned on despite using -Wall -Wextra -pedantic-errors, I routinely compile with all these ones:

http://www.cplusplus.com/forum/general/183731/#msg899203
http://www.cplusplus.com/forum/beginner/186236/#msg908299

Good Luck !!
Topic archived. No new replies allowed.