Switch Case

Hello everyone, i can't seem to get this to run, it says total was not declared in this scope when i want to run it. it points out the error in my display statement at the end.
#include <iostream>

using namespace std;

int main()
{
int sPrice=10,ePrice=20,vPrice=15;
char redo;
do
{
char choice;
cout<<"Choose a section: "<<endl;
cout<<"E for Employee section"<<endl;
cout<<"S for student Section"<<endl;
cout<<"V for visitors section"<<endl;
cin>>choice;
int total=0;

switch(choice)
{
case 's':
cout<<"You've picked the Student Section, which costs R10"<<endl;
total=total+sPrice;
break;
case 'e':
cout<<"you've picked the Employee section, which costs R20"<<endl;
total=total+ePrice;
break;
case 'v':
cout<<"You've picked the Visitors section which costs R15"<<endl;
total=total+vPrice;
break;
default:
cout<<"Invalid Section"<<endl;

}
cout<<"Do you want to continue?"<<endl;
cin>>redo;
}while(redo=='y' || redo=='Y');
cout<<"The total ticket sales is R"<< total <<endl;

return 0;
}
Because total's life time ends when the do while loop ends. You have to put total outside of the do while loop.
Hello ashleydee1999,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

As rjphares has explained this may help you visualize what is happening. I put comments at the start of the different blocks which have different scope.

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

using namespace std;

int main()
{  // <--- Main block start.
	int sPrice = 10, ePrice = 20, vPrice = 15;
	char redo{};  // <---Need to initialize variable.

	do
	{  // <--- do/while block start.
		char choice{};  // <---Need to initialize variable.

		cout << "Choose a section: " << endl;
		cout << "E for Employee section" << endl;
		cout << "S for student Section" << endl;
		cout << "V for visitors section" << endl;
		cin >> choice;

		int total = 0;

		switch (choice)
		{  //  switch block start.
			case 's':
				cout << "You've picked the Student Section, which costs R10" << endl;
				total = total + sPrice;
				break;
			case 'e':
				cout << "you've picked the Employee section, which costs R20" << endl;
				total = total + ePrice;
				break;
			case 'v':
				cout << "You've picked the Visitors section which costs R15" << endl;
				total = total + vPrice;
				break;
			default:
				cout << "Invalid Section" << endl;
		} //  End switch.

		cout << "Do you want to continue?" << endl;
		cin >> redo;

	} while (redo == 'y' || redo == 'Y');  //  End do/while. End do/while block.

	cout << "The total ticket sales is R" << total << endl;

	return 0;
}  // End main. End main block. 

As you can see with code tags, a few well placed blank lines and proper indenting the code is much easier to read along with making the different blocks easier to see.

Sorry hit the wrong button.

Each block has its own scope. Any variable defined in the {}s of main ar available to the whole program. What is defined in the do/while block, like "int total{};", have scope only to that block. When the block ends so does the scope and any variables defined in that scope.

If you had defined a variable in the case statements of the switch only the switch would have scope. The do/while loop would not have access to those variables.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.