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.
CodePudding user response:
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.
CodePudding user response:
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;
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:
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);
}
rata = Brg[i].Total / banyak;