hello i have a problem with getline function on structure * pointer

hello i have a problem i used an structure to make a database
of movies that the user sow i came from the tutorial on the site
i just made it to interact with a user

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
// array of structures//
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

struct movies_t {
  string title;
  int year;
};

void printmovie (movies_t movie);

movies_t * pointer;


int main ()
{
  string mystr;
  int n,size;
  
  cout <<"enter how mov"; //the amount of movies you want to enter//
  cin >> size;
  pointer = new movies_t [size];  
  for (n=0; n<size; n++)
  {
    cout << "Enter title: ";
    cin >> pointer[n].title; /*if i use the getline (cin,pointer[n].title) 
    my program skip the cout << "enter title and the 
    getline (cin,pointer[n].title and goes to the cout << enter year*/
    cout << "Enter year: ";
    cin >> pointer[n].year;
    
  }

  cout << "\nYou have entered these movies:\n";
  for (n=0; n<size; n++)
    printmovie (pointer[n]);

    cin >> size;
  return 0;
}

void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}


please help me

best regards,
Evgeny Bershadski
This happens because cin >> leaves the newline char in the input stream. Then getline finds that newline and therefore an empty string is read. To prevent this, use cin.ignore() after cin >>. cin.ignore discards a number of chars in the stream until a delimiter is found. The arguments you should use are cin.ignore(numeric_limits<streamsize>::max(), '\n');
thanks but i didnt learned it yet so i have no clue about the cin.ignore(numeric_limits<streamsize>::max(), '\n');

im still at data structures and havent seen it yet in the tutorial in this site
well, if you don't like cin.ignore(), you could use cin.get().. or you could add an additional getline after every cin >>..
Topic archived. No new replies allowed.