Click here to Skip to main content
15,913,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am writing a code that allows users to add a certain movie's name with its director and then display what the user wrote. I am using vectors but the code that I tried only displays the values after I write the first name and director's name. Moreover, after the two initial inputs the strings are not put together. Any help on keep adding values to the vector then displaying? The code below is a constructor Movie().

What I have tried:

Movie() {
		cout << "Enter the film name name: " << endl;
		getline(cin, filmname);
		cout << "Enter the director's name: " << endl;
		getline(cin, director);
		full = filmname + " " + director;
		cout << "The film and director is therefore: " << full << endl;
		while (cin >> full) {
			if (full == "end") {
				break;
			}
			Film.push_back(full);
		}
		cout << "You entered the following: ";
		for (int i = 0; i < Film.size(); i++) {
			cout << Music[i] << endl;
		}
	}
Posted
Updated 7-May-18 21:23pm
v2
Comments
Patrice T 8-May-18 2:18am    
Show an autonomous piece of code, so that we can reproduce the problem.

With cin >> full you are overwriting the current value of full with the new input.
You should store the new input in a separate string and append to full until "end" is typed.
 
Share this answer
 
Comments
KarstenK 8-May-18 3:26am    
Another Q&A which the importance of the usage of the debugger shows :-O
The while statement should be at the beginning of the block, before you get the film details, and a do loop would probably work better:
C++
do
{
    cout << "Enter the film name name: " << endl;
    getline(cin, filmname);
    if (filmname == "end")
        break;
    cout << "Enter the director's name: " << endl;
    getline(cin, director);
    full = filmname + " " + director;
    cout << "The film and director is therefore: " << full << endl;
    Film.push_back(full);
} while (true);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900