Problem with getline

My code seems alright to me. Then when I run it, it doesnt let me get input for the title for movie 2. It skips straight to director.


#include <iostream>
#include <string>
using namespace std;

void display(string,string,int,int);

struct MovieData{

string title,
director;

int yearReleased,
runningTime;
};

int main(int argc, char** argv) {

MovieData movie1;
MovieData movie2;

cout<<"Enter the following information for movie 1\n"<<endl;

cout<<"Title: ";
getline(cin, movie1.title);

cout<<"Director: ";
getline(cin, movie1.director);

cout<<"Year Released: ";
cin>>movie1.yearReleased;

cout<<"Run time (min): ";
cin>>movie1.runningTime;

cout<<"\nEnter the following information for movie 2 \n"<<endl;

cout<<"Title: ";
getline(cin, movie2.title);

cout<<"Director: ";
getline(cin, movie2.director);

cout<<"Year Released: ";
cin>>movie2.yearReleased;

cout<<"Run time (min): ";
cin>>movie2.runningTime;


display(movie1.title, movie1.director, movie1.yearReleased, movie1.runningTime);

display(movie2.title, movie2.director, movie2.yearReleased, movie2.runningTime);



system("PAUSE");
return 0;
}

void display(string title, string director, int yearOut, int runTime){

cout<<"Movie Information"<<endl;
cout<<"----------------"<<endl;

cout<<"Title: "<<title<<endl;
cout<<"Director: "<<director<<endl;
cout<<"Year released: "<<yearOut<<endl;
cout<<"Running time: "<<runTime<<endl;

}
Last edited on
closed account (E0p9LyTq)
PLEASE learn to use code tags so your code is easier to read. (You can go back and edit your post)
http://www.cplusplus.com/articles/jEywvCM9/

You are not being allowed to input the second movie's title because cin's buffer isn't cleared. Add the following code, for example:

1
2
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout<<"\nEnter the following information for movie 2 \n"<<endl;

http://www.cplusplus.com/reference/istream/istream/ignore/?kw=cin.ignore

Include the <limits> standard library too.

You already have a defined structure, why not pass that into your display function?

void display(MovieData&);

and

1
2
3
4
5
6
7
8
9
10
11
12
void display(MovieData& movie)
{

   cout<<"Movie Information"<<endl;
   cout<<"----------------"<<endl;

   cout<<"Title: "<<movie.title<<endl;
   cout<<"Director: "<<movie.director<<endl;
   cout<<"Year released: "<<movie.yearReleased<<endl;
   cout<<"Running time: "<<movie.runningTime<<endl;

}
Topic archived. No new replies allowed.