Thread: Basic:switch Program

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    8

    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 '-'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.

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    8
    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

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If you click "Go Advanced" instead of "Post Quick Reply", there is an option to disable smilies.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    or better - use code tags...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    8
    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.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    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..

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you cannot use &#37; on floats...

    you should not fflush(stdin) - read the FAQ
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    matsp is true , read FAQ. (By the way he is really true , I dont mean opposite.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM