0

I'm new to C Programming. Im really confused why printf gives undefined behavior output. I tried to double-check every command and I don't think there is a mistake with my current knowledge. What was wrong with my code?

struct Barang
    {
        int Kode;
        int Jumlah;
        int HargaSatuan;
        float Total;
        char Nama[20];
    }Brg[10];

    int i,j,harga=0,banyak;
    float rata=0;
    printf("Berapa banyak barang? : ");
    scanf("%d",&banyak);
    for (i=0;i<banyak;i++)
    {
        printf("\nBarang Ke-%d\n",i+1);
        printf("Masukkan Kode : ");
        scanf("%d",&Brg[i].Kode);
        fflush(stdin);
        printf("Masukkan Nama Barang  : ");
        fgets(Brg[i].Nama,20,stdin);
        strtok(Brg[i].Nama, "\n" );
        printf("Masukkan Harga Satuan : ");
        scanf("%d",&Brg[i].HargaSatuan);
        printf("Ada Berapa Jumlah?    : ");
        scanf("%d",&Brg[i].Jumlah);
        harga+=Brg[i].HargaSatuan;
    }
    for (i=0;i<banyak;i++)
    {
        printf("\nBarang Ke-%d\n",i+1);
        printf("Kode         :  %d\n",Brg[i].Kode);
        printf("Nama Barang  : %s\n",Brg[i].Nama);
        printf("Harga Satuan : %d\n",Brg[i].HargaSatuan);
        printf("Jumlah       : %d\n",Brg[i].Jumlah);
    }
    j=i;
    Brg[i].Total=(float)(Brg[i].HargaSatuan*Brg[i].Jumlah);
    rata=(float)Brg[i].Total/j;
    printf("\nTotal Harga Semua Barang : %.2f\n",Brg[i].Total);
    printf("Rata-Rata Harga Barang     : %.2f\n",rata);

If I run it

Berapa banyak barang? : 2

Barang Ke-1
Masukkan Kode : 20
Masukkan Nama Barang  : Tes A
Masukkan Harga Satuan : 20
Ada Berapa Jumlah?    : 1

Barang Ke-2
Masukkan Kode : 10
Masukkan Nama Barang  : Tes B
Masukkan Harga Satuan : 30
Ada Berapa Jumlah?    : 2

Barang Ke-1
Kode         :  20
Nama Barang  : Tes A
Harga Satuan : 20
Jumlah       : 1

Barang Ke-2
Kode         :  10
Nama Barang  : Tes B
Harga Satuan : 30
Jumlah       : 2

Total Harga Semua Barang : 2093416448.00
Rata-Rata Harga Barang     : 1046708224.00

Process returned 0 (0x0)   execution time : 21.303 s
Press any key to continue.

It give me very wrong output and unexpected. The right output should be like this:

Berapa banyak barang? : 2

Barang Ke-1
Masukkan Kode : 20
Masukkan Nama Barang  : Tes A
Masukkan Harga Satuan : 20
Ada Berapa Jumlah?    : 1

Barang Ke-2
Masukkan Kode : 10
Masukkan Nama Barang  : Tes B
Masukkan Harga Satuan : 30
Ada Berapa Jumlah?    : 2

Barang Ke-1
Kode         :  20
Nama Barang  : Tes A
Harga Satuan : 20
Jumlah       : 1

Barang Ke-2
Kode         :  10
Nama Barang  : Tes B
Harga Satuan : 30
Jumlah       : 2

Total Harga Semua Barang : 70
Rata-Rata Harga Barang     : 23.3

Process returned 0 (0x0)   execution time : 21.303 s
Press any key to continue.

How do I solve this? Thank you.

2
  • 3
    Passing an input-only stream (like stdin) to fflush is explicitly mentioned in the C specification as leading to undefined behavior. Any literature, tutorial or class that teaches it should be looked at with great suspicion. Commented Mar 9, 2022 at 17:04
  • 2
    scanf("%d") will produce undefined behavior if the input stream contains a value that cannot be represented in an int (eg INT_MAX + 1) Commented Mar 9, 2022 at 17:15

2 Answers 2

2

When you do

Brg[i].Total=(float)(Brg[i].HargaSatuan*Brg[i].Jumlah);
rata=(float)Brg[i].Total/j;

you are outside the loop, which means that i will be an index out of range of your initialized data.

For your input with two entries in the array, the valid indexes will be 0 and 1. The value of i after the loop will be 2.

You need a separate total variable that you modify inside the loop.

1

You have an error in your loop:

for (i=0;i<banyak;i++)
{
    printf("\nBarang Ke-%d\n",i+1);
    printf("Kode         :  %d\n",Brg[i].Kode);
    printf("Nama Barang  : %s\n",Brg[i].Nama);
    printf("Harga Satuan : %d\n",Brg[i].HargaSatuan);
    printf("Jumlah       : %d\n",Brg[i].Jumlah);
}
j=i;
Brg[i].Total=(float)(Brg[i].HargaSatuan*Brg[i].Jumlah);
rata=(float)Brg[i].Total/j;
printf("\nTotal Harga Semua Barang : %.2f\n",Brg[i].Total);

When the loop is done, i = banyak, but values for Brg[i] are only defined for i up to banyak - 1. I think what you meant was:

float total = 0;
for (i = 0; i < banyak; i++) {
    printf("\nBarang Ke-%d\n",i+1);
    printf("Kode         : %d\n",Brg[i].Kode);
    printf("Nama Barang  : %s\n",Brg[i].Nama);
    printf("Harga Satuan : %d\n",Brg[i].HargaSatuan);
    printf("Jumlah       : %d\n",Brg[i].Jumlah);
    Brg[i].Total = (float)(Brg[i].HargaSatuan * Brg[i].Jumlah);
    total += Brg[i].total;
}
rata = total / banyak;
printf("\nTotal Harga Semua Barang : %.2f\n",total);
2
  • You still make the same mistake as the OP, and use i after the loop. And you don't accumulate the total, but only modify the Total of the current index. And you use the uninitialized value of the Total member. Commented Mar 9, 2022 at 17:56
  • You're right, @Some_programmer_dude... fixed now Commented Mar 11, 2022 at 17:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.