Hi,

I am trying to create a basic calculator program using the switch case method ...The following code runs fine for me

------------------------------------------------------------------

/* calculator using the switch case method ,zero divide taken into account,invalid op messsage added*/

#include<stdio.h>

int main()

{

int a,b;

float c;

printf("Please enter the values for a and b respectively:");

scanf("%d %d",&a,&b);

fflush(stdin);

char op;

printf("Please enter the mathematical operator:");

scanf("%c",&op);

fflush(stdin);

if (op=='+'||op=='-'||op=='*'||op=='%'||op=='/')

{

switch (op)

{

case '+': printf("The sum of %d + %d is %d ",a,b,a+b);

break;

case '-'rintf("The difference between %d and %d is %d",a,b,a-b);

break;

case '*'rintf("The product of %d and %d is %d",a,b,a*b);

break;

case '%':

if (b==0)

printf("divide by zero not possible please enter a number above 0 and rerun");

else

{

c=a%b;

printf("The modulus of %d and %d is %.2f",a,b,c);

}

break;

case '/':

if (b==0)

printf("No integer can be divided by zero please enter a number above 0 and rerun");

else

{

c=a/b;

printf("The result of %d / %d is %.2f",a,b,c);

}

break;

default: printf("Non integers entered please rerun the program,Thanks");

}

}

else printf("Operator entered cannot be tackled by this program,Sorry");

getchar();

}

----------------------------------------------------------------------

But when I try to convert all the input data to floats it throws up an error when I either use the modulus operator or the divide operator can u please guide me as to where exactly I am going wrong .the code is

--------------------------------------------------------------

/* calculator using the switch case method ,zero divide taken into account,invalid op messsage added*/

#include<stdio.h>

int main()

{

float a,b,c;

printf("Please enter the values for a and b respectively:");

scanf("%f %f",&a,&b);

fflush(stdin);

char op;

printf("Please enter the mathematical operator:");

scanf("%c",&op);

fflush(stdin);

if (op=='+'||op=='-'||op=='*'||op=='%'||op=='/')

{

switch (op)

{

case '+': printf("The sum of %.2f + %.2f is %.2f ",a,b,a+b);

break;

case '-'rintf("The difference between %.2f and %.2f is %.2f",a,b,a-b);

break;

case '*'rintf("The product of %.2f and %.2f is %.3f",a,b,a*b);

break;

case '%':

if (b==0)

printf("divide by zero not possible please enter a number above 0 and rerun");

else

{

c=a%b; /* THE COMPILER THROWS AN ERROR INDICATING "INVALID OPERANDS TO BINARY" IS IT GOT TO DO

WITH THE FACT THAT THE VALUES ARE IN FLOAT OR IS THERE SOMETHING I AM DOING WRONG*/

printf("The modulus of %.2f and %.2f is %.2f",a,b,c);

}

break;

case '/':

if (b==0)

printf("No integer can be divided by zero please enter a number above 0 and rerun");

else

{

c=a/b;

printf("The result of %.2f / %.2f is %.2f",a,b,c);

}

break;

default: printf("Non integers entered please rerun the program,Thanks");

}

}

else printf("Operator entered cannot be tackled by this program,Sorry");

getchar();

}

------------------------------------------------------------

Also just to take this program even further is there a method where I can check if I have entered valid numbers before the program enters the switch mode i.e at this point if I enter a character in the place of a number the compiler converts the character to its ascii number and returns the result but is there anyway I can stop the program from running any further when anything other than a valid number is entered.

I would appreciate if someone can just guide me where to look to find a solution for the same rather than letting me know the answer.

Thanks a lot in advance.