0

Possible Duplicate:
cin.getline() is skipping an input in C++

I'm working on C++, to get some data of 2 movies like the following:

    struct Movie m1, m2;
    cout << "Enter first movie title: ";
    cin.getline(m1.title, 30); 
    cout << "Enter first movie director: ";
    cin.getline(m1.director, 30); 
    cout << "Enter first movie length: ";
    cin >> m1.length; 

    cout << "Enter second movie title: ";
    cin.getline(m2.title, 30);
    cout << "Enter second movie director: ";
    cin.getline(m2.director, 30); 
    cout << "Enter second movie length: ";
    cin >> m2.length;

But, I got surprised that in the output it doesn't all me to enter the title of the second movie. Here is the output

Enter first movie title: Girl

Enter first movie director: GirlD

Enter first movie length: 10

Enter second movie title: Enter second movie director: Boy

Enter second movie length: 20
Community
  • 1
  • 1
user2033484
  • 21
  • 1
  • 2
  • http://stackoverflow.com/search?q=getline+skipping. Alternatively, glance over to the column on the right labelled "Related". – chris Feb 01 '13 at 19:26

1 Answers1

0

the cin >> m1.length;, its only read the number, so it leaves the \n, the next getline will read the empty line, so you can read it as this:

string temp;
cin.getline(temp, 30);
stringstream sst(temp);
sst >> m1.lenght;
Rami Jarrar
  • 4,523
  • 7
  • 36
  • 52