read .txt file into vector of objects

Basically this is a movie class assignment. Theres 2 parts, part 1 is read a file of movies (ordered as such: title, director last name, director first name, main star last name, main star first name, studio, year of release, language, rating -- each on a separate line) into a vector of movie objects. Second part is add more functions such as sorting, adding/removing movies etc.

I am stuck on the first part, I know that there will always be 10 movies in the .txt file, so i tried using a for loop to fill the vector in and its not working. Im not sure as to why but my output is always this x10

Movie Name: Bohemian Rhapsody
Director: Bryan Singer
Main Star: Rami Malek
Studio: GK Films
Year of release: 0
Language:
IMDb rating: 0

I'm 90% sure this is where the problem is, but not sure what exactly the problem is,
any help is greatly appreciated!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  void fillVector(vector<Movie>& newMoviesList){
    string title, directorLastName, directorFirstName, starLastName, starFirstName, studio, language;
    int year = 0;
    double rating = 0.0;
    
    for (int i = 0; i < 10; i++){
        getline(movieDataFile, title);
        getline(movieDataFile, directorLastName);
        getline(movieDataFile, directorFirstName);
        getline(movieDataFile, starLastName);
        getline(movieDataFile, starFirstName);
        getline(movieDataFile, studio);
        movieDataFile >> year;
        getline(movieDataFile, language);
        movieDataFile >> rating;
             
        Movie newMovie(title, directorLastName, directorFirstName, starLastName, starFirstName, studio, year, language, rating);
        newMoviesList.push_back(newMovie);
    }
} 


I'll also post all my code just incase.
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "Movie.h"
#include <vector>
#include <fstream>

ifstream movieDataFile;

void openFile();
void fillVector(vector<Movie>&);
void printVector(vector<Movie>&);

int main(){
    openFile();
    
    vector<Movie> moviesList;
    fillVector(moviesList);
    printVector(moviesList);
    
    return 0;
}

void openFile(){
    string fileName;
    
    cout << "Enter the name of your movie database(file name): ";
    cin >> fileName;
    
    //ifstream movieData;
    movieDataFile.open(fileName);
    
    if (!movieDataFile.is_open())
        cout << "Could not find database." << endl;
    else
        cout << "Database sucessfully accessed!" << endl;
}

void fillVector(vector<Movie>& newMoviesList){
    string title, directorLastName, directorFirstName, starLastName, starFirstName, studio, language;
    int year = 0;
    double rating = 0.0;
    
    for (int i = 0; i < 10; i++){
        getline(movieDataFile, title);
        getline(movieDataFile, directorLastName);
        getline(movieDataFile, directorFirstName);
        getline(movieDataFile, starLastName);
        getline(movieDataFile, starFirstName);
        getline(movieDataFile, studio);
        movieDataFile >> year;
        getline(movieDataFile, language);
        movieDataFile >> rating;
             
        Movie newMovie(title, directorLastName, directorFirstName, starLastName, starFirstName, studio, year, language, rating);
        newMoviesList.push_back(newMovie);
    }
}

void printVector(vector<Movie>& newMoviesList){
    
    for(int i = 0; i < 10; i++){
        cout << "Movie Name: " << newMoviesList[i].getTitle() << endl;
        cout << "Director: " << newMoviesList[i].getDirectorFirstName() << " " <<  newMoviesList[i].getDirectorLastName() << endl;
        cout << "Main Star: " << newMoviesList[i].getStarFirstName() << " " <<  newMoviesList[i].getStarLastName() << endl;
        cout << "Studio: " << newMoviesList[i].getStudio() << endl;
        cout << "Year of release: " << newMoviesList[i].getYear() << endl;
        cout << "Language: " << newMoviesList[i].getLanguage() << endl;
        cout << "IMDb rating: " << newMoviesList[i].getRating() << endl;
        cout << endl;
    }
}


movie.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>

using namespace std;

class Movie{
    private:
        string title;
        string directorLastName;
        string directorFirstName;
        string starLastName;
        string starFirstName;
        string studio;
        int year;
        string language;
        double rating;
    public:
        Movie();
        Movie(string mTitle, string mDirectorLastName, string mDirectorFirstName, string mStarLastName, string mStarFirstName, string mStudio, int mYear, string mLanguage, double mRating);
        void setTitle();
        //void setTitle(string mTitle){title = mTitle;};
        string getTitle(){return title;};
        void setDirectorLastName();
        //void setDirectorLastName(string mDirectorLastName){directorLastName = mDirectorLastName;};
        string getDirectorLastName(){return directorLastName;};
        void setDirectorFirstName();
        //void setDirectorFirstName(string mDirectorFirstName){directorFirstName = mDirectorFirstName;};
        string getDirectorFirstName(){return directorFirstName;};
        void setStarLastName();
        //void setStarLastName(string mStarLastName){starLastName = mStarLastName;};
        string getStarLastName(){return starLastName;};
        void setStarFirstName();
        //void setStarFirstName(string mStarFirstName){starFirstName = mStarFirstName;};
        string getStarFirstName(){return starFirstName;};
        void setStudio();
        //void setStudio(string mStudio){studio = mStudio;};
        string getStudio(){return studio;};
        void setYear();
        //void setYear(int mYear){year = mYear;};
        int getYear(){return year;};
        void setLanguage();
        //void setLanguage(string mLanguage){language = mLanguage;};
        string getLanguage(){return language;};
        void setRating();
        //void setRating(float mRating){rating = mRating;};
        double getRating(){return rating;};
};


movie.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "Movie.h"

Movie::Movie(){
    /*setTitle();
    setDirectorLastName();
    setDirectorFirstName();
    setStarLastName();
    setStarFirstName();
    setStudio();
    setYear();
    setLanguage();
    setRating();*/
    
    title = "unknown";
    directorLastName = "unknown";
    directorFirstName = "unknown";
    starLastName = "unknown";
    starFirstName = "unknown";
    studio = "unkown";
    year = 0;
    language = "unknown";
    rating = 0.0;
}
    
Movie::Movie(string mTitle, string mDirectorLastName, string mDirectorFirstName, string mStarLastName, string mStarFirstName, string mStudio, int mYear, string mLanguage, double mRating){
    title = mTitle;
    directorLastName = mDirectorLastName;
    directorFirstName = mDirectorFirstName;
    starLastName = mStarLastName;
    starFirstName = mStarFirstName;
    studio = mStudio;
    year = mYear;
    language = mLanguage;
    rating = mRating;
}

void Movie::setTitle(){
    cout << "Name of movie: ";
    getline(cin, title);
}

void Movie::setDirectorLastName(){
    cout << "Directors last name: ";
    getline(cin, directorLastName);
}

void Movie::setDirectorFirstName(){
    cout << "Directors first name: ";
    getline(cin, directorFirstName);
}

void Movie::setStarLastName(){
    cout << "Stars last name: ";
    getline(cin, starLastName);
}

void Movie::setStarFirstName(){
    cout << "Stars first name: ";
    getline(cin, starFirstName);
}

void Movie::setStudio(){
    cout << "Studio's name: ";
    getline(cin, studio);
}

void Movie::setYear(){
    int y;
    cout << "Year of " << getTitle() << "'s release: ";
    cin >> y;
    year = y; 
}

void Movie::setLanguage(){
    cout << getTitle() << "'s langauge: ";
    getline(cin, language);
}

void Movie::setRating(){
    double r;
    cout << getTitle() << "'s rating: ";
    cin >> r;
    rating = r;
}

Last edited on
Hello idk6199,

As a start consider this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <vector>

#include <fstream>

#include "Movie.h"

//using namespace std;  // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::vector;

ifstream movieDataFile;

bool openFile();
void fillVector(vector<Movie>&);
void printVector(vector<Movie>&);

int main()
{
	if(openFile())
		return 1;

	vector<Movie> moviesList;
	fillVector(moviesList);
	printVector(moviesList);

	return 0;
}

bool openFile()
{
	string fileName;

	cout << "Enter the name of your movie database(file name): ";
	cin >> fileName;

	//ifstream movieData;
	movieDataFile.open(fileName);

	if (!movieDataFile.is_open())
	{
		cout << "Could not find database." << endl;

		return true;
	}
	else
		cout << "Database sucessfully accessed!" << endl;

        return false;
}

Lines 1 - 5 should be in the ".cpp" file not the header file. Putting them in the header file you could be including more than you need in a ".cpp" or not enough.

When it comes to putting "using namespace std;" in the header file you should read this:
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

The way you have the "openFile" function if the file did not open all you would do is print an error message and return to "main" to continue with the program.

When looking over the code for "main" I noticed that you defined the "ifstream" as a global variable. You should try to avoid this even though it seems to be easy. Define the stream in "main" and pass it to the functions that need it.

The prototype and function would be: bool openFile(std::ifstream& movieDataFile) and the function call would be: if(openFile(movieDataFile)).

The "fillVector" function I still need to work on.

It would help if you post the input file that you are using, so that everyone can use the same information.

Andy
I appreciate the reply Andy. I know that using namespace std is bad practice, but thats what my teacher wants us to use, so. I apologize for my horrible errors lmao I'm fairly new to this. Please let me know if you figure something out about the fillVector function. The printVector fucntions works fine (or so I hope there arent any bugs). This is my .txt file (just a bunch of movie data)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Bohemian Rhapsody
Singer
Bryan
Malek
Rami
GK Films
English
2018
8.0
The Dark Knight
Nolan
Christopher
Bale
Christian
Warner Bros
English
2008
9.0
Interstellar
Nolan
Christopher
McConaughey
Matthew
Paramount Pictures
English
2014
8.6
The Karate Kid
Avildsen
John
Macchio
Ralph
Columbia Pictures
English
1984
7.2
Harry Potter and the Philosopher's Stone
Columbus 
Chris 
Radcliffe
Daniel
Warner Bros
English
2001
7.6
Forrest Gump
Zemeckis
Robert
Hanks
Tom
Wendy Finerman Productions
English
1994
8.8
Ready Player One 
Spielberg 
Steven 
Sheridan 
Tye 
Warner Bros
English 
2018
7.5 
The Hangover
Philips 
Todd
Cooper 
Bradley 
Legendary Pictures
English 
2009
7.7
When a Stranger Calls 
West 
Simon 
Belle
Camilla 
Screen Gems 
English 
2006
5.1
Mean Girls
Waters
Mark
Lohan 
Lindsay
Lorne Michaels Productions 
English
2004
7 
1
2
3
        movieDataFile >> year;
        getline(movieDataFile, language);
        movieDataFile >> rating;


Be very careful if you mix >> and getline()

The former LEAVES the '\n' end of line character ... which means that the next getline() call will pick up the remainder of a blank line.

Lots of ways around this, including:
- using getline() for everything; you can put numerical values in variables later by using stringstream
- using ignore() after >>
- using >> ws to dump whitespace
- a single call getline( dummy ) after use of >> to dump the remaining blank line to an unused string variable.
Last edited on
Thanks for the reply @lastchance, i figured something about that was wrong so i decided to switch those lines (and switch them in the data file as well)

1
2
3
getline(movieDataFile, language);
movieDataFile >> year;
movieDataFile >> rating;


The input starts out correct
1
2
3
4
5
6
7
Movie Name: Bohemian Rhapsody
Director: Bryan Singer
Main Star: Rami Malek
Studio: GK Films
Year of release: 2018
Language: English
IMDb rating: 8


but then it continues, and the output becomes this x9
1
2
3
4
5
6
7
Movie Name: 
Director: Nolan The Dark Knight
Main Star: Bale Christopher
Studio: Christian
Year of release: 0
Language: Warner Bros
IMDb rating: 8


If you know, is this due to the error you pointed out? Or is it my for loop and implementation and my way going about it. And if you don't that its ok! Still thanks for the tips!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int i = 0; i < 10; i++){ 
        getline(movieDataFile, title);
        getline(movieDataFile, directorLastName);
        getline(movieDataFile, directorFirstName);
        getline(movieDataFile, starLastName);
        getline(movieDataFile, starFirstName);
        getline(movieDataFile, studio);
        movieDataFile >> year;
        getline(movieDataFile, language);
        movieDataFile >> rating;
             
        Movie newMovie(title, directorLastName, directorFirstName, starLastName, starFirstName, studio, year, language, rating);
        newMoviesList.push_back(newMovie);
    }


Also I thought ignore was only used for cin
1
2
cin >> x;
cin.ignore(); //or something like that? 
Last edited on
is this due to the error you pointed out?

Yes



Also I thought ignore was only used for cin

No, that's not true. You could use something like (untested)
movieDataFile >> year; movieDataFile.ignore( 1000, '\n' );
and similarly for any other input where you use the stream extraction operator >>
http://www.cplusplus.com/reference/istream/istream/ignore/

BTW. This is not the only way of solving the problem.
Last edited on
Yup the other ways are the other ones you listed correct
Yup the other ways are the other ones you listed correct

I'm trying to work out what you meant from a phrase without punctuation!

I suggested 4 ways of overcoming the fact that >> leaves the end-of-line character in the stream. I don't have hard preferences for any of them.
Just to get this straight, the only problem with that part of code is the >> operators? Fix that, and my program will do what I want it to do? (store the data into as objects correctly)

NVM. Thank you so much @lastchance, I used the dummy method :) works perfectly, thanks!!!
Last edited on
Just to get this straight, the only problem with that part of code is the >> operators?

Your code is in too many separate files to make it worth my while to download and compile it.

The mixing of >> and getline will definitely cause problems, so you should fix that. It is likely to be your first problem. What else happens after that I have no idea.
I said nvm lmao :)
Hello idk6199,

I think you closed this to soon. There are some other things that you need to consider.

Just about finished except for the writing, so give me a few minutes to put it together.

Andy
Take your time :) I'm honestly not trying to waste anyones time so I thought since I found something that worked, just close the discussion. Thank you for the time you spent tho :)
Hello idk6199,

Starting in the "main" file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <limits>  // <--- Added.
#include <string>
#include <vector>

#include <fstream>

//using namespace std;  // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::vector;

#include "Movie.h"

bool openFile(std::ifstream& movieDataFile);
void fillVector(std::ifstream& movieDataFile, vector<Movie>& newMoviesList);
void printVector(vector<Movie>&);

int main()
{
	ifstream movieDataFile;  // <--- Moved to here.

	if(openFile(movieDataFile))  // <--- Changed.
		return 1;  // <--- No reason to continue.

	vector<Movie> moviesList;
	fillVector(movieDataFile, moviesList);  // <--- Changed.
	printVector(moviesList);

	return 0;  // <--- Not required, but makes a good break point.
}

Lines 1 - 17 I find this to work out the best. It may not be considered the most proper way, but it works. By placing #include "Movie.h" after everything else when that code is loaded into the ".cpp" file everything above it covers what is in the header file.

For the function "openFile":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
bool openFile(std::ifstream& movieDataFile)
{
	string fileName{ "File 2.txt" };
	std::string availableFiles[]
	{
		"File 1.txt",
		"File 2.txt",
		"File 3.txt"
	};

	std::cout << "\n Available Files:\n";

	for (std::string file : availableFiles)
		std::cout << "  " << file << '\n';

	//cout << "\n Enter the name of your movie database(file name): ";
	//std::getline(std::cin, fileName);
	cout << "\n Enter the name of your movie database(file name): " << fileName; // <--- Used for testing. Comment or remove when finished.

	//ifstream movieData;
	movieDataFile.open(fileName);

	if (!movieDataFile.is_open())  // <--- Changed.
	{
		cout << "Could not find database." << endl;

		return true;  // <--- Added.
	}
	else
		cout << "\n    Database successfully accessed!\n" << endl;

	return false;  // <--- Added.
}


And what shows up on the screen is:

 Available Files:
  File 1.txt
  File 2.txt
  File 3.txt

 Enter the name of your movie database(file name): File 2.txt
    Database sucessfully accessed!


The problem with your original code is that you are asking the user to enter a file name and expecting said user to get it right. This example is just one way you could solve the problem.

I did get a problem with the "fillVector" function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void fillVector(std::ifstream& movieDataFile, vector<Movie>& newMoviesList)
{
	char junk{};  // <---  As lastchance suggested.
	string title, directorLastName, directorFirstName, starLastName, starFirstName, studio, language;
	int year = 0;
	double rating = 0.0;

	while(getline(movieDataFile, title))
	{
		getline(movieDataFile, directorLastName);
		getline(movieDataFile, directorFirstName);
		getline(movieDataFile, starLastName);
		getline(movieDataFile, starFirstName);
		getline(movieDataFile, studio);
		getline(movieDataFile, language);
		movieDataFile >> year;
		movieDataFile >> rating;
		//movieDataFile >> junk;  // <---  As lastchance suggested.
		movieDataFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. The more protable method.

		//Movie newMovie(title, directorLastName, directorFirstName, starLastName, starFirstName, studio, year, language, rating);
		newMoviesList.emplace_back
		(
			title,
			directorLastName,
			directorFirstName,
			starLastName,
			starFirstName,
			studio,
			year,
			language,
			rating
		);
	}
}

The while loop will serve you better than the for loop, but you still change the for loop to work similar to the while loop. This way it will not matter how little or how much is in the input file it will continue until the "eof" bit is set and the stream fails.

The problem I had reading your input file is that your file did not match the way you had your reads set up. In your original code your were reading "studio", "year", "language" and rating, but the input file was set up as "studio", "language" , "year", and rating.

Once I rearranged the input as above it worked.

Note: when it tried to input "year" the input file was at a point to read a string which caused the stream to fail because it was expecting number to be read.

Other than adjusting how the output looks I did not have to change anything for the "printVector" function. Except for the for loop: for (size_t i = 0; i < newMoviesList.size(); i++). The ".size()" function returns a "size_t", an alais for "unsigned int", which means that "i" is the same type as the returned type of the "size()" function.

For the "Movie.h" file I did this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef MOVIE_H
#define MOVIE_H

class Movie
{
	private:
		string title;
		string directorLastName;
		string directorFirstName;
		string starLastName;
		string starFirstName;
		string studio;
		int year;
		string language;
		double rating;
	public:
		Movie();
		Movie(string mTitle, string mDirectorLastName, string mDirectorFirstName, string mStarLastName, string mStarFirstName, string mStudio, int mYear, string mLanguage, double mRating);

		string getTitle() { return title; }
		string getDirectorLastName() { return directorLastName; }
		string getDirectorFirstName() { return directorFirstName; }
		string getStarLastName() { return starLastName; }
		string getStarFirstName() { return starFirstName; }
		string getStudio() { return studio; }
		int getYear() { return year; }
		string getLanguage() { return language; }
		double getRating() { return rating; }

		void setTitle();
		//void setTitle(string mTitle){title = mTitle;}
		void setDirectorFirstName();
		void setDirectorLastName();
		//void setDirectorLastName(string mDirectorLastName){directorLastName = mDirectorLastName;};
		//void setDirectorFirstName(string mDirectorFirstName){directorFirstName = mDirectorFirstName;};
		void setStarLastName();
		//void setStarLastName(string mStarLastName){starLastName = mStarLastName;};
		void setStarFirstName();
		//void setStarFirstName(string mStarFirstName){starFirstName = mStarFirstName;};
		void setStudio();
		//void setStudio(string mStudio){studio = mStudio;};
		void setYear();
		//void setYear(int mYear){year = mYear;};
		void setLanguage();
		//void setLanguage(string mLanguage){language = mLanguage;};
		void setRating();
		//void setRating(float mRating){rating = mRating;};
	};
#endif // !MOVIE_H 

Lines 1,2 and 49 are called an include guard and something that you should get use to using.

The "private" section is OK. The use of the key word "private" is not necessary in this format because everything in a class starts is private unless you tell it differently. Leaving "private;" is OK and does not make any difference.

One thing I would suggest is string m_title;. I have seen the "m_" prefix in many tutorials and it helps to let you know that it is a member variable of a class. Not necessary to change here, but keep in mind for the future.

I rearranges the "public:" section To me it is easier to keep track of things. I also noticed that you originally wrote string getTitle(){return title;};. The (;) at the end is not needed. It is not a declaration it is a full function.

For now the section for the "set" functions are not used at this time, but I do see a use later if the program would take new input.

In the "Movie.cpp" file other than the opening it compiles, so I did not have to change anything there.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::string;

#include "Movie.h"

Movie::Movie()

When finished it loaded 10 records and printed 10 records to the screen.

Andy
Hello idk6199,

I can make a lot of thing work, but it does not mean that it is right.

It is much better to learn what is wrong and how to fix it.

Andy
Hello idk6199,

I did have one question.

When you provided the input file will this be the proper format or would your teacher use a different file when testing the program?

It will make a difference in how you read the file.

Andy
Well, first of all thank you so much for the detailed answer and all the tips, i'll be sure to make use of them. And I completely understand what you mean, theres so many ways to approach different problems. Now about your question, the actual data itself might be different but it'll be in the same format. Everything is on a separate line and the order is the same.
Hello idk6199,

You are welcome.

OK that program should work for you,

Before I forget.

In the read function I used "emplace_back" instead of "push_back". The difference is the parameters of "emplace_back" will construct an object of the class using the overloaded ctor before it adds it to the vector. A way os saving some steps and using what you have.

I noticed that you changed some of the variable names in the functions. This is OK, but you can use the same variable name that you send to the function because in the function it becomes a local variable and there is no conflict with the same name in a different function.

I made some changes to the "printVector" function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void printVector(vector<Movie>& newMoviesList)
{
	constexpr int WIDTH{ 18 };

	std::cout << std::fixed << std::showpoint << std::setprecision(1);

	for (size_t i = 0; i < newMoviesList.size(); i++)
	{
		cout << std::setw(WIDTH) << "Movie Name: " << newMoviesList[i].getTitle() << endl;
		cout << std::setw(WIDTH) << "Director: " << newMoviesList[i].getDirectorFirstName() << " " << newMoviesList[i].getDirectorLastName() << endl;
		cout << std::setw(WIDTH) << "Main Star: " << newMoviesList[i].getStarFirstName() << " " << newMoviesList[i].getStarLastName() << endl;
		cout << std::setw(WIDTH) << "Studio: " << newMoviesList[i].getStudio() << endl;
		cout << std::setw(WIDTH) << "Year of release: " << newMoviesList[i].getYear() << endl;
		cout << std::setw(WIDTH) << "Language: " << newMoviesList[i].getLanguage() << endl;
		cout << std::setw(WIDTH) << "IMDb rating: " << newMoviesList[i].getRating() << endl;
		cout << endl;
	}
}

I realize that you may not know about the "iomanip" header file yet. If so just keep this for future use.

The output it generates is:

      Movie Name: Bohemian Rhapsody
        Director: Bryan Singer
       Main Star: Rami Malek
          Studio: GK Films
 Year of release: 2018
        Language: English
     IMDb rating: 8.0


The "setW()" can be replaced with spaces, but the "setprecision(1)" will allow you to print one decimal place when the decimal part is zero.

Andy
@Handy Andy, thank you again. I realized before your last reply that I should have modified the for loop to be < the size of the vector. Again thanks for all the tips!
Hello idk6199,

You are welcome.

Andy
Topic archived. No new replies allowed.