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.

Recommended Answers

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, …

Jump to Post

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 …

Jump to Post

All 6 Replies

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.

Print the value of coming in from ch

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.