Basic:switch Program

This is a discussion on Basic:switch Program within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to create a basic calculator program using the switch case method ...The following code runs fine ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,457
    or better - use code tags...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    As vart says, use code tags:

    << !! Posting Code? Read this First !! >>

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,457
    you cannot use &#37; on floats...

    you should not fflush(stdin) - read the FAQ
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 12: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, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21