Creating a simple calculator in C

I am trying to write a simple C script calculator using only the basic +, -, /, *. I have the following, but I'm not sure why it is 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 printed? I'm something wrong here

 result = number1 + number2; 
+1
source share
3 answers

"Why is the result not printed?"

You correctly calculate the answer, but do not print it anywhere.

You need something like:

 printf("Answer: %f + %f = %f\n", number1, number2, result); 

Without a print statement, nothing is printed.


EDIT Reply to comment:

You did printf after , did you calculate the result? Personally, I would put printf immediately before getchar ();

For more debugging, immediately after scanning, I would write:

 printf("Input as received: number1 is %f\n number2 is %f\nsymbol is %c\n", number1, number2, symbol); 

If this does not display the input you entered, then something is wrong with how you collect the input.

+6
source

You never print the result ...

You need to add something like this:

 printf("Result: %f", result); 
+17
source
 /* I think I see the problem; you're trying to reinvent the wheel. */ #include &lt;stdio.h> #include &lt;stdlib.h> int main (void) { system("/bin/bc"); return 0; } 
0
source

All Articles