# Basic:switch Program

Printable View

• 07-25-2007
time789
Basic:switch Program
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 '-':printf("The difference between %d and %d is %d",a,b,a-b);
break;

case '*':printf("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 '-':printf("The difference between %.2f and %.2f is %.2f",a,b,a-b);
break;

case '*':printf("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.
• 07-25-2007
time789
HI,

The smiles are something I did not add,probably it was because I did not add some space between the : and p of printf .Sorry
• 07-25-2007
robatino
If you click "Go Advanced" instead of "Post Quick Reply", there is an option to disable smilies.
• 07-25-2007
vart
or better - use code tags...
• 07-25-2007
time789
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.
• 07-25-2007
robatino
• 07-25-2007
ozumsafa
Time789 , I offer you to solve your problem by yourself . See ? All answers you can get :

1-See FAQ
2-Use Code Tags
3-How are yaa??
4-You look beautiful
5- I am the best!!!! which is the most possible..
• 07-25-2007
vart
you cannot use &#37; on floats...

you should not fflush(stdin) - read the FAQ
• 07-25-2007
matsp
You may be able to use "fmod": http://www.hmug.org/man/3/fmod.php

But as was stated, % is not a valid operation on floating point (What would "2.3 % 4.6" actually mean?)

--
Mats
• 07-25-2007
ozumsafa
matsp is true , read FAQ. (By the way he is really true , I dont mean opposite.)