-
Char in a Switch()
Another question by me!
How can I use a Char in a switch()?
I tried it like this:
Code:
char x[2];
cout<<"Write A or B";
cin>>x;
switch((char)x);
{
case("A"):
cout<<"You wrote <<x<<"!";
break;
case("B"):
cout<<"You wrote <<x<<"!";
break;
default:
cout<<"Bad input!";
}
But I get an error message saying:" case label "A" does not reduce to an integer constant "
I know it's easy to fix.. but how?
-
For char you would test it as 'A' not "A". The first is the intrigral value. The second is the string representation.
-
Re: Char in a Switch()
Code:
char x[2];
cout<<"Write A or B";
cin>>x;
switch(x[0]);
{
case('A'):
cout<<"You wrote <<x<<"!";
break;
case('B'):
cout<<"You wrote <<x<<"!";
break;
default:
cout<<"Bad input!";
}
Fixed
-
Thx alot! That was all I needed to know!
-
FillYourBrain Reply!
OW ! One more thing about your post, FillYourBrain, the code only works when I write it like
switch((char)IC) and not like switch(IC[0])
-
PS
I used the char name ' IC ' in my own script :D
-
what is the type if IC?
char IC[2];
or what??