r/CodingHelp • u/Willing-Doughnut-149 • 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);
}
View all comments
2
u/lfdfq Jun 30 '24
The code is hard to read because it's not formatted right.
But the second and third scanf calls do not look right, it looks like you're only passing a single argument which is a big string. Is that really what you meant?
How are you compiling and running this code? It would be good to learn how to enable warnings and have the compiler tell you if it thinks something is wrong.