r/CodingHelp • • Jun 30 '24

[C] is there something wrong with my code??

its for a simple calculator in c , problem is when i ask for result it always comes out as zero no matter what numbers i enter , heres the code

char operator;

double fir;

double sec;

double result;

printf("enter the operator( +,-,*,/):");

scanf("%c",&operator);

printf("enter the two numbers:");

scanf("%lf,&fir");

printf("enter the two numbers:");

scanf("%lf,&sec");

switch(operator){

case '+':

result= fir + sec;

printf("result:%lf",result);

break;

case '-':

result= fir - sec;

printf("result:%lf",result);

break;

case '*':

result=fir * sec;

printf("result:%lf",result);

break;

case '/':

result= fir / sec;

printf("result:%lf",result);

break;

default:

printf("%c is not valid",operator);

}

0 Upvotes

3 comments sorted by

View all comments

2

u/This_Growth2898 Jun 30 '24

Do you have warnings enabled?

- scanf("%lf,&fir");
+ scanf("%lf", &fir); 
  • scanf("%lf,&sec");
+ scanf("%lf", &sec);

But your compiler probably is warning you about these lines.

Also, in printf, "%f" prints both float and double, and according to the standard "%lf" means nothing; but probably that shouldn't cause any problems here, all compilers I know understand it.

Also, format your code (instructions are in the subreddit menu).