Swich case problem???

Joined
Feb 2, 2022
Messages
2
Reaction score
0
hi guys when I turn into float to int it doesnt work.what is the problem over here??
float s1,s2,s3;
char s4;
//s3=s1+s2;
//printf("sonuc: %d\n",s3);
printf("Birinci Sayisi Giriniz:");
scanf("%f",&s1);
printf("Ikinci Sayiyi Giriniz: ");
scanf("%f",&s2);

printf("Yapmak Istediginiz Islemi Giriniz:");
scanf("%s",&s4);


switch(s4)
{
case '+':
s3=s1+s2;
printf("Sonuc: %f",s3);
break;

case '-':
s3=s1-s2;
printf("Sonuc: %f",s3);
break;

case'/':
s3=s1/s2;
printf("Sonuc: %f",s3);
break;

case '*':
s3=s1*s2;
printf("Sonuc: %f",s3);
break;

}
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
That was a tricky one, well done! Alrighty, so the problem is that your operator/action is only a single character but the last scanf is reading a string, which is writing the action AND a null character. The reason is that the way the variables are stored in memory is actually putting your s4 first, then your s2. It's writing your operator into s4, then writing the null character (a zero) into the lowest byte of s2. Why it's stored that way is beyond my knowledge. Anyway, the easiest solution is to make your s4 two characters long and just look at the first. For safety, I've also limited that scanf to a single character, so the user can't type in a bunch of text and overflow into memory your program doesn't own (segfault).

C:
void main()
{
    int s1, s2, s3;
    char s4[2];
    //s3=s1+s2;
    //printf("sonuc: %d\n",s3);
    printf("Birinci Sayisi Giriniz: ");
    scanf("%d",&s1);
    printf("Ikinci Sayiyi Giriniz: ");
    scanf("%d",&s2);

    printf("Yapmak Istediginiz Islemi Giriniz:");
    scanf("%1s",&s4);

    switch(s4[0])
    {
        case '+':
        s3=s1+s2;
        printf("Sonuc: %d\n",s3);
        break;

    case '-':
        s3=s1-s2;
        printf("Sonuc: %d\n",s3);
        break;

    case'/':
        s3=s1/s2;
        printf("Sonuc: %d\n",s3);
        break;

    case '*':
        s3=s1*s2;
        printf("Sonuc: %d\n",s3);
        break;
    }
}
 
Joined
Feb 2, 2022
Messages
2
Reaction score
0
That was a tricky one, well done! Alrighty, so the problem is that your operator/action is only a single character but the last scanf is reading a string, which is writing the action AND a null character. The reason is that the way the variables are stored in memory is actually putting your s4 first, then your s2. It's writing your operator into s4, then writing the null character (a zero) into the lowest byte of s2. Why it's stored that way is beyond my knowledge. Anyway, the easiest solution is to make your s4 two characters long and just look at the first. For safety, I've also limited that scanf to a single character, so the user can't type in a bunch of text and overflow into memory your program doesn't own (segfault).

C:
void main()
{
    int s1, s2, s3;
    char s4[2];
    //s3=s1+s2;
    //printf("sonuc: %d\n",s3);
    printf("Birinci Sayisi Giriniz: ");
    scanf("%d",&s1);
    printf("Ikinci Sayiyi Giriniz: ");
    scanf("%d",&s2);

    printf("Yapmak Istediginiz Islemi Giriniz:");
    scanf("%1s",&s4);

    switch(s4[0])
    {
        case '+':
        s3=s1+s2;
        printf("Sonuc: %d\n",s3);
        break;

    case '-':
        s3=s1-s2;
        printf("Sonuc: %d\n",s3);
        break;

    case'/':
        s3=s1/s2;
        printf("Sonuc: %d\n",s3);
        break;

    case '*':
        s3=s1*s2;
        printf("Sonuc: %d\n",s3);
        break;
    }
}
thank you very much,mate!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top