Getline in input file

Hello,
in a cinema booking system using c++
we read a .txt file containing movie names and info using ifstream
the structure of movies is

1
2
3
4
5
6
7
8
9
10
11
struct movie
{
	string title;
	string director;
	string actor;
	string rating;
	screening_info screening_info[5];
};

//Global variables
movie movies[4];


then we take the input from

1
2
3
	for (int i = 0; i < 4; i++) {
		
		ListFile >> movies[i].title >> movies[i].rating >> movies[i].actor >> movies[i].director;


however when i output this info its in bad alignment because i cant add a space in the .txt file or the compiler will encounter it as a variable

so i tried to use the getline function but i failed :D

any help please?
Last edited on
If I understood your problem correctly, each line in the file has one attribute of the movie. Try using two for loops, first the main loop will count the number of movies. The second, inner, loop will read each line, parse it, and store the disered value.



So lets say we have 4 movies
will the code look like this?

1
2
3
4
5
6
7
8
for(int i =0 ; i<4;i++){

for(int i=0;i<4;i++){
getline(movies[i].title ,movies[i].rating , movies[i].actor , movies[i].director)
//Don't know how to use that so i dont think its correct ..
}

}
Last edited on
Well the problem is getline function takes 2 parameters the object and the variable and here its an array...
main problem is for example Movie1 name is The pursuit of happiness i must add it in notepad as ThePursuitOfHappiness i cant add spaces between the words or each word will be added in the array separately
Last edited on
will the code look like this?

A nested for loop won't work, because your movie attributes are not an array.
1
2
3
4
5
6
7
  const int NUM_MOVIES = 4;
  for (int i =0 ; i<NUM_MOVIES; i++)
  {  getline (listfile, movies[i].title);
      getline (listfile, movies[i].rating);
      getline (listfile, movies[i].actor);
      getline (listfile, movies[i].director);
  }

main problem is for example Movie1 name is The pursuit of happiness i must add it in notepad as ThePursuitOfHappiness i cant add spaces between the words or each word will be added in the array separately
i cant add spaces between the words or each word will be added in the array separately

No. That's the reason for using getline. getline will read everything up to the \n preserving spaces.
Topic archived. No new replies allowed.