-1

So I'm learning some C and I've made a simple calculator that will perform some basic calculations depending on what a user enters. The program uses a switch structure and thats really about it. Evey time I enter a calculation just gives me 0.000000. Theres no errors givin just the wrong answers. The program was created using VS Code. The program:

/*
The problem is to write a simple calculator that can add, subtract, multiply, divide, and find the 
remainder when one
number is divided by another. The program must allow the calculation that is to be performed to be 
keyed in a natural
way, such as 5.6 * 27 or 3 + 6.
*/

#include<stdio.h>

int main(int argc, char* argv[])
{
  //declare and initialise objects
  double num1 = 0.0; //1st operand value
  double num2 = 0.0; //2nd operand value
  char operation; //operations that will be performed: +, -, *, /, %

  //user input
  printf("Enter the calculation: ");
  scanf("%1f %c %1f", &num1, &operation, &num2);

  //calculations
  switch (operation)
  {
    case '+':
        printf(" = %1f\n", num1 + num2);
        break;
    
    case '-':
        printf(" = %1f\n", num1 - num2);
        break;

    case '*':
        printf(" = %1f\n", num1*num2);
        break;

    case '/':
        if (num2 == 0) //can't divide by zero
        {
            printf("\n\nDivision by zero error!\n");
        }
        else
        {
                printf(" = %1f\n", num1/num2);
        }
        break;
        
    case '%':
        if ((long)num2 == 0) //can't divide by zero
        {
            printf("\n\nDivision by zero error!\n");
        }
        else
        {
                printf(" = %1ld\n", (long)num1 % (long)num2);
        }
        break;

    default:
        printf("\n\naIllegal Operation!"); //operation is illegal if we get to here
        break;
        
}
//end of program
return 0;
}

Example:

Enter the calculation: 4.7-3.2
= 0.000000

I feel the problem is very simple, however Google has failed me because my "search" seems too specific. Any one with an idea of whats going on here would be greatly appreciated.

MRL
  • 389
  • 5
  • 22

1 Answers1

3

You absolutely have to be using and understanding compiler warnings. If you'd understood the reasons for them and then addressed them, there would have been no need to post here. These are the warnings I get when compiling the code you originally posted:

main.c:12:11: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat=]                                                              
main.c:12:17: warning: format ‘%f’ expects argument of type ‘float *’, but argument 4 has type ‘double *’ [-Wformat=]                                                              
main.c:47:30: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]

The offending warnings are in your scanf, and the printf in the '%' case for printing an equation.

In order to read in a double value via scanf, the format specifier is %lf, not %f.

See: Reading in double values with scanf in c

In order to print out a long int value via printf, the format specifier is %ld, not %d.

See: What is the argument for printf that formats a long?

So, your code needs to have:

scanf("%lf %c %lf", &num1, &operation, &num2);

And (compared to what was in your original post which just had %1d):

printf(" = %1ld\n", (long)num1 % (long)num2);
Random Davis
  • 6,662
  • 4
  • 14
  • 24