I have a problem about float

I tried to do calculator but when I play the program it gives wrong result.

int secim,secim2,s1,s2;

printf("Dort islem icin :1\n");
printf("Ozel matematik fonksiyonlari icin :2\n\n");
printf("Secim yapiniz :"); scanf("%d",&secim);
switch(secim)
{
case 1:
{
printf("Toplama icin :1\n");
printf("Cikarma icin :2\n");
printf("Carpma icin :3\n");
printf("Bolme icin :4\n");
secimyap:printf:("Secim yapiniz :"); scanf("%d",&secim2);

if(secim2==1)
{
printf("2 adet sayi giriniz :"); scanf("%d%d",&s1,&s2);
printf("Toplam:%.d",s1+s2);
}
else if(secim2==2)
{
printf("2 adet sayi giriniz :"); scanf("%d%d",&s1,&s2);
printf("Fark:%d",s1-s2);
}
else if(secim2==3)
{
printf("2 adet sayi giriniz :"); scanf("%d%d",&s1,&s2);
printf("Carpim:%d",s1*s2);
}
else if(secim2==4)
{
printf("2 adet sayi giriniz :"); scanf("%f%f",&s1,&s2);
printf("Bolum: %.1f",(float)s1/(float)s2);
}
else
{
printf("Lutfen 1-4 arasi bir deger giriniz.");goto secimyap;
}
}
Since s1/s2 are not float. You can not scan them as such. I.e. scanf("%f%f",&s1,&s2); is wrong. Use float variables for that.
Must I turn all %d's to %f and do that?=> int secim,secim2; float s1,s2;
Not necessarily. You may introduce additional variables:
1
2
3
4
5
6
else if(secim2==4)
{
float f1, f2;
printf("2 adet sayi giriniz :"); scanf("%f%f",&f1,&f2);
printf("Bolum: %.1f",f1/f2);
}
Note that you program will crash if you have a division by zero.
I did what you said but this time it gives me 0.00 every time. :(
I checked it:
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>

int main ()
{
float f1, f2;
printf("2 adet sayi giriniz :"); scanf("%f%f",&f1,&f2);
printf("Bolum: %.1f",f1/f2);

  return 0;
}
It works as expected.
Firstly, I m so thankful to you. I am sory. I just missed to turn %s to %f. See you soon :D
Topic archived. No new replies allowed.