Two switch statements in one?

Sep 8, 2010 at 3:10am
We were given a problem like this:
Compute for the total tuition fee of a student based on the course given.
Year Level Description Course Rate
1 Freshman BSIT 225.40
2 Sophomore BS-CS 423.42
3 Junior Mgt 234
4 Senior Finance 425.31

Input no. of units, downpayment, and year level. Ouput total tuition fee, balance, and description of year level.

Here's what I did:
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
#include <iostream>
#include <string>
using namespace std;
int main ()

{
     int units, yc, cc;
     double dp, ttf, bal;
     
     cout <<"Enter no. of units:";
     cin >> units;
     
     cout <<"Enter your downpayment:";
     cin >> dp;
     
     cout <<"Enter your year level:";
     cin >> yc;
     
     cout <<"Enter your course:";
     cin >> cc;
     
     switch (yc)
     {
            case 1: cout <<"Freshman";
            break;
            case 2: cout <<"Sophomore";
            break;
            case 3: cout <<"Junior";
            break;
            case 4: cout <<"Senior";
            break;
            
     }
     
     switch (cc)
     {
     
            case 'a': ttf = units * 225.40;
                         bal = ttf - dp;
                         cout <<"Your balance is " << bal;
                         cout <<"YOur ttf is " << ttf;
                         break;
                         
            case 'b': ttf = units * 325.40;
                         bal = ttf - dp;
                         cout <<"Your balance is " << bal;
                         cout <<"YOur ttf is " << ttf;
                         break;
                         
     }
     system ("PAUSE");
     return 0;
}


But it's kinda messed up.
The thing that confuses me is that you need to have two switch. One for the year level, and one for the course.
Help?
Sep 8, 2010 at 3:29am
Looks fine to me, although I would place the freshman etc. strings in an array and use yc to index it.
Sep 8, 2010 at 10:08am
There's nothing wrong with having two switch statements.
Sep 8, 2010 at 12:12pm
Just we call nested switch :)
Sep 8, 2010 at 12:20pm
Theres nothing wrong with my code? Then why is that when I enter a number, it show's all the cout i made and it wont proceed to compute the ttf?

Also, can I have two variables in the switch (x,x)?
Sep 8, 2010 at 12:44pm
Then how do you want the case statements to look?

If you really want to make one for each combination, you can do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Two
{
	int A, B;

	Two(const int& a, const int& b)
	{
		A = a;
		B = b;
	}
}

const Two PossibilityA (0,0);
const Two PossibilityB (1,0);
const Two PossibilityC (0,1);
//etc 
1
2
3
4
5
6
7
8
9
10
11
12
13
Two Input;
cin >> Input.A;
cin >> Input.B;

switch(Input)
{

case PossibilityA:
	//do stuff
case PossibilityB:
	//do more stuff
case PossibilityC:
	//do even more stuff, etc etc 


But as you can clearly see, it's a lot more work.
Last edited on Sep 8, 2010 at 12:44pm
Sep 8, 2010 at 12:49pm
Also, can I have two variables in the switch (x,x)?

i think no, for what logic?
Theres nothing wrong with my code?

here is
You have declared cc as interger int units, yc, cc;but...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  
     switch (cc)
     {
     
            case 'a': ttf = units * 225.40;//you have use it as char
                         bal = ttf - dp;
                         cout <<"Your balance is " << bal;
                         cout <<"YOur ttf is " << ttf;
                         break;
                         
            case 'b': ttf = units * 325.40;
                         bal = ttf - dp;
                         cout <<"Your balance is " << bal;
                         cout <<"YOur ttf is " << ttf;
                         break;
                         
     }
Last edited on Sep 8, 2010 at 12:52pm
Sep 8, 2010 at 12:53pm
this work for me ;)
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
#include <iostream>
#include <string>
using namespace std;
int main ()

{
     int units, yc;
     char cc;
     double dp, ttf, bal;
     
     cout <<"Enter no. of units:";
     cin >> units;
     
     cout <<"Enter your downpayment:";
     cin >> dp;
     
     cout <<"Enter your year level:";
     cin >> yc;
     
     cout <<"Enter your course:";
     cin >> cc;
     
     switch (yc)
     {
            case 1: cout <<"Freshman";
            break;
            case 2: cout <<"Sophomore";
            break;
            case 3: cout <<"Junior";
            break;
            case 4: cout <<"Senior";
            break;
            
     }
     
     switch (cc)
     {
     
            case 'a': ttf = units * 225.40;
                         bal = ttf - dp;
                         cout <<"Your balance is " << bal;
                         cout <<"YOur ttf is " << ttf;
                         break;
                         
            case 'b': ttf = units * 325.40;
                         bal = ttf - dp;
                         cout <<"Your balance is " << bal;
                         cout <<"YOur ttf is " << ttf;
                         break;
                         
     }
     system ("PAUSE");
     return 0;
}
Sep 8, 2010 at 1:00pm
Okay I get it now.

Can I use a switch within a switch?
Sep 8, 2010 at 1:03pm
Yes, but it won't really help you, here. AFAICT, yc and cc are two completely independent variables.
Sep 8, 2010 at 1:08pm
Here is an example, just for reference, of a switch inside of a switch:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch(bleh)
{
case 1:
	//do something
case 2:
	switch(meh)
	{
	case 'a':
		//do something
	case 'b':
		//do something
	}
case 3:
	//do something
}
Sep 8, 2010 at 1:08pm
yap! nested switch
Sep 8, 2010 at 7:18pm
hello..i see that you are talking about "nested switches",so y ran into a problem of my own.here is what y done so far.
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
int main ()
{
	char Color;
	int Fruit;
	printf("Chose what fruit you want:\n");
	printf("1-Apple\n");
	printf("2-Orange\n");
	scanf("%d",&Fruit);
	switch(Fruit){
		case 1: printf("Ok..so we have 2 types of apples:\n");
				  printf("R-Red apples\n");
				  printf("G-Green apples\n");
				  Color=getche(); //the bug is somewhere here(i think :))

				  switch(Color){
						case 'r':
						case 'R' : printf("Ty for buying red apples.10 $ please %c\n",1);
							break;
                                                case 'v' :
						case 'V' : printf("Ty for buying green apples.12$ please %c\n",1);
							break;
						default : printf("Wrong option..u blind or what?\n");
				  }
			break;
		case 2: printf("Damn..where out of oranges\n");
				  printf("1-Report the problem\n");
				  printf("2-Then get out of my shop\n");
				  //and maybe another switch/case
			break;
		default : printf("Wrong option..u blind or what?\n");
	}
	printf("\nPress Enter to continue");
	getchar();
	return 0;
}

sry for the bad formatting,just pasted it from vs and it is a bit displaced..don't now why.
So my problem is not the menu itself,y got the idea and it works fine but,the problem is when i thy to read the options from the keyboard.if y had int values,there would be no problem,but y need some char to..and my getchar() skips the second switch..i tried to use getche(),witch does not wait for you to press enter(no ecko )..and y am starting to thing that the enter thing may be the problem..tried printf("%c"&Color); but skips also..so how can y read the char options so that is going trough all switches.TY
Last edited on Sep 8, 2010 at 7:22pm
Topic archived. No new replies allowed.