Problem with calculator program

Asked

Viewed 294 times

4

Good morning, I would like to know, why is my calculator program not printing the value of 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;
}
  • 1

    Mete default in the switch! Turn on as many warnings as possible from your compiler and pay attention to them!

  • 3

    Remove the quotes from the 'op': switch(op)

  • I’ve removed the quotes and you still have the same problem

  • in the default of switch, prints the value of op.

  • As I said in my reply it is not possible to put strings in the case.

5 answers

8

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

1.0ENTER
+ENTER

And the program ends, without asking for the third number! That’s because the ENTER after the first float is considered the character you were expecting in the second scanf. If you type the whole expression in one line, the program works normally:

1.0+2.0ENTER

A simple solution (but not very pretty) to read line by line is to put a getchar after reading the first float:

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

http://ideone.com/Guc1Kj

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

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

http://ideone.com/2n5uMO

  • @Jjoao Really works, and is much cleaner. I will mention this in the answer, if I may. I know very little about C...

  • Force! (this question is tricky and rarely described in a way that is understood)

3

Sorry to answer again but now in a way more according to what the OP intention.

  1. case 43 --> case '+' makes things more readable

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

  3. in format scanf spaces mean zero or whites (spaces and " n") which is very important for operator reading.

  4. 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;
}
  • Posting another answer was the right attitude, since they are two different approaches to the problem.

  • @bfavaretto, I’m "talking" too much, but despite a little redundant I ended up writing this because of the spaces in the scanf (which is importantissiomo and almost nobody knows)

1

If I’m not mistaken the switch of C is only of integer primitive values (this may have changed in the latest versions) another detail in char op you are assigning two characters soon the last will not be caught so even if it were possible to catch char on switch will never match one case so that it can pick up two or more characters you should declare a character array

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

That’s 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;
}
  • 3

    43 is the ASCII code for +, lol

1

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

The typical structure of a program flexis:

%%
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 format sscanf).

  • What is the C code that this generates? I think it could be part of the answer.

  • 1

    @bfavaretto, The generated code is complicated -- flex creates a yylex function with a deterministic automaton (3 or 4 matrix vectors) with the union of all the expreg used, and optimized until exastan: it is not at all readable, but stupidly efficient...

0

I may be wrong but I did not find any value being passed to enter the switch. I did it that way 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;
        }
  • my teacher asked me to be of the char type

Browser other questions tagged

You are not signed in. Login or sign up in order to post.