Problem with calculator program

4

Good morning, I would like to know, because my calculator program is not printing the value of the operations? Below is the code:

#include <stdio.h>

int main (void)

{

    float A, B;
    char op;

    scanf("%f", &A);
    scanf("%c", &op);
    scanf("%f", &B);

    switch('op')
            {

            case 43 : printf("%f", A + B);
            break;

            case 45 : printf("%f", A - B);
            break; 

            case 42 : printf("%f", A * B);
            break;

            case 47 : printf("%f", A / B);
            break;

            }

return 0;
}
    
asked by anonymous 28.04.2015 / 16:10

5 answers

8

In addition to the switch('op') problem, which should be without quotation marks as already mentioned in the comments, your scanf is not consuming the input the way you imagine it. Let's test for example the sum of 1.0 and 2.0 . You type:

  

% w / w

And the program ends, without asking for the third number! This is because ENTER after the first float is considered the character you were expecting in the second 1.0 . If you type the entire expression in a single line, the program works normally :

  

% com_of ENTER

A simple (but not very pretty) solution for reading row by line is to place a + after reading the first float:

scanf("%f", &A);
getchar();
scanf("%c", &op);
scanf("%f", &B);

link

Or, as suggested by @JJao, simply add a space in what is expected by the second scanf :

scanf("%f", &A);
scanf(" %c", &op);
scanf("%f", &B);

link

    
28.04.2015 / 16:39
3

Sorry to respond again but now more in accordance with what the OP intended.

  • case 43 - > case '+' makes things readable

  • scanf returns a value (number of elements read) - can be used for validation

  • Vertical symmetries in indentation make things more readable

  • Code:

    #include <stdio.h>
    
    int main (){
       float A, B;
       char op;
    
       if (scanf("%f %c %f", &A, &op , &B) == 3)
           switch(op) {
              case '+' : printf("%f", A + B); break;
              case '-' : printf("%f", A - B); break; 
              case '*' : printf("%f", A * B); break;
              case '/' : printf("%f", A / B); break;
    
              default : printf("???\n"); 
          }
    
       return 0;
    }
    
        
    28.04.2015 / 18:22
    1

    If I am not mistaken, the switch of C is only of primitive integer values (this may have changed in the last versions) another detail in char op you are assigning two characters so the last one will not be picked up then even if it were possible catch char in switch will never match a case so you can get two or more characters you should declare an array of characters

    char op [3]; // the first two positions for 4 and 3 and the last position for /0

    How it works:

    #include <stdio.h>
    
    int main (void)
    
    {
    
        float A = 3;
        float B = 9;
        int op = 43;
    
        //scanf("%f", &A);
       // scanf("%c", &op);
       // scanf("%f", &B);
    
        switch(op)
                {
    
                case 43 : printf("%f", A + B);
                break;
    
                case 45 : printf("%f", A - B);
                break; 
    
                case 42 : printf("%f", A * B);
                break;
    
                case 47 : printf("%f", A / B);
                break;
    
                }
    
    return 0;
    }
    
        
    28.04.2015 / 16:24
    1

    One of the most comfortable ways to write C is to generate it. In this case with flex .

    The typical structure of a flex program is:

    %%
    regExp    {ação C}
    ...
    %%
    

    In our case the file calc.fl is:

    F   [ ]*[0-9]+(\.[0-9]+)?[ ]*
    %%
      float A, B;
    
    {F}\+{F}    { sscanf(yytext,"%f + %f",&A, &B);   printf("%f\n", A+B); }
    {F}\*{F}    { sscanf(yytext,"%f * %f",&A, &B);   printf("%f\n", A*B); }
    {F}\/{F}    { sscanf(yytext,"%f / %f",&A, &B);   printf("%f\n", A/B); }
    {F}\-{F}    { sscanf(yytext,"%f - %f",&A, &B);   printf("%f\n", A-B); }
    \n          {}
    
    %%
    

    To compile:

    flex -o calc.c calc.fl
    cc  -o calc cal.c -lfl 
    

    This solution accepts questions repeatedly until CTR-D; the user can insert spaces and new-lines freely (spaces in the format sscanf ).

        
    28.04.2015 / 17:39
    0

    I may be wrong but I did not find any value being passed to get into the switch. I did that and it worked:

    int main (void)
    
    {
    
    float A, B;
    int op;
    
    printf("Entre com a opcao: ");
    scanf("%d", &op);
    printf("Entre com o valor de A: ");
    scanf("%f", &A);
    printf("Entre com o Valor de B: ");
    scanf("%f", &B);
    
    
    
    switch(op)
            {
    
            case 43 : printf("%f", A + B);
            break;
    
            case 45 : printf("%f", A - B);
            break; 
    
            case 42 : printf("%f", A * B);
            break;
    
            case 47 : printf("%f", A / B);
            break;
    
            }
    
            return 0;
            }
    
        
    28.04.2015 / 16:26