Error, variable not being used?

This is for my assignment for a course, and I was wondering what is wrong with my variable total? Whenever I enter a order number, it saids total is being used without being defined. I already did

double total;

isn't that defining it?

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

using namespace std;
 
void main()
{
        string name;
 
        cout << "What is your name? ";
        getline (cin, name);
        cout << endl << "Welcome " << name << "! " << "How may I help you today at Pizzaria?\n";
        // Display Menu
        cout << "Pizzaria Menu Display\n";
        cout << endl << "Drinks:\n";
        cout << "#1: Pepsi               - $1.29\n";
        cout << "#2: Coco Cola           - $1.29\n";
        cout << "#3: Milkshake           - $2.19\n";
        cout << "#0: Nothing             - $0.00\n";
    cout << endl << "Toppings:\n";
        cout << "#4: Bacon               - $0.49\n";
        cout << "#5: Ricotta             - $0.49\n";
        cout << "#6: Mushroom            - $0.49\n";
        cout << "#7: Pepperoni           - $0.49\n";
        cout << "#0: Nothing             - $0.00\n";
    cout << endl << "Pizza Size:\n";
        cout << "#8: Small Sliced Pizza  - $2.19\n";
        cout << "#9: Medium Sliced Pizza - $2.89\n";
        cout << "#10: Large Sliced Pizza  - $3.58\n";
        cout << "#0: Nothing             - $0.00\n";
       
        double total;
        int drink;
        int top;
        int size;
 
        cout << endl << "What would you like to drink?\n";
        cout << "Enter the order number: ";
        cin >> drink;
        if (drink = 3)
                {
                total = total + 2.19;
                cout << endl << "#3: Milkshake           - $2.19\n";           
                cout << endl << "What would you like your toppings to be?\n";
                }
                        else if (drink = 2)
                        {
                        total = total + 1.29;
                        cout << endl << "#2: Coco Cola           - $1.29\n";           
                        cout << endl << "What would you like your toppings to be?\n";
                        }
                                else if (drink = 1)
                                {
                                total = total + 1.29;
                                cout << endl << "#1: Pepsi               - $1.29\n";           
                                cout << endl << "What would you like your toppings to be?\n";
                                }
        else
        {
        total = 0;
        cout << endl << "Nothing to drink.\n";
        cout << endl << "What would you like your toppings to be?\n";
		}
		
		cout << "Your total will be " << total << endl;
}

double total;

When a variable is defined without being initialized(assigned a value), it holds the value that was previously in the spot of memory it is now located. If you do not assign total a value when it is defined, it is not automatically set to zero.

That is why when you try to execute:

total = total + 2.19;

you are having issues. Because at that point, total does not equal anything, usable anyway. Try:

double total = 0;
Last edited on
first error void main() this is NOT the c++ standard and some compilers will even reject this.

second errordouble total; total is a piece of memory in your RAM and could be anything from part of a picture to that video you were just watching, it has just been declared as a double. so when you do total = total + 2.19;you are really doing [yourpicture] += 2.19; which will give you some random value. setting it to 0 when you declare it will fix your problem with that. double total = 0;

also all the else if statements should be written into a switch statement.

you can also declare variables like
1
2
3
4
    int drink;
        int top;
        int size;
 

as int drink, top, size;
Last edited on
Thanks bcrawford, I completely forgot about that.

Also uiho, do you have any links to any switch tutorial? Why would it be any different to else if etc?

Thanks for the help guys, and for the extra tips and advices to the rest of the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main(){
    
    switch (<#expression#>) {
        case <#constant#>:
            <#statements#>
            break;
            
        default:
            break;
    }
}
I just ran the program and realized something, the order numbers are all wrong, any ideas?


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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <string>

using namespace std;
 
void main()
{
        string name;
 
        cout << "What is your name? ";
        getline (cin, name);
        cout << endl << "Welcome " << name << "! " << "How may I help you today at Pizzaria?\n";
        // Display Menu
        cout << endl << "Pizzaria Menu Display\n";
		cout << "=================================";
        cout << endl << "             Drinks:\n";
        cout << endl << "#1:  Pepsi               - $1.29\n";
        cout << "#2:  Coco Cola           - $1.29\n";
        cout << "#3:  Milkshake           - $2.19\n";
        cout << "#0:  Nothing             - $0.00\n";
		cout << endl << "             Toppings:\n";
        cout << endl << "#4:  Bacon               - $0.49\n";
        cout << "#5:  Ricotta             - $0.49\n";
        cout << "#6:  Mushroom            - $0.49\n";
        cout << "#7:  Pepperoni           - $0.49\n";
        cout << "#0:  Nothing             - $0.00\n";
		cout << endl << "             Pizza Size:\n";
        cout << endl << "#8:  Small Sliced Pizza  - $2.19\n";
        cout << "#9:  Medium Sliced Pizza - $2.89\n";
        cout << "#10: Large Sliced Pizza  - $3.58\n";
		cout << "=================================\n";
       
        double total = 0;
        int drink, top, size;
 
        cout << endl << "What would you like to drink?\n";
        cout << "Enter the order number: ";
        cin >> drink;

		// Determine drink order
        if (drink = 3)
                {
                total = total + 2.19;
                cout << endl << "#3: Milkshake           - $2.19\n";           
                cout << endl << "What would you like your toppings to be?\n";
				cout << "Enter the order number: ";
				cin >> top;
                }
                        else if (drink = 2)
                        {
                        total = total + 1.29;
                        cout << endl << "#2: Coco Cola           - $1.29\n";           
                        cout << endl << "What would you like your toppings to be?\n";
						cout << "Enter the order number: ";
						cin >> top;
                        }
                                else if (drink = 1)
                                {
                                total = total + 1.29;
                                cout << endl << "#1: Pepsi               - $1.29\n";           
                                cout << endl << "What would you like your toppings to be?\n";
								cout << "Enter the order number: ";
								cin >> top;
                                }
        else
        {
        cout << endl << "Nothing to drink.\n";
        cout << endl << "What would you like your toppings to be?\n";
		cout << "Enter the order number: ";
		cin >> top;
		}
		// Determine topping order
		if (top = 7)
		{
			total = total + 0.49;
			cout << endl << "#7: Pepperoni           - $0.49\n";
			cout << endl << "What size would you like your slice be?";
			cout << "Enter the order number: ";
			cin >> size;
		}
			else if (top = 6)
			{
				total = total + 0.49;
				cout << endl << "#6: Mushroom            - $0.49\n";
				cout << endl << "What size would you like your slice be?";
				cout << "Enter the order number: ";
				cin >> size;
			}
				else if (top = 5)
				{
					total = total + 0.49;
					cout << endl << "#5: Ricotta             - $0.49\n";
					cout << endl << "What size would you like your slice be?";
					cout << "Enter the order number: ";
					cin >> size;
				}
					else if (top = 4)
					{
						total = total + 0.49;
						cout << endl << "#4: Bacon               - $0.49\n";
						cout << endl << "What size would you like your slice be?";
						cout << "Enter the order number: ";
						cin >> size;
					}
		else
		{
			cout << endl << "No toppings.\n";
			cout << endl << "What size would you like your slice be?\n";
			cin >> size;
		}
		// Determine size order
		if (size = 10)
		{
			total = total + 3.58;
			cout << endl << "#10: Large Sliced Pizza  - $3.58\n";
		}
			else if (top = 9)
			{
				total = total + 2.89;
				cout << endl << "#9:  Medium Sliced Pizza - $2.89\n";
			}
				else if (top = 8)
				{
					total = total + 2.19;
					cout << endl << "#8:  Small Sliced Pizza  - $2.19\n";
				}
		else
		{
			cout << endl << "Unrecognizable order number, please try again. \n";
			cout << endl << "What size would you like your slice be?\n";
			cin >> size;
		}

		//Calculate total with tax
		float taxf = 0.10;
		float tax = 0;
		tax = total * taxf;
		total += tax;
		cout << endl << "Tax: " << tax << endl;
		cout << "Total: " << total << endl;
		cout << "Thanks for your stay here at Pizzaria, " << name;
}
Hey uiho, would you mind using the switch on my code for an example for me?

It would help much!
NVM! I got it! Thanks so much for the recommendation on switches
Topic archived. No new replies allowed.