Switch

Hi, how the sytem read the input from my computer as 1, 2, 3, or 4? is that read from the case name? eg: case 1, then if i input 1, it will read from case "1"? Thanks...

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

int main ()
{
	int choice;

cout<<"What flavour ice cream do you want?"<<endl;
cout<<"Enter 1 for chocolate"<<endl;
cout<<"Enter 2 for vanilla"<<endl;
cout<<"Enter 3 for strawberry"<<endl;
cout<<"Enter 4 for Yam"<<endl;
cout<<"Enter your choice: ";
cin>> choice;

switch (choice)
{
case 1:
	cout<<"Chocolate, Muhammad's favourite."<<endl;
   break;
case 2:
	cout<<"Vanilla, Ismael's favourite"<<endl;
   break;
case 3:
	cout<<"Strawberry, Adibah's favourite"<<endl;
   break;
case 4:
	cout<<"Yam, Munirah's favourite"<<endl;
   break;
default:
	cout<<"We don't have any"<<endl;
   cout<<"Make another selection"<<endl;
}
getch ();
return 0;
}
e.g. this switch-clause

1
2
3
4
5
6
7
switch(choice){
    case 1:
        cout<<"blah..."<<endl;
        break;
    default:
        cout<<"blah, blah, blah"<<endl;
}


equals an if-clause which is structured like this:

1
2
3
4
5
6
if(choice==1)
{
    cout<<"blah..."<<endl;
}else{
    cout<<"blah, blah, blah"<<endl;
}


Therefore you can amplify this switch-case-stuff like you want and it will always work like an adequate if-else-clause.
it will read from the matching case (yes, it will read from case 1 according to your question), and you have to break it...
Thanks, just now the switch case can read my keyboard input, 1, 2, .... so how if i want to have a input like a, b, c? can it happen too if let say, i press "a" on my keyboard, then it will run the 1st case and give output like "Chocolate, Muhammad's favourite."? Thanks...
If you want to "switch" characters, you HAVE TO use if-statements... You CAN NOT use something like this:
1
2
3
4
5
6
7
8
9
char c='x';
switch(c)
{
    case 'x':
        //do this and that
        break;
    default:
        //do something different
}
@FlashDrive
It'd help if you could explain why not. ;)

-Albatross
Eh? Maybe I'm misreading things, but you can most certainly switch characters. In fact, you could probably take the exact copy paste from FlashDrive's "You CAN NOT use something like this:" code and it would work instantly.
I was hoping FlashDrive would explain why he thought it wouldn't work so that we could explain to him why it would, but eh. Switching on constants for POD types works fine. Switching on variables doesn't.

EDIT: For those who don't know, POD means Plain Old Data. It basically refers to non-compound C variable types.

-Albatross
Last edited on
Thanks~~

Gaminic, u r right, it works on character using switch but i don't know why need this 'x'; inside char c='x';, can I just declare to "0" since it can works too like the one below...

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

int main ()
{
	char choice = 0;

cout<<"What flavour ice cream do you want?"<<endl;
cout<<"Enter a for chocolate"<<endl;
cout<<"Enter b for vanilla"<<endl;
cout<<"Enter c for strawberry"<<endl;
cout<<"Enter d for Yam"<<endl;
cout<<"Enter your choice: ";
cin>> choice;

switch (choice)
{
case 'a':
	cout<<"Chocolate, Muhammad's favourite."<<endl;
   break;
case 'b':
	cout<<"Vanilla, Ismael's favourite"<<endl;
   break;
case 'c':
	cout<<"Strawberry, Adibah's favourite"<<endl;
   break;
case 'd':
	cout<<"Yam, Munirah's favourite"<<endl;
   break;
default:
	cout<<"We don't have any"<<endl;
   cout<<"Make another selection"<<endl;
}
getch ();
return 0;
}


somemore, I try to declare only char choice = 'a';, it works also, may i know why?

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

int main ()
{
	char choice = 'a';

cout<<"What flavour ice cream do you want?"<<endl;
cout<<"Enter a for chocolate"<<endl;
cout<<"Enter b for vanilla"<<endl;
cout<<"Enter c for strawberry"<<endl;
cout<<"Enter d for Yam"<<endl;
cout<<"Enter your choice: ";
cin>> choice;

switch (choice)
{
case 'a':
	cout<<"Chocolate, Muhammad's favourite."<<endl;
   break;
case 'b':
	cout<<"Vanilla, Ismael's favourite"<<endl;
   break;
case 'c':
	cout<<"Strawberry, Adibah's favourite"<<endl;
   break;
case 'd':
	cout<<"Yam, Munirah's favourite"<<endl;
   break;
default:
	cout<<"We don't have any"<<endl;
   cout<<"Make another selection"<<endl;
}
getch ();
return 0;
}



@Albatross, can I know what is POD types means? TQ...
oh, sorry. I guess I misunderstood something... ^^
@atjm88
The initialization step is trivial, because 'choice' is overwritten by cin on line 14.

Initialization is only important if the value is going to be used before an assignment takes place. Take this example:
1
2
3
int i;
i += 20;
cout << i;

What's the value of i? Who knows. Line 1 initializes i without assigning a value, line 2 adds 20 to i. "empty" initialization means i could have any value (some compilers set it to 0 automatically, but that's not guaranteed behavior).
Unless you're certain 'i' will be assigned a value before it is used, you need to initialize it with a (neutral) value:
1
2
3
int i = 0;
i += 20;
cout << i;
Thanks Geminic for the explanation^^

Question here:
Can I declare my char to zero? Is that a correct way char choice = 0;? Or I can only declare integer to zero int choice = 0;?

another question is
how about this kind of code char choice = 'a';? Is that means I declare choice to alphabet a? Or I can write in this way too char choice = a;? Thanks^^
Question here:
Can I declare my char to zero? Is that a correct way char choice = 0;? Or I can only declare integer to zero int choice = 0;?

Yes, you can initialise your char to 0. It's quite safe, as the ASCII value for 0 is NULL.

another question is
how about this kind of code char choice = 'a';? Is that means I declare choice to alphabet a? Or I can write in this way too char choice = a;? Thanks^^

The first one is fine. The second one will give you an error unless a is a previously defined variable.
Last edited on
Thanks iHutch...so for this, char choice = 'a';, does it have any meaning? since 0 is a value...how about "a"? got any value? TQ...
Well, yes. Because it's encased in ' ', it means the character literal a.

Not sure what you mean about "got any value". There's an ASCII value for a, if that's what you mean (97).
char's are basically just 1byte numbers. The compiler (or something else, I don't really know, but it doesn't matter) knows to 'translate' it via an ASCII table when it's shown to the user, but internally it's just a number. Like iHutch105 said, 'a' is actually 97. This letter<->number relationship is further exploited by seeing that 'b' = 'a'+1 and 'z' = 'a'+25.

That being said, while 'a' is 97, a isn't. a is a variable, because it has no quotes. If a doesn't exist (or is a number that can't be translated into a char), the compiler will show errors.
Thanks>>>

So means that char choice = 'a'; = char choice = 97;? Am I right?

How about char choice = a;? can works? or need to declare char a = 97 then only can use this code char choice = a;? which is actually similar to char choice = 'a';? Is that correct? TQ...
Yes, that's correct.
I tried to check what's the number of "choice" actually and 1st expectation is "97", but then after run, it gives answer "a" instead of "97" (Line 6-9), may I know why? TQ...

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

int main ()
{
   char a = 97;
	char choice = a;

cout<<choice<<"\n";
cout<<"What flavour ice cream do you want?"<<endl;
cout<<"Enter a for chocolate"<<endl;
cout<<"Enter b for vanilla"<<endl;
cout<<"Enter c for strawberry"<<endl;
cout<<"Enter d for Yam"<<endl;
cout<<"Enter your choice: ";
cin>> choice;

switch (choice)
{
case 'a':
	cout<<"Chocolate, Muhammad's favourite."<<endl;
   break;
case 'b':
	cout<<"Vanilla, Ismael's favourite"<<endl;
   break;
case 'c':
	cout<<"Strawberry, Adibah's favourite"<<endl;
   break;
case 'd':
	cout<<"Yam, Munirah's favourite"<<endl;
   break;
default:
	cout<<"We don't have any"<<endl;
   cout<<"Make another selection"<<endl;
}
getch ();
return 0;
}
Last edited on
Topic archived. No new replies allowed.