why do I need cin.ignore here?

Hi, I'm writing this simple program, and for some reason if I don't use a cin.ignore at the end of the getInput function, the next time the function is called it skips the first entry. I understand why this may happen after the first getline, but here I'm using two getline's, then some cin's then calling the function again. Can someone explain what is happening with the buffer here?

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
86
87
88
89
90
91
92
93
94
95
96

using namespace std;

#include <iostream>
#include <iomanip>

// Structure Definition ------------------------------------------
    struct MovieData
    {
        string title;
        string director;
        int yearReleased;
        double runTime;
        double yearOneRevinue;
        double prodCosts;
    };

// Prototypes ----------------------------------------------------
MovieData *fillData(MovieData *);
void DisplayData(MovieData);


// Main **********************************************************
int main()
{
    MovieData var1;
    MovieData *ptr1 = &var1;

    MovieData var2;
    MovieData *ptr2 = &var2;






    fillData(ptr1);
    fillData(ptr2);

    DisplayData(var1);
    DisplayData(var2);
}

// Function Display Data -----------------------------------------
void DisplayData(MovieData record)
{
    cout << endl;
    cout << setw(40) << left << "Film: " << record.title << endl;
    cout << setw(40) << left <<"Director: " << record.director << endl;
    cout << setw(40) << left << "Year Released: " << record.yearReleased << endl;
    cout << setw(40) << left <<"Running Time: " << record.runTime << endl;
    cout << setw(40) << left << "Year 1 Revinue " << record.yearOneRevinue << endl;
    cout << setw(40) << left << "Production Costs " << record.prodCosts << endl;
    cout << endl;
}

// Function Fill Data --------------------------------------------
MovieData *fillData(MovieData *record)
{
    cout << "Please enter title: ";
    getline(cin, (*record).title);
    cout << endl;
    cout << "Please enter director for " << (*record).title << " : ";
    getline(cin, (*record).director);
    cout << endl;
    cout << "Please enter the year released for " << (*record).title << " : ";
    cin >> (*record).yearReleased;
    cout << endl;

    cout << "Please enter the run time for " << (*record).title << " : ";
    cin >> (*record).runTime;
    cout << endl;
    cout << "Please enter the first year revenue for " << (*record).title << " : ";
    cin >> (*record).yearOneRevinue;
    cout << endl;
    cout << "Please enter the production costs for " << (*record).title << " : ";
    cin >> (*record).prodCosts;
    cout << endl;
    cin.ignore(); // <--- Without this the next time the function is called,
                  //      it will skip the first entry on the 2nd call.
    return record;
}













Your first input operation reads until a newline character, and consumes the newline. Your last input operation, without the ignore, reads until whitespace but does not consume it. This means that the next time around, that whitespace is still there.

I personally recommend always reading entire lines of user input at a time and then parsing the line with a std::istringtream if you need to extract individual tokens (e.g. multiple numbers per line).
Thanks!
Topic archived. No new replies allowed.