[C] - Listy

0

Witam, posiadam taki kod w swoim programie:

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


typedef struct lista 
{    struct lista *next; 
   int rok; 
   int mies; 
   char typesign; 
   float wart; 
} listaout, listain; 

listaout *firstout=NULL; 
listain *firstin=NULL; 
choose=0; 


void add_element_out (listaout *list,char letter, float valueof) 
{ 
      FILE *f; 
   time_t curtime; 
   struct tm *loctime; 
   struct lista *ptr=NULL,*newone=NULL; 
    
   ptr=list; 
   while (ptr-> next!=NULL) 
      { 
         ptr=ptr->next; 
      } 
   curtime=time(NULL); 
   loctime=localtime(&curtime); 

   newone=malloc(sizeof(listaout)); 
   newone->rok=(loctime->tm_year)+1; 
   newone->mies=(loctime->tm_mon)+1; 
   newone->typesign=letter; 
   newone->wart=valueof; 
f=fopen("out.txt","a+"); 
fprintf(f,"%d %d %c %f \n",newone->rok, newone->mies, newone->typesign, newone->wart); 
fclose(f); 
ptr-> next=newone; 
} 

void add_element_in (listain *list,char letter, float valueof) 
{ 
   FILE *f; 
   time_t curtime; 
   struct tm *loctime; 
   struct lista *ptr=NULL,*newone=NULL; 
   ptr=list; 
   while (ptr-> next!=NULL) 
      { 
         ptr=ptr->next; 
      } 
   curtime=time(NULL); 
   loctime=localtime(&curtime); 

   newone=malloc(sizeof(listaout)); 
   newone->rok=(loctime->tm_year)+1; 
   newone->mies=(loctime->tm_mon)+1; 
   newone->typesign=letter; 
   newone->wart=valueof; 
f=fopen("in.txt","a+"); 
fprintf(f,"%d %d %c %f \n",newone->rok, newone->mies, newone->typesign, newone->wart); 
fclose(f); 
ptr-> next=newone; 
} 

void incomesmenu () 
{listain *ptr; 
float valu=0; 
char name; 
ptr=(listain*)malloc(sizeof(listain)); 
firstin=malloc(sizeof(listain)); 
firstin->next=NULL; 


 system("clear"); 
   printf("You are in Incomes menu. Please choose what do you want to do. When you choose the number, type how much you earn."); 
   printf("Incomes Menu\n"); 
   printf("(1) - Job\n"); 
   printf("(2) - Gifts\n"); 
   printf("(3) - Casual Work\n"); 
   printf("(4) - Sum\n"); 
   printf("(5) - Back\n"); 


      scanf("%d", &choose); 
      switch (choose) 
      { 
      case 1: 
         name="j"; 
         scanf("%f",&valu); 
         add_element_in(firstin,name,valu); 
         incomesmenu(); 
      case 2: 
         name="g"; 
         scanf("%f",&valu); 
         add_element_in(firstin,name,valu); 
         incomesmenu(); 
      case 3: 
         name="c"; 
         scanf("%f",&valu); 
         add_element_in(firstin,name,valu); 
         incomesmenu(); 
      case 4: 
      //   (float) sumoflistin(firstin); 
         break; 
          
      case 5: 
         menu(); 

      default: 
         menu(); 
      } 
  
} 

void outcomesmenu() 
{ 
listaout *ptr; 
float valu=0; 
char name; 
ptr=(listaout*)malloc(sizeof(listaout)); 
firstout=malloc(sizeof(listaout)); 
firstout->next=NULL; 

 system("clear"); 
   printf("You are in Outcomes menu. Please choose what do you want to do.When you choose the number, type how much you spend."); 
   printf("Outcomes Menu\n"); 
   printf("(1) - Food\n"); 
   printf("(2) - Entertainment\n"); 
   printf("(3) - House development\n"); 
   printf("(4) - Clothes\n"); 
   printf("(5) - Sum\n"); 
   printf("(6) - Back\n"); 

      scanf("%d", &choose); 
      switch (choose) 
      { 
      case 1: 
         name="f"; 
         scanf("%f",&valu); 
         add_element_out(firstout,name,valu); 
         outcomesmenu(); 
      case 2: 
         name="e"; 
         scanf("%f",&valu); 
         add_element_out(firstout,name,valu); 
         outcomesmenu(); 
      case 3: 
         name="h"; 
         scanf("%f",&valu); 
         add_element_out(firstout,name,valu); 
         outcomesmenu(); 
      case 4: 
         name="c"; 
         scanf("%f",&valu); 
         add_element_in(firstout,name,valu); 
         outcomesmenu(); 
         break; 
      case 5: 
      //   (float) sumoflistout(firstout); 
         break; 
      case 6: 
      menu(); 

      default: 
         menu(); 
      } 
  


} 


summenu() 
{ 
    
 FILE *f; 
  system("clear"); 
printf("You are in sum menu. Please choose what do you want to do.\n"); 
    printf("Sum menu"); 
    printf("(1) Calculate all incomes\n"); 
    printf("(2) Calculate all outcomes\n"); 
    printf("(3) Make a report for in monthly\n"); 
    printf("(4) Make a report for incomes yearly\n"); 
    printf("(5) Make a report for outcomes monthly\n"); 
    printf("(6) Make a report for outcomes yearly\n"); 
    printf("(7) Calculate bilance"); 
   printf("(8) Back"); 
    
scanf("%d", &choose); 
switch(choose) 
{   case 1: 
   f="in.txt"; 
      sum(f); 
      summenu(); 
    case 2: 
    f="out.txt"; 
    sum(f); 
   summenu(); 
   case 8: 
      menu(); 
   default: 
      summenu(); 
} 

}    
    
menu() 
{ 
    system("clear"); 
printf("Welcome in the Home Budget Organiser. Choose what do you want to do.\n"); 
   printf("Main Menu\n"); 
   printf("(1) - Incomes\n"); 
   printf("(2) - Outcomes\n"); 
   printf("(3) - How much money is left? \n"); 
   printf("(4) - Exit\n"); 

      scanf("%d", &choose); 
      switch (choose) 
      { 
      case 1: 
         incomesmenu(); 
      case 2: 
         outcomesmenu(); 
       
      case 3: 
          summenu(); 
       
      default: 
         exit(1); 
         break; 
      } 
} 


 sum(FILE *fp) 
{ FILE *f; 
int rok,mies; 
char typesign,znak; 
float wart,suma; 
f=fp; 

f=fopen("in.txt","r"); 
 while (f=!EOF){ 
 fscanf(f, "%d", &rok); 
 fscanf(f, "%d", &mies); 
 fscanf(f, "%c", typesign); 
 fscanf(f, "%f", &wart); 
 suma=suma+wart; 
 return(suma); 
 } 
 fclose(f); 
} 



} 


void main () { 
int n; 

menu(); 
    

   }

Jest to program "Budżet domowy". Po otwarciu Menu, włącza się incomesmenu bądź outcomesmenu. Za każdym wybraniem punktu w switch ponownie włącza się to samu menu. Mój problem polega na tym że nie tworzy mi listy, tylko nadpisuje pierwszy element. Spekuluję, że jest to wina tego, że na początku tych funkcji są deklaracje wskaźników. Bardzo proszę o pomoc w rozwiązaniu tego, nie mam pojęcia jak to zrobić.

0
newone=malloc(sizeof(listaout)); 

W funkcji add_element_in. Takie są skutki kopiowania, zamiast pisania elastycznego kodu. :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1