case statement

Feb 19, 2013 at 5:11pm
Having trouble using case statements in the following program
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

#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
	const float
		LASER_TAG=18.75,
		BOWLING=6.5,
		ICE_SKATING=8.25,
		ROCK_CLIMBING=16.5;
	float activityCost;
	int groupSize,activitySelect;
	float totalCost;
	

	cout<<"How many people are in your group today? ";
	cin>>groupSize;
	
	while(groupSize<1)
	{
		cout<<"How many people are in your group today? ";
		cin>>groupSize;
	}

		cout<<"What activity would you like to do today? "<<endl
		<<"1. Laser tag"<<endl
		<<"2. Bowling"<<endl
		<<"3. Ice Skating"<<endl
		<<"4. Rock Climbing"<<endl
		<<"Please enter activity number followed by return key: ";
		cin>>activitySelect;

	while(activitySelect<1||activitySelect>4)
	{
		cout<<"What activity would you like to do today? "<<endl
			<<"1. Laser tag"<<endl
			<<"2. Bowling"<<endl
			<<"3. Ice Skating"<<endl
			<<"4. Rock Climbing"<<endl
			<<"Please enter activity number followed by return key: ";
			cin>>activitySelect;	

	}

			switch (activitySelect)
			{
			case '1':
				activityCost=LASER_TAG;
				break;
			case '2':
				activityCost=BOWLING;
				break;
			case '3':
				activityCost=ICE_SKATING;
				break;
			case '4':
				activityCost=ROCK_CLIMBING; 
				break;
			default:cout<<"Try Again"<<endl;
				break;
			}

	totalCost=activityCost*groupSize;

	
	cout<<setprecision(2)<<fixed
		<<"The total cost today for "<<groupSize<<" people is: "<<endl
		<<totalCost<<endl
		<<"Thank you and have a nice day!"<<endl;

	return 0;
}


returns error "activity cost is bieng used without being initialized
Last edited on Feb 19, 2013 at 5:13pm
Feb 19, 2013 at 5:22pm
If the 'default' statement is reached in the switch, then activityCost has not been set to anything. You then use it on line 65.
Feb 19, 2013 at 5:24pm
Should be
case 1:

And not:
case '1': <--This would be checking for a char.
Feb 19, 2013 at 5:27pm
Your case labels are incorrect. You're using character literals, not integer values. activitySelect is an int, therefore, your labels should also be int values.
 
  case 1: 


You're getting the error because you're taking the default branch and then falling through to the calculation on line 65 without setting actvity cost.
Feb 19, 2013 at 5:36pm
Thanks worked great Lynx and Abstract
Topic archived. No new replies allowed.