c++ - List loses the last item in List C -


i've got problem code..... reading method works fine "crashes" when want add new item 1 direction list, , adding item relies on evertything sorted ascending. items in file sorted ascending short cut. works not , draw everythink on paper follow code , have no idea why loses last item in list when i'm trying print list on screen. please, me solve problem. code below mixed c , c++.

#include <iostream> #include <stdio.h> #include <conio.h> #pragma warning(disable:4996) using namespace std;  int howmanyrecords = 0;   // how many record readed file   struct pojazd  {     char model[40];   // name of vechicle     int yearofproduction;  // year of production     float enginecapacity;   // capacity of engine     struct pojazd *nast;  // pointer next element }; struct pojazd* creatingnewitem()  // method creating new object of structure later adding list {     struct pojazd *tmpvechicle=null;      tmpvechicle = (struct pojazd*)malloc(sizeof(struct pojazd));   // model, year , capacity of engine  /////////////////     cout << "zaraz podasz dane nowego pojazdu. przygotuj sie." << endl << endl;     cout << "podaj model samochodu: "; cin >> tmpvechicle->model; cout << endl;     cout << "podaj rok produkcji samochodu: "; cin >> tmpvechicle->yearofproduction; cout << endl;     cout << "podaj pojemnosc silnika samochodu: "; cin >> tmpvechicle->enginecapacity; cout << endl;      tmpvechicle->nast = null;      cout<<"model:"<< tmpvechicle->model<<" rok:" << tmpvechicle->yearofproduction << " pojemosc:" << tmpvechicle->enginecapacity <<endl;      return tmpvechicle;  }   //adding new created item list using pointers list , new item // adding list keeping ascending politic.  void addingnewitemtolist(struct pojazd *headlist, struct pojazd *newitem) {      struct pojazd *pomocnicza = null, *head = null;      head = headlist->nast;     pomocnicza = headlist;      while(true)     {               if( (pomocnicza->yearofproduction < newitem->yearofproduction) && (newitem->yearofproduction < head->yearofproduction))             {                 pomocnicza->nast = newitem;                 newitem->nast = head;                 break;             }             else if((head->nast == null) && (pomocnicza->yearofproduction < newitem->yearofproduction))             {                 pomocnicza->nast = newitem;                 break;             }             else             {                 pomocnicza = head;                 head = head->nast;             }     }  }   // reading file , allocating new object of list  ///////////////////////// struct pojazd* uzupelnianielisty(file *odczytywanie) {       struct pojazd *beggining = null,*nextelement = null;      while (!feof(odczytywanie))     {         if (beggining == null)         {             beggining = nextelement = (struct pojazd*)malloc(sizeof(struct pojazd));         }         else         {             nextelement->nast = (struct pojazd*)malloc(sizeof(struct pojazd));             nextelement = nextelement->nast;         }          fscanf(odczytywanie, "%s %d %f", nextelement->model, &(nextelement->yearofproduction), &(nextelement->enginecapacity));         cout << nextelement->model << endl;         cout << nextelement->yearofproduction << endl;         cout << nextelement->enginecapacity << endl;          cout << "\n";         nextelement->nast = null;          howmanyrecords++;         cout<< howmanyrecords <<endl;      }     fclose(odczytywanie);//closing pliku      system("pause");      return beggining;  }   int main() { // input output file     char wejscie[20], wyjscie[20];      file* odczytywanie;     file *zapisywanie; //head of list     struct pojazd *headlist = null; //new item pointer     struct pojazd *newitem = null;  //additional pointer in printing code @ bottom     struct pojazd *helper = null;       cout << "podaj nazwe pliku odczytu: "; cin >> wejscie;     odczytywanie = fopen(wejscie, "r");         headlist = uzupelnianielisty(odczytywanie);       newitem = creatingnewitem(); // creating new item      addingnewitemtolist(headlist, newitem);      helper = headlist;    /// new list of items   ////     cout << "*************************nowa lista*********************" << endl;     for(int = 0; < howmanyrecords; i++)     {          cout << helper->model << endl;         cout << helper->yearofproduction << endl;         cout << helper->enginecapacity << endl;          helper = helper->nast;      }     cout << "*************************koniec nowa lista*********************" << endl;       _getch();     return 0; } 

here file contents:

// name year capacity  syrena 1977 650 maluch 1999 3800 polonez 2004 1774 

what wrong program....?

you don't increment ilerekordow in dodawaniedolisty, when add new record. (or howmanyrecords in addingnewitemtolist in new code)

here example, how should be:

void addingnewitemtolist(struct pojazd **headlist, struct pojazd *newitem) {      struct pojazd *pomocnicza = null, *head = null;      head = (*headlist)->nast;     pomocnicza = *headlist;      while(true)     {           if(head == null)          {             pomocnicza->nast = newitem;             break;         }else if( (pomocnicza->yearofproduction <= newitem->yearofproduction) && (newitem->yearofproduction < head->yearofproduction))         {             pomocnicza->nast = newitem;             newitem->nast = head;             break;         }else if (pomocnicza->yearofproduction>newitem->yearofproduction){             newitem->nast=pomocnicza;             (*headlist)=newitem;             break;         }         else          {             pomocnicza = head;             head = head->nast;         }     }     howmanyrecords++;  } 

also, because changed declaration of function, should called this:

addingnewitemtolist(&headlist, newitem); 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -