Help, how to enter a list from a file? I did the output of the data to the file, but I don’t know how to read the information about editions from the file. Very urgent need. Thanks in advance for any help.

Task: The records contain the names of the publication, newspaper or magazine, the price of the copy. Add new entries so that the names are ordered alphabetically.

#include <stdio.h> #include <conio.h> #include <malloc.h> #include<iomanip> #include<iostream> #include <string> #include <fstream> using namespace std; struct spis //opisivaem structuru { char name [20]; int type; int cena; struct spis *prev; //ssilka na pred.structuru struct spis *next; //ssilka na sled.structuru }; void create(void); //opisanie funkciy void list(spis *); void add(void); void sortt(void); void addkorr(void); struct spis *head, *tail; int main () { char c; //peremennaya vibora punkta menu while(1) { system("CLS"); puts("1 - Sozdat' spisok"); puts("2 - Posmotret' spisok"); puts("3 - Dobavit' novuyu structuru v konec spiska"); puts("4 - Korrekciya spiska s dobavleniem novoy structury"); puts("5 - Vihod"); c=_getch(); switch(c) { case '1': create(); break; case '2': list(head); break; case '3': add(); break; case '4': addkorr(); break; case '5': return 0; default: puts("Oshibka vvoda!"); } } free(head); //Osvobojdenie pamyati } void create(void) //Funkciya sozdaniya spiska { system("CLS"); //Ochistka ekrana spis *p, *pred; //opisanie ukazateley na structuru pred=NULL; do //cikl sozdaniya spiska { p=(spis *)malloc(sizeof(spis)); //videlenie pamyati pod spisok cout<<"Nazvanie izdaniya: "; cin>>p->name; cout<<"Vvedite 0, esli eto jurnal i 1, esli gazeta: "; cin>>p->type; cout<<"Cena izdaniya: "; cin>>p->cena; p->prev=pred; //ustanovka svyazey if(pred!=NULL) pred->next=p; else head=p; pred=p; cout<<"Dlya vihoda najmite <esc>"; cout<<endl<<endl; } while(getch()!=27); tail=p; //ssilka na konec spiska tail->next=NULL; } void list(spis *p) //prosmotr spiska { ofstream output; output.open("D:\\laba16.txt", ios::out); system("CLS"); cout<<" ----------------------------------------------------------\n"; cout<<" є Nazvanie izdaniya є Jurnal=0, Gazeta=1 є Cena izdaniya є\n"; cout<<" ----------------------------------------------------------\n"; output<<" ----------------------------------------------------------\n"; output<<" є Nazvanie izdaniya є Jurnal=0, Gazeta=1 є Cena izdaniya є\n"; output<<" ----------------------------------------------------------\n"; p=head; while(p!=NULL) { cout<<"\n є"<<setw(18)<<p->name<<"є"<<setw(20)<<p->type<<"є"<<setw(16)<<p->cena<<"є"; output<<"\n є"<<setw(18)<<p->name<<"є"<<setw(20)<<p->type<<"є"<<setw(16)<<p->cena<<"є"; p=p->next; } cout<<"\n\nNajmite lubuyu klavishu dlya vihoda v glavnoe menu..."; output.close(); getch(); } void add(void) //dobavlenie novogo spiska v konec structuri { spis *p, *pn; system("CLS"); pn=(spis *)malloc(sizeof(spis)); cout<<"Nazvanie izdaniya: "; cin>>pn->name; cout<<"Vvedite 0, esli eto jurnal i 1, esli gazeta: "; cin>>pn->type; cout<<"Cena izdaniya: "; cin>>pn->cena; p=tail; //perehod v konec spiska pn->prev=tail; pn->next=NULL; p->next=pn; tail=pn; //noviy konec spiska } void sortt(void) //sortirovka spiska { spis *p, *pn; int j,x,y; char d[20]; do { j=1; p=head; //perehod v nachalo spiska pn=p; p=p->next; while(p!=NULL) { if(strcmp(pn->name, p->name)>0) //esli pn>p sortiruem { j=0; strcpy(d, pn->name); x=pn->type; y=pn->cena; strcpy(pn->name, p->name); pn->type=p->type; pn->cena=p->cena; strcpy(p->name, d); p->type=x; p->cena=y; } pn=p; //perehod k sled.elementu spiska p=p->next; } } while(j==0); } void addkorr(void) //vstavka novoy structury, korrekciya po alfavitu { spis *p, *pn; system("CLS"); sortt(); //vizov sortirovki spiska pn=(spis *)malloc(sizeof(spis)); cout<<"Nazvanie izdaniya: "; cin>>pn->name; cout<<"Vvedite 0, esli eto jurnal i 1, esli gazeta: "; cin>>pn->type; cout<<"Cena izdaniya: "; cin>>pn->cena; int j, ei; j=0; ei=0; p=head; do { if(strcmp(pn->name, p->name)<=0 && p->prev==NULL && ei==0) //esli pn<p, eto nachalo spiska i ei=0 { j=1; //vihod iz cikla p->prev=pn; pn->prev=NULL; pn->next=p; head=pn; ei=1; } if(strcmp(pn->name, p->name)<=0 && ei==0) //esli pn<p, eto ne nachalo spiska i ei=0 { j=1; pn->prev=NULL; pn->next=NULL; p->prev->next=pn; pn->prev=p->prev; pn->next=p; p->prev=pn; ei=1; } if(p->next==NULL && ei==0) { j=1; pn->prev=NULL; pn->next=NULL; p->next=pn; pn->prev=p; ei=1; } p=p->next; } while (j==0); cout<<"\n\nNoviy spisok dobavlen. Najmite lubuyu klavishu dlya prodoljeniya"; getch(); } 
  • the meaning of C ++, OOP, is not to write a few dozen lines unrelated to the solution of the problem. - AR Hovsepyan

1 answer 1

I did not even try to understand what you have painted here ... Containers and not quite containers for storing data, in the STL there is any taste and purpose. To store data from the smallest to the largest, use set <> (it will turn out like sorting by inserts)

 #include <iostream> #include <set> using namespace std; struct Edition { // издание string s; // название int p; // цена Edition(const string& name, int price) :s(name), p(price) {} // как выводить?... friend ostream& operator <<(ostream& os, const Edition& ed) { os << ed.s << " price == " <<ed.p << endl; return os; } }; // как определить что меньше?... bool operator <(const Edition& e1, const Edition& e2) { return e1.s < e2.s; } int main() { string in; int n; std::set<Edition> editions; // тут вы должны определиться откуда данные : из файла или из cin... // допустим это из cin while (cin >> in >> n) { editions.emplace(Edition(in, n)); } //а тут вы можете выводить и в файл и в cout copy(editions.begin(), editions.end(), ostream_iterator<Edition>(cout, "\n")); return 0; } 

defining the operator '<' simply set in set , and the set will contain the elements in an ordered form. We will need to determine how these elements are displayed and output them ... And you can also write a comparator instead of the operator < and declare the обьект set<> with this comparator, but these are the details ... It is important that you try to use STL and program using the ООП principle