When the program comes to the yazici function, I can't print the elements of urun correctly to the file.
I can print satin_alim_kg,toplam_ucret and barkodsorgu. I tried making structures globally, but I couldn't.
/*
** So overwhelming. Make your code attractive and much more readable. No one will get his hands in it. Use proper spacing in between statements in your program.
** Missed closing brace at the end after return 0; statement.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
// Structure templates
struct urun_bilgileri{
int barkod;
char urun_adi[50];
char kategori[50];
float kilogram_fiyati;
};
struct fatura_bilgileri{
char* satin_alim_tarihi;
float alinan_kg_miktari;
float odenen_ucret;
};
// Global Variables
struct fatura_bilgileri *fatura;
struct urun_bilgileri *urun;
int i, barkodsorgu, faturasayac = 0;
char alinan_urun, alinan_urun_kategorisi;
float satin_alim_kg, toplam_ucret;
// Function Declarations & Definitions
void tarayici(){
int c;
FILE *fpbarkod = fopen("barkod.txt", "r");
if (fpbarkod)
{
while ((c = getc(fpbarkod)) != EOF)
putchar(c);
rewind(fpbarkod);
fclose(fpbarkod);
}
}
void yazici(){
FILE *fpfatura;
time_t t;
// Why are you allocating memory twice to fatura and urun, you haven't freed it yet in the main()?
time(&t);
(fatura+faturasayac)->satin_alim_tarihi = ctime(&t);
if( (fpfatura = fopen("fatura.txt", "w+") ) == NULL)
{
perror("fatura.txt");
exit(3);
}
fprintf(fpfatura, "Islem tarihi:%sAlinan urun:%s Barkod Numarasi:%d Urunun kategorisi:%s Alinan miktar:%.3fKG Odenen ucret:%.2f\n\n", (fatura+faturasayac)->satin_alim_tarihi, (urun+i)->urun_adi,barkodsorgu, (urun+i)->kategori, satin_alim_kg, toplam_ucret);
printf("%f", toplam_ucret);
fclose(fpfatura);
faturasayac++;
}
// main()
int main()
{
FILE *fpbarkod;
int urun_sayac = 0, ana_menu_secim;
if( ( fatura = (struct fatura_bilgileri *)malloc( sizeof(struct fatura_bilgileri) ) ) == NULL) // Why are you redeclaring struct fatura_bilgileri *fatura
{
printf("Memory Unavailable.\n"); // Check for the errors while allocating memory.
exit(4);
}
if( ( urun = (struct urun_bilgileri*)malloc( sizeof(struct urun_bilgileri) ) ) == NULL) // Why are you redeclaring struct urun_bilgileri *urun
{
printf("Memory Unavailable.\n"); // Check for the errors while allocating memory.
exit(5);
}
printf("MARKET OTOMATIK ODEME SISTEMI\n");
baslangic:
printf("------------------\n");
printf("1-URUN EKLE\n");
printf("2-URUN KATALOGU\n");
printf("3-CIKIS\n");
printf("------------------\n");
printf("Lutfen islem yapmak istediginiz secenegi seciniz:");
scanf("%d", &ana_menu_secim);
switch(ana_menu_secim){
case 1:
printf("\nLutfen urunun adini giriniz:");
scanf("%s", (urun+urun_sayac)->urun_adi);
printf("\nLutfen urunun kategorisini giriniz:");
scanf("%s", (urun+urun_sayac)->kategori);
printf("\nLutfen urunun kilogram fiyatini giriniz(TL):");
scanf("%f", &(urun+urun_sayac)->kilogram_fiyati);
srand(time(NULL));
(urun+urun_sayac)->barkod = rand() % (1000 + 1 - 100) + 100;
sleep(1);
printf("\n\nUrun Adi:%s\nUrun kategorisi:%s\nUrun Kilogram fiyati:%.2f TL\nUrun barkod numarasi:%d\n",(urun+urun_sayac)->urun_adi,(urun+urun_sayac)->kategori,(urun+urun_sayac)->kilogram_fiyati,(urun+urun_sayac)->barkod);
if( (fpbarkod = fopen("barkod.txt","a+") ) == NULL)
{
perror("barkod.txt");
exit(6);
}
fprintf(fpbarkod, "Urun Adi:%s Kategori:%s Fiyati(kg):%.2f TL Barkod No:%d\n", (urun+urun_sayac)->urun_adi, (urun+urun_sayac)->kategori, (urun+urun_sayac)->kilogram_fiyati, (urun+urun_sayac)->barkod);
rewind(fpbarkod);
fclose(fpbarkod);
sleep(1);
printf("\n\nUrun ekleme basarili\n\n");
urun_sayac++;
goto baslangic; // Avoid use of goto as far as possible. It makes your program unreadable, ambiguous and unstructured.
case 2:
printf("-------------------KATALOG-------------------\n");
tarayici();
printf("---------------------------------------------\n");
geribarkodsorgu:
printf("\nLutfen satin almak istediginiz urunun barkod numarasini giriniz:");
scanf("%d" , &barkodsorgu);
sleep(1);
for(i = 0; i <= urun_sayac; i++){
if(barkodsorgu == (urun+i)->barkod){
printf("\n%s kilogram Fiyati:%.2f\n", (urun+i)->urun_adi, (urun+i)->kilogram_fiyati);
goto satinalimkg;
}
else if(i == urun_sayac){
printf("\n\nYANLIS BARKOD NUMARASI\n");
goto geribarkodsorgu;
}
else
continue;
}
satinalimkg:
sleep(1);
printf("\nLutfen satin almak istediginiz miktari giriniz(kg):");
scanf("%f" , &satin_alim_kg);
if(satin_alim_kg <= 0){
printf("\n\nLutfen satin almak istediginiz miktari dogru giriniz\n");
goto satinalimkg;
}
toplam_ucret = (satin_alim_kg) * ((urun+i)->kilogram_fiyati);
printf("Toplam ucret:%.2f", toplam_ucret);
yazici();
break;
case 3:
return 0;
default:
printf("\nLutfen menudeki seceneklerden birini seciniz.\n\n");
sleep(1);
goto baslangic;
break;
}
// If you are done with fatura and urun free them.
free(fatura);
free(urun);
return 0;
}
How can I correctly print the elements to the file? Should I redefine structures in the printer function