sir i have a problem in the c program.

switch(ch)
{
case 1: t=a+b;break;
case 2: t=a-b;break;
case 3: t=a*b;break;
case 4: t=a/b;break;
default:printf("invalid no:");
}

when i execute the program and the any choice the comipler skip to the default statement. is it any problem in the syntax. sir please solve my problem.

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

I will not solve your problem, but I will help you with it. My first thoughts are on your case statements, making sure that the values match up to what is coming in on 'ch'.

In the future, when posting a question, please post it to a new thread, instead of an old one.

sir i have a problem in the c program.

switch(ch)
{
case 1: t=a+b;break;
case 2: t=a-b;break;
case 3: t=a*b;break;
case 4: t=a/b;break;
default:printf("invalid no:");
}

when i execute the program and the any choice the comipler skip to the default statement. is it any problem in the syntax. sir please solve my problem.

Then the value of ch is not 1, 2, 3, nor 4. There's nothing wrong with the code other than being poorly formatted.

I'm guessing you may have meant something along the lines of:

switch(ch)
{
case '1': t=a+b;break;
case '2': t=a-b;break;
case '3': t=a*b;break;
case '4': t=a/b;break;
default:printf("invalid no:");
}

Notice that the check is for the character values not the integer values.

i think on getting the value of the "ch" variable you might have given
"scanf("%d",ch);" instead of "scanf("%d",&ch);"
check once for this

#inlcude//include functions which u want
main(0
{
char ch;//ch is character
switch(ch)//Character Values
{
case '1':t=a+b;break;
case '2':t=a-b;break;
case '3':t=a*b;break;
case '4';t=a/b;break;
default:printf("invalid character");//at the end of the switch there is need of break statement
}
}


In your question u taken 1,2,3 and 4 as numbers but u given "switch(ch)" like characters ....Make sure that if u r giving character values in the switch give characters, if u giving integer values in the switch give integer values....

Both must be same

This question has already been answered. Start a new discussion instead.