I am trying to write a simple C calculator script, using only the basic +, -, /, *. I have the following, but I’m not sure why it’s not printing correctly.
#include<stdio.h>
#include<stdlib.h>
int main (void)
{
//introduce vars
double number1, number2, result;
char symbol; //the operator *, -, +, /
//allow user interaction
printf("Enter your formula \n");
scanf("%f %c %f", &number1, &symbol, &number2);
switch (symbol) {
case '+':
result = number1 + number2;
break;
default:
printf("something else happened i am not aware of");
break;
}
getchar();
return 0;
}
Why is the result not being printed? Am I doing something wrong here,
result = number1 + number2;
You calculate the answer properly, but do not print it anywhere.
You need to have something like:
Without a print statement, nothing gets printed.
EDIT Responding to comment:
Did you do the printf after you calculate the result?
Personally, I would put the printf just before the getchar();
For more debugging, just after your scanf, I would write:
If that does not show the input that you typed, then something is wrong with how you gather input.