what's wrong with this program, i will sort descending this program

#include <stdio.h>
#include <stdlib.h>

FILE *fp;
    typedef struct buku{
    int no;
    char judul[30];
    char pengarang[30];
    int jumlah;
    }dt_buku;

dt_buku sementara[50],bantu;
int i,j;

void tukar(void);
void viewData(void);
void sortData(void);
int main()
{
    viewData();
    printf("\n\n");
    sortData();
    return 0;
}

void tukar()
{
bantu = sementara[i];
sementara[i] = sementara[j];
sementara[j] = bantu;
}

void viewData()
{
    if((fp = fopen("databuku.txt","r")) == NULL)
    {
        printf("File gagal diciptakan");
        exit(1);
    }

    printf("%2s. %30s %30s %s\n\n","No","Judul","Pengarang","Jumlah");
    for(i=0;i<10;i++){
    fread(&sementara[i], sizeof(sementara[i]),1,fp);
    printf("%2d %30s %30s %2d\n",i+1, sementara[i].judul, sementara[i].pengarang, sementara[i].jumlah);
    }
     fclose(fp);
}
void sortData()
{
     for(i=0;i<10;i++){
            for(j=0;j<10;j++){
                if(sementara[i].jumlah < sementara[j].jumlah){
                    tukar();}
                }
        }
        viewData();
}
Member Avatar for v3ga

sementara is an object of struct buku so it should be declared as buku sementara[50]. Same with bantu.
sortData() seems to be wrong.
Hint: You seems like using bubble sort but should j go from 0 to 10?
Shouldn't the file be opened in binary mode?(not sure abt this one)

sementara is an object of struct buku so it should be declared as buku sementara[50]. Same with bantu.
sortData() seems to be wrong.
Hint: You seems like using bubble sort but should j go from 0 to 10?
Shouldn't the file be opened in binary mode?(not sure abt this one)

yeah... thank you for helping...

Member Avatar for v3ga

yeah... thank you for helping...

Pls mark the thread solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.