0
What is error in below source code please identify and suggest me..
https://code.sololearn.com/cYdC1lhIj94A/?ref=app https://code.sololearn.com/cVtCr8x8jNJF/?ref=app
5 ответов
+ 7
The switch statement only allows CHAR and INT data types.
Char refers to a single letter, symbol, number while String refers to set of characters.
'&' -> character
"&&" -> string
"=>" -> string
+ 4
It's better to use if-else statements for this kind of things.
Somehow,
https://stackoverflow.com/questions/4014827/how-can-i-compare-strings-in-c-using-a-switch-statement
+ 3
In C, switch is only used with integers and characters that it converts to numbers, not with strings.
You need to bring the strings to a number and only then use switch.
#include <stdio.h>
int main()
{
int a,b,c;
char ch;
printf("\n enter operators (&,|,!):\n");
scanf("%c",&ch);
printf ("\n enter two operands\n");
scanf("%d%d",&a,&b);
switch (ch)
{
case '&': //debug
c=a&&b;
printf ("c=%d",c);
break;
case '|': //debug
c=a||b;
printf ("c=%d",c);
break ;
case '!':
c=a!=b; //debug, (or: c=!a)
printf ("c=%d",c);
break;
default :
printf ("\n enter only logical operator\n");
}
}
+ 2
Also, in operands input, use a separator, like a space, in scanf format spec. This way, the command can distinguish the two operands.
0
How can we use strings in switch statement ??
can anyone give example of this code..