switch case

If i want to have one extra conditions in which others input besides the character below is in zone4, where should i add in the condition?

#include<iostream>

using namespace std;

int main()
{
char code;
int zone;
cin>>code;

switch(code)
{
case'a':cout<<"zone=1"<<endl;break;
case'j':cout<<"zone=1"<<endl;break;
case'm':cout<<"zone=1"<<endl;break;
case'e':cout<<"zone=2"<<endl;break;
case'f':cout<<"zone=2"<<endl;break;
case'h':cout<<"zone=2"<<endl;break;
case'g':cout<<"zone=3"<<endl;break;
case'r':cout<<"zone=3"<<endl;break;
}
return 0;
}
With the other case statements.
Litterally what PanGalactic said.

More specifically:

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

using namespace std;

int main()
{
char code;
int zone;
cin>>code;

switch(code)
{
case'a':cout<<"zone=1"<<endl;break;
case'j':cout<<"zone=1"<<endl;break;
case'm':cout<<"zone=1"<<endl;break;
case'e':cout<<"zone=2"<<endl;break;
case'f':cout<<"zone=2"<<endl;break;
case'h':cout<<"zone=2"<<endl;break;
case'g':cout<<"zone=3"<<endl;break;
case'r':cout<<"zone=3"<<endl;break;
case's':cout<<"zone=4"<<endl;break;
case't':cout<<"zone=4"<<endl;break;
case'u':cout<<"zone=4"<<endl;break;
}
return 0;
}


Try using the code tags (the <> icon on the right) for posting code in the future for ease of readability.
You do realize he was speaking of dynamically adding them at run-time?

EDIT:
Anyways, case-statements only accept constant values, I advice using if and else repeatedly:
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
#include<iostream>

using namespace std;

int main()
{
   char code, ch1, ch2;
   int zone;
   cin >> code >> ch1 >> ch2;
   if (code == 'a') {
      zone=1;
   } else if (code == 'j') {
      zone=1;
   } else if (code == 'm') {
      zone=1;
   } else if (code == 'e') {
      zone=2;
   } else if (code == 'f') {
      zone=2;
   } else if (code == 'h') {
      zone=2;
   } else if (code == 'g') {
      zone=3;
   } else if (code == 'r') {
      zone=3;
   } else if (code == ch1) {
      zone=4;
   } else if (code == ch2) {
      zone=4;
   } else {
      zone=0;
   }
   cout << zone << endl;
   return 0;
}
Last edited on
No I did not. I retract my previous statement then.
Mottman: Thanks for your advise, but wat i want is others than the character i put into the
switch, it will cout zone 4, if i key in others.So, how to switch all the others in a case so
it will show zone 4?
Kyon: Actually this is my past year paper in which the question ask us to change the if and else if
to the switch case.
Case is not superior to else if, in fact, it's worse in most cases (pun not intended), since case will not allow you to check non-const values. (Like objects or other values that are not literals.) In your case, it's not really a good idea to use switches. Furthermore, in your code example, you were coutting "zone = 1", "zone = 2", etc, and you had an integer called zone. I don't know if you expect the program to do something with that value, but it won't. I fixed this in my example too.

Shortly put, it's impossible to check for non-constant values in a case-statement.
Last edited on
closed account (D80DSL3A)
Maybe he's asking about the default case
1
2
3
case default:
cout << "zone4" << endl;
break;

This would print "zone4" for any value entered that is not on the list for other cases.
His question was kind of unclear, and that would indeed be the right solution, except that it's not case default:, but default:.
fun2code:Ya, this is the answer i want, thanks:)
Topic archived. No new replies allowed.