Getline problem

I'm having a problem with getline making me skip an input part of this program. Run the code and you will feel my pain lol Anyone know how I can fix 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
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
//Assignment 3
//CPII
//Movie Data

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

struct MovieData
	{
		string title;     //Declare Struct
		string director;
		int year;
		int length;
	};

    MovieData Movie1;   //Declare MovieData variables
	MovieData Movie2;
	

// getMovieData() stores info in the MovieData object 
void getMovieData(MovieData m , MovieData n);
 // prntMovie() displays the info in the MovieData object 
void prntMovie(MovieData m);

int main()
{
	getMovieData(Movie1 , Movie2);
	prntMovie(Movie1);
	prntMovie(Movie2);
	
}


//function definition for getMoviedata
void getMovieData(MovieData m , MovieData n)
{
	
	cout << "Enter movie title: ";
	getline(cin, Movie1.title);
	cout << endl;

	cout << "Enter director: ";
	getline(cin, Movie1.director);
	cout << endl;

	cout << "Enter the year movie was made: ";
	cin >> Movie1.year;
	cout << endl;

	cout << "Enter the length of the movie in minutes: ";
	cin >> Movie1.length;
	cout << endl;

	
	cout << "Enter movie title: ";
	getline(cin, Movie2.title);
	cin.ignore();
	cout << endl;

	cout << "Enter director: ";
	getline(cin, Movie2.director);
	cin.ignore();
	cout << endl;


	cout << "Enter the year movie was made: ";
	cin >> Movie2.year;
	cout << endl;

	cout << "Enter the length of the movie in minutes: ";
	cin >> Movie2.length;
	cout << endl;

}

//Function definition for prntMovie
void prntMovie(MovieData m)
{
	cout << "Movie Title: " << m.title << endl;
	cout << "Director: " << m.director << endl;
	cout << "Year: " << m.year << endl;
	cout << "Length: " << m.length << endl;
	cout << endl;
}
Last edited on
Where does it skip? I'm guessing at line 57, right?
Yea how did u know?
If i take out the cin.ignore() it acts even crazier
It skips there because when you read in Movie.length on line 52, it doesn't read in the newline character, just the number, so when it gets to line 57 it sucks up that newline character and says thanks for nothing. Try cin.ignore() BEFORE the getlines.
Last edited on
Thank You that worked perfectly. And thanks for telling me why that didn't work! I knew it was something small. Again Thank you.
One more question: How come it cuts off the first letter of the second movie director?
On line 57, getline reads in the newline, so you cin.ignore() the next character the user types on line 58.
so how do I fix it?
"Doctor, Doctor! It hurts when I do this!"
"Don't do that."

Essentially, don't cin.ginore() after a getline.
HAHA Ahhh I get the drift now. Thank you again!
Topic archived. No new replies allowed.