Home > Enterprise >  if statement in c not output correctly
if statement in c not output correctly

Time:10-20

    printf("Golongan Rumah Tangga A1\n");
    printf("Masukan pemakaian \n");
    scanf("%f", &pemakaian);

    if ( pemakaian <= 10 ) {
        tarif = 1.780;
        printf("Tarif : %.3f\n", tarif);
    }
    else if ( pemakaian > 11 || pemakaian <= 20 ) {
        tarif = 2.060;
        printf("Tarif : %.3f\n", tarif);
    }

when I input 2 the result is still 2,060

which should be 1,780

this is my full code

#include <stdio.h>

int main() {

int klaster, golongan, listrik, pemakaian;
double berm, tarif;

    printf("Program air\n");

    printf("Pilih klaster\n ");
    printf("1. Rumah Tangga\n");
    printf("2. Usaha\n");

    scanf("%d", &klaster);
    if (klaster == 1) {
        printf("pilih golongan\n");
        printf("1. Subsidi\n");
        printf("2. tanpa subsidi\n");
        scanf("%d", &golongan);
        if( golongan == 1) {
            printf("Masukan daya listik\n");
            scanf("%d", &listrik);

            printf("Masukan lebar berm\n");
            scanf("%f", &berm);

            if (listrik == 450 && berm <=3.99 ) {
                printf("Golongan Rumah Tangga A1\n");
                printf("Masukan pemakaian \n");
                scanf("%f", &pemakaian);

                if ( pemakaian <=10 && pemakaian > 0 ) {
                    tarif = 1.780;
                    printf("Tarif : %.3f\n", tarif);
                }
                else if ( pemakaian > 11 ) {
                        tarif = 2.060;
                        printf("Tarif : %.3f\n", tarif);
                }
            } 
            else if (listrik == 900 && berm <=3.99 ) {
                printf("Golongan Rumah Tangga A1\n");
                printf("Masukan pemakaian \n");
                scanf("%f", &pemakaian);

                if ( pemakaian <= 10 ) {
                    tarif = 1.780;
                    printf("Tarif : %.3f\n", tarif);
                }
                else if ( pemakaian > 11 || pemakaian <= 20 ) {
                    tarif = 2.060;
                    printf("Tarif : %.3f\n", tarif);
                }
                else if ( pemakaian > 20 ) {
                    tarif = 5.880;
                    printf("Tarif : %.3f\n", tarif);
                }
            }


        }else {
            printf("Input yang benar woiii");
        }

    }else{
        printf("Input yang benar woiii");
        
    }
}

CodePudding user response:

When reading a float with scanf("%f", &pemakaian) (notice the %f flag), a binary value of 0b01000000000000000000000000000000 is read for a decimal input 2, which later is stored in an int variable pemakaian as a 4194304 decimal.

You can find out more about the binary format of the float numbers in C here.

The easiest solution for this is to either read an integer with scanf("%d", &pemakaian) (notice the %d flag), or initialize pemakaian as a float.

CodePudding user response:

If pemakaian is a char, then "2" has ASCII value 50, which is larger than 11, so the output looks normal.

Please check the type and the value of pemakaian or print it as output in order to be sure what the value is.

  • Related