This function is really confusing me. In some code, getline works as expected but other times, the compiler seemingly skips over it. I've been trying to recognize patterns to pick up on why sometimes it works and sometimes it doesn't. Anyway here's my question...
In line 23, the getline function is seemingly ignored. However, 2 lines later in line 25, I have essentially the same function, just using a different variable. Why does getline work in line 25 and not in line 23? To me those lines of code look identical, but obviously I'm missing something. Could someone please provide an explanation? Thanks!
#include <iostream>
#include <string>
usingnamespace std;
struct MovieData{
string title;
string director;
int year_released;
int run_time;
};
void displayMovieInfo(MovieData movie, int movieNumber);
int main(){
int numMovies = 0;
cout << "How many movies do you have information on? ";
cin >> numMovies;
MovieData movies[numMovies];
for(int i=0; i<numMovies; i++){
cout << "Please enter the movie title: ";
getline(cin, movies[i].title); // why is this getline function ignored?
cout << "Please enter the director of the movie: ";
getline(cin, movies[i].director); // but this getline function is prompted?
cout << "Please enter the year the movie wss released: ";
cin >> movies[i].year_released;
cout << "Please enter the run time of the movie in minutes: ";
cin >> movies[i].run_time;
cout << '\n';
displayMovieInfo(movies[i], i+1);
}
return 0;
}
void displayMovieInfo(MovieData movie, int movieNumber){
cout << "Movie # " << movieNumber << endl;
cout << "Title: " << movie.title << endl;
cout << "Director: " << movie.director << endl;
cout << "Year Released: " << movie.year_released << endl;
cout << "Run Time: " << movie.run_time << " minutes" << endl;
cout << '\n';
}