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.