What is error in below source code please identify and suggest me.. | Sololearn: Learn to code for FREE!


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

1/19/2022 1:54:43 AM


5 Answers

New Answer


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


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


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"); } }


Also, in operands input, use a separator, like a space, in scanf format spec. This way, the command can distinguish the two operands.


How can we use strings in switch statement ?? can anyone give example of this code..