krzaczki w listach c++

0

Witam męczę się od 4 godzin z programem mam zrobić projekt na studia system oceniający filmy filmy mają być wczytywane z pliku txt i tytuł reżyser max 3 aktorów 10 ostatnich ocen itp

próbuje wczytać plik do listy ale jak uruchamia się while(!plik.eof()) to pętla leci w nieskończoność a jak ręcznie skopiuje kod 3 razy tle ile na razie mam informacji to pierwszy się dobrze wypiszę a reszta krzaczki oto kod pierwsza partia informacji się wpisuję a jak skopiuje zawartość while 3 razy lub 2 a while za komentuje to wychodzą krzaczki Prosił bym o pomoc .

oto txt

Skazani na Shawshank
Frank Darabont
1994
USA
Tim Robbins
Morgan Freeman
brak
10 2 5 6 7 4 8 4 3 6
3.5
Ojciec chrzestny
Francis Ford Coppola
1972
USA
Marlon Brando
Al Pacino
James Caan
6 2 6 7 4 6 8 3 7 3
6.5
Lot nad kukułczym gniazdem
Miloš Forman
1975
USA
Jack Nicholson
Louise Fletcher
William Redfield
4 6 8 4 6 8 5 3 5 7
7.4

 #include<iostream>
#include<string>
#include<fstream>
#include<sstream>




using namespace std;


 struct el_list{
	 string tytul;
	 string rezyser;
	 string rok;
	 string wytwornia;
	 string ak[3];
	 int oceny[10];
	 float srednia;

	 


	 el_list *next;
 };





void wczyt(el_list *head, el_list *nowy, el_list *temp){
	


	ifstream plik("baza.txt");
	if (plik){
	//while (!plik.eof()){
				
			

			nowy = new el_list;
			
			getline(plik, nowy->tytul);
			getline(plik, nowy->rezyser);
			getline(plik, nowy->rok);
			getline(plik, nowy->wytwornia);
			getline(plik, nowy->ak[0]);
			getline(plik, nowy->ak[1]);
			getline(plik, nowy->ak[2]);
			
			plik >> nowy->oceny[0];
			plik >> nowy->oceny[1];
			plik >> nowy->oceny[2];
			plik >> nowy->oceny[3];
			plik >> nowy->oceny[4];
			plik >> nowy->oceny[5];
			plik >> nowy->oceny[6];
			plik >> nowy->oceny[7];
			plik >> nowy->oceny[8];
			plik >> nowy->oceny[9];
			plik >> nowy->srednia;
			
			temp->next = nowy;
			temp = temp->next;
			


			
		

		
	//}
			nowy->next = NULL;
			plik.close();

	}
	
	else{
		cout << "nie udało się otworzyć pliku" << endl;
		nowy->next = NULL;


	}
	


}
void wyswietl(el_list * head, el_list *nowy, el_list *temp){
	
	temp = head->next;

	while (temp != NULL)
	{
		
		cout << temp->tytul << endl;
		cout << temp->rezyser << endl;
		cout << temp->rok << endl;
		cout << temp->wytwornia << endl;
		cout << temp->ak[0] << endl;
		cout << temp->ak[1] << endl;
		cout << temp->ak[2] << endl;
		cout << temp->oceny[0] << endl;
		cout << temp->oceny[1] << endl;
		cout << temp->oceny[2] << endl;
		cout << temp->oceny[3] << endl;
		cout << temp->oceny[4] << endl;
		cout << temp->oceny[5] << endl;
		cout << temp->oceny[6] << endl;
		cout << temp->oceny[7] << endl;
		cout << temp->oceny[8] << endl;
		cout << temp->oceny[9] << endl;
		cout << temp->srednia << endl;

		temp = temp->next;
	}


	
	
}

 int main(){
	 el_list *nowy;
	 el_list *temp;
	 el_list *head;

	 nowy = new el_list;
	
	 temp = nowy;
	 head = nowy;
	 
	 

	wczyt(head, nowy, temp);

	wyswietl(head, nowy, temp);
	


	 
	


	 system("pause");
	 return 0;
 }
2
  1. plik.eof() stanie się prawdą dopiero po wczytaniu które się nie powiedzie, więc:
while(true) { file>>value; if(!file) break; proceed(value); }
  1. Podziel funkcjonalność:
struct Film { string tytul,rezyser,rok,wytwornia,ak[3]; int oceny[10]; }; // średnią liczymy na zawołanie
struct Node { Film film; Node *next; };
struct List { Node *head,*tail; };
List L={0,0};
wczyt(&L);
wyswietl(&L);
0

mógł bym prosić o małe wyjaśnienie bo słabo ogarniam listy szukałem w necie poradników itp ale szkół jest sporo każdy inaczej + większość już obiektowo a program ma być strukturalny

1

temp = temp->next; to nie zmienia wartości zmiennej, bo nie przekazujesz jej przez referencję.

0

gdzie mam przekazać ją jako referencję do funkcji wczyt() ?

0

Zmieniłem kod na taki dodaje teraz 2 pierwsze dobrze ale znowu w while leci w nieskończoność a jak dam for żeby się wykonało 3 razy bo informacji w pliku jest na 3 razy to pierwszy ok wpisuje drugi też a 3 powtarza to co w drugim czyli wychodzi mi wynik + w następnym już oceny są z pierwszego wszędzie i brak jednej linijki

Skazani na Shawshank
Frank Darabont
1994
USA
Tim Robbins
Morgan Freeman
brak
10
2
5
6
7
4
8
4
3
6

Ojciec chrzestny
Francis Ford Coppola
1972
USA
Marlon Brando
Al Pacino
10
2
5
6
7
4
8
4
3
6

Ojciec chrzestny
Francis Ford Coppola
1972
USA
Marlon Brando
Al Pacino
10
2
5
6
7
4
8
4
3
6
Press any key to continue . . .

czemu powiela 2 zamiast wpisywać 3 zestaw ?

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<conio.h>




using namespace std;


 struct el_list{

	 
	 string tytul;
	 string rezyser;
	 string rok;
	 string wytwornia;
	 string ak[3];
	 int oceny[10];
	 

	 


	 el_list *next;
 };


 bool pusta(el_list *head){
	 if (head == NULL){
		 return true;
	 }
	 else
		 return false;
 }


 void wczytpierw(el_list *&head, el_list *&last,string tytul,string rezyser,string rok,string wytwornia,string ak[],int oceny[])
 {
	 	el_list *tmp = new el_list;

		tmp->tytul = tytul;
		tmp->rezyser = rezyser;
		tmp->rok = rok;
		tmp->wytwornia = wytwornia;
		tmp->ak[0] = ak[0];
		tmp->ak[1] = ak[1];
		tmp->ak[2] = ak[2];

		tmp->oceny[0] = oceny[0];
		tmp->oceny[1] = oceny[1];
		tmp->oceny[2] = oceny[2];
		tmp->oceny[3] = oceny[3];
		tmp->oceny[4] = oceny[4];
		tmp->oceny[5] = oceny[5];
		tmp->oceny[6] = oceny[6];
		tmp->oceny[7] = oceny[7];
		tmp->oceny[8] = oceny[8];
		tmp->oceny[9] = oceny[9];
		
		tmp->next = NULL;
		head = tmp;
		last = tmp;

 }



 void wczyt(el_list *&head, el_list *&last, string tytul, string rezyser, string rok, string wytwornia, string ak[], int oceny[])
 {
	

	if (pusta(head)){
		wczytpierw(head, last, tytul, rezyser, rok, wytwornia, ak, oceny);
	}

	
		
			
			else{

				el_list *tmp = new el_list;
				
				tmp->tytul = tytul ;
				tmp->rezyser=rezyser;
				tmp->rok=rok;
				tmp->wytwornia=wytwornia;
				tmp->ak[0] = ak[0];
				tmp->ak[1] = ak[1];
				tmp->ak[2] = ak[2];

				tmp->oceny[0] = oceny[0];
				tmp->oceny[1] = oceny[1];
				tmp->oceny[2] = oceny[2];
				tmp->oceny[3] = oceny[3];
				tmp->oceny[4] = oceny[4];
				tmp->oceny[5] = oceny[5];
				tmp->oceny[6] = oceny[6];
				tmp->oceny[7] = oceny[7];
				tmp->oceny[8] = oceny[8];
				tmp->oceny[9] = oceny[9];
				
				tmp->next = NULL;
				last->next = tmp;
				last = tmp;


			}

			
		}
	
	
	
	



void wyswietl(el_list *& temp){
	
	


	while (temp != NULL)
	{
		
		cout << temp->tytul << endl;
		cout << temp->rezyser << endl;
		cout << temp->rok << endl;
		cout << temp->wytwornia << endl;
		cout << temp->ak[0] << endl;
		cout << temp->ak[1] << endl;
		cout << temp->ak[2] << endl;
		cout << temp->oceny[0] << endl;
		cout << temp->oceny[1] << endl;
		cout << temp->oceny[2] << endl;
		cout << temp->oceny[3] << endl;
		cout << temp->oceny[4] << endl;
		cout << temp->oceny[5] << endl;
		cout << temp->oceny[6] << endl;
		cout << temp->oceny[7] << endl;
		cout << temp->oceny[8] << endl;
		cout << temp->oceny[9] << endl;
		

		temp = temp->next;
	}


	
	
}

int main(){


	el_list *head = NULL;
	el_list *last = NULL;
	string tytul;
	string rezyser;
	string rok;
	string wytwornia;
	string ak[3];
	int oceny[10];


	ifstream plik("baza.txt");
	if (plik){
		for (int i = 0; i < 3;i++){
	
		getline(plik, tytul);
		getline(plik, rezyser);
		getline(plik, rok);
		getline(plik, wytwornia);
		getline(plik, ak[0]);
		getline(plik, ak[1]);
		getline(plik, ak[2]);
		plik >> oceny[0];
		plik >> oceny[1];
		plik >> oceny[2];
		plik >> oceny[3];
		plik >> oceny[4];
		plik >> oceny[5];
		plik >> oceny[6];
		plik >> oceny[7];
		plik >> oceny[8];
		plik >> oceny[9];
		




		wczyt(head, last, tytul, rezyser, rok, wytwornia, ak, oceny);

		
		}
}
else{
	cout << "nie udalo sie otworzyc pliku" << endl;
}

	 wyswietl(head);
	


	 system("pause");
	 return 0;
 }
 
0

zauważyłem że jak dam przy wczytywaniu po ostatniej ocenie[9]
plik >> s;
i w pliku zrobię enter po każdym zestawie znaków
to wszystko się wypisuje już dobrze oprócz tułów bo mają pourywane kawałki nazw

0

wyczytałem że muszę użyć cin.ignore('\n'); bo zostaje mi znak końca linni ale nie wiem gdzie to wstawić bo jak stawiam na początku to nic się nie wyswiatla

0

rozwiązałem problem użyłem
plik.ignore();

i działa

0

Wstawiam działający krótszy kod bez zbędnych zmiennych może ktoś w przyszłości będzie miał podobny przypadek

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<conio.h>




using namespace std;


 struct el_list{

	 
	 string tytul;
	 string rezyser;
	 string rok;
	 string wytwornia;
	 string ak[3];
	 int oceny[10];
	 

	 


	 el_list *next;
 };


 bool pusta(el_list *head){
	 if (head == NULL){
		 return true;
	 }
	 else
		 return false;
 }


 void wczyt(el_list *&head, el_list *&last)
 {
	 ifstream plik("baza.txt");
	 if (plik){
		 for (int i = 0; i < 3; i++){

			 if (pusta(head)){



				 el_list *tmp = new el_list;

				 getline(plik, tmp->tytul);
				 getline(plik, tmp->rezyser);
				 getline(plik, tmp->rok);
				 getline(plik, tmp->wytwornia);
				 getline(plik, tmp->ak[0]);
				 getline(plik, tmp->ak[1]);
				 getline(plik, tmp->ak[2]);

				 plik >> tmp->oceny[0];
				 plik >> tmp->oceny[1];
				 plik >> tmp->oceny[2];
				 plik >> tmp->oceny[3];
				 plik >> tmp->oceny[4];
				 plik >> tmp->oceny[5];
				 plik >> tmp->oceny[6];
				 plik >> tmp->oceny[7];
				 plik >> tmp->oceny[8];
				 plik >> tmp->oceny[9];
				 plik.ignore();

				 

				 tmp->next = NULL;
				 head = tmp;
				 last = tmp;
			 }
			 else{

				 el_list *tmp = new el_list;

				 getline(plik, tmp->tytul);
				 getline(plik, tmp->rezyser);
				 getline(plik, tmp->rok);
				 getline(plik, tmp->wytwornia);
				 getline(plik, tmp->ak[0]);
				 getline(plik, tmp->ak[1]);
				 getline(plik, tmp->ak[2]);

				 plik >> tmp->oceny[0];
				 plik >> tmp->oceny[1];
				 plik >> tmp->oceny[2];
				 plik >> tmp->oceny[3];
				 plik >> tmp->oceny[4];
				 plik >> tmp->oceny[5];
				 plik >> tmp->oceny[6];
				 plik >> tmp->oceny[7];
				 plik >> tmp->oceny[8];
				 plik >> tmp->oceny[9];
				 plik.ignore();


				 tmp->next = NULL;
				 last->next = tmp;
				 last = tmp;


			 }
		 }
	 }
	 else{
		 cout << "nie udalo sie otworzyc pliku" << endl;
	 }

 }



 
	
	
	
	



void wyswietl(el_list * temp){
	
	


	while (temp != NULL)
	{
		
		cout << temp->tytul <<" ";
		cout << temp->rezyser << " ";
		cout << temp->rok << " ";
		cout << temp->wytwornia << " ";
		cout << temp->ak[0] << " ";
		cout << temp->ak[1] << " ";
		cout << temp->ak[2] << " ";
		cout << temp->oceny[0] << " ";
		cout << temp->oceny[1] << " ";
		cout << temp->oceny[2] << " ";
		cout << temp->oceny[3] << " ";
		cout << temp->oceny[4] << " ";
		cout << temp->oceny[5] << " ";
		cout << temp->oceny[6] << " ";
		cout << temp->oceny[7] << " ";
		cout << temp->oceny[8] << " ";
		cout << temp->oceny[9] << " ";
		cout << endl;

		temp = temp->next;
	}


	
	
}

int main(){


	el_list *head = NULL;
	el_list *last = NULL;
	


		wczyt(head, last);

		
	


	 
wyswietl(head);


	 system("pause");
	 return 0;
 }
 

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