Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a calculator program, coding in C, and it can't divide 1/2 correctly, it gives 0.000000. So therefore I ask you how to fix the code so that the program divides correctly?

The code:
C++
#include <stdio.h>
#include <stdlib.h>

int main(){

    double num1, num2;
    char op;

    printf("Enter num1: ");
    scanf("%d", &num1);

    getchar();

    printf("Enter Operator: ");
    scanf("%c", &op);

    printf("Enter num2: ");
    scanf("%f", &num2);

    if(op == '+'){
        printf("%f", num1 + num2);
    } else if(op == '-'){
     printf("%f", num1 - num2);
    } else if(op == '/'){
     printf("%f", num1 / num2);
    } else if(op == '*'){
     printf("%f", num1 * num2);
    } else {
     printf("Invalid Operator");
    }

    return 0;
}


What I have tried:

I have tried to make double variables, in the code from this guide, instead of int variables and also changing the
%d
's to
%f
's. I have also added a
getchar();
line to catch some input which is making a problem otherwise.
Posted
Updated 20-Dec-20 20:45pm
v2

C++
printf("Enter num1: ");
scanf("%d", &num1);       // <-- this is wrong

getchar();                // <-- this is unnecessary

printf("Enter Operator: ");
scanf("%c", &op);

printf("Enter num2: ");
scanf("%f", &num2);       // <-- this one is correct
I hope that tells you where the problems are. The first scanf should look like the last one there and use the "%f" format string.

If I were you, I would write the last part like this :
C++
double result = 0;
int valid = 1;

if( op == '+' ) {
    result = num1 + num2;
} else if( op == '-' ) {
    result = num1 - num2;
} else if( op == '/' ) {
    result = num1 / num2;
} else if( op == '*' ) {
    result = num1 * num2;
} else {
    printf( "Invalid Operator\n" );
    valid = 0;
}
if( valid )
    printf( "result is %f\n", result );
 
Share this answer
 
Comments
Rick York 20-Dec-20 18:01pm    
Note : Griff's comment about the switch statement is a good one - that is preferred to the if/else arrangement there.
Mieczyslaw1683 20-Dec-20 18:23pm    
This code made the calculator to work correctly:
#include <stdio.h>
#include <stdlib.h>

int main(){

    float num1, num2;
    char op;

    printf("Enter num1: ");
    scanf("%f", &num1);

    getchar();

    printf("Enter Operator: ");
    scanf("%c", &op);

    printf("Enter num2: ");
    scanf("%f", &num2);

    /* printf("%f\n", num1); */

    if(op == '+'){
        printf("%f", num1 + num2);
    } else if(op == '-'){
     printf("%f", num1 - num2);
    } else if(op == '/'){
     printf("%f", num1 / num2);
    } else if(op == '*'){
     printf("%f", num1 * num2);
    } else {
     printf("Invalid Operator");
    }

    return 0;
}


Thank you Rick York for learning me to change that %d to %f which made me use float and with float the calculator works. I still need that getchar line because when I press Enter I get like a double input.
Start by finding out what it's working with: the simplest and quickest way (in the long run) is to use the debugger - a quick google for the name of your IDE and "debugger" will find you the instructions - but adding printf statements to show the content of num1, num2, and op before entering the if statement will do at a pinch.

When you know exactly what it's processing, you can start looking at why - but until then you are just guessing.

BTW: I'd strongly recommend you look at using a switch statement instead of extended if ... else if ... else if ... blocks.
 
Share this answer
 
This is what I was striving for and now it works thanks to Rick York. :)
#include <stdio.h>
#include <stdlib.h>

int main(){

    float num1, num2;
    char op;

    printf("Enter num1: ");
    scanf("%f", &num1);

    getchar();

    printf("Enter Operator: ");
    scanf("%c", &op);

    printf("Enter num2: ");
    scanf("%f", &num2);

    /* printf("%f\n", num1); */

    if(op == '+'){
        printf("%f", num1 + num2);
    } else if(op == '-'){
     printf("%f", num1 - num2);
    } else if(op == '/'){
     printf("%f", num1 / num2);
    } else if(op == '*'){
     printf("%f", num1 * num2);
    } else {
     printf("Invalid Operator");
    }

    return 0;
}
 
Share this answer
 
v2
Comments
Shao Voon Wong 20-Dec-20 19:36pm    
You should mark Rick York's post as the answer in that case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900