Getline displaying empty character

Hello,
I'm using the getline method to get data from the user, store it in a variable called description and then display it later in the code. I use this variable twice because I'm implementing polymorphism. Everything else in my code works, except that it displays the second description as " " (without the quotes). Below is my code.

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
cout << endl;
        cin.ignore(1);
        cout << "Enter  Name of pen: "; getline(cin, description);              cout << description;
        cin.clear();
        cout << "Enter stock number: "; cin >> stockNumber;
        cout << "Enter quantity: "; cin >> quantity;
        cout << "Enter price: "; cin >> price;
        cout << "Enter pen color (1,2,3): "; cin >> nPenColor;
        cout << "Enter ink color (1,2,3): "; cin >> nInkColor;
        supplyList[3] = new Pen(description, stockNumber, quantity, price, nPenColor, nInkColor  );

        //store pointer to MemPen obect mp1 in [2]
        //
        cout << description;
        cout << endl;
        description = " ";
        cout << "Test" << endl;
        cout << des2;

        cout << "Enter  Name of pen: "; getline(cin,description); //Error occurs here           
 cout << description;


        cin.ignore(1000,'\n');

        cout << "Enter stock number: "; cin >> stockNumber;
        cout << "Enter quantity: "; cin >> quantity;
        cout << "Enter price: "; cin >> price;
        cout << "Enter pen color (1,2,3): "; cin >> nPenColor;
        cout << "Enter ink color (1,2,3): "; cin >> nInkColor;
        cout << "Enter memory size in Mb: "; cin >> memory;
        //
        //loop to print out ALL elements in the array that point to an object,
        //that is, there is a valid address in the element of the array.
        supplyList[7] = new MemPen(  des2,  stockNumber, quantity, price, nPenColor, nInkColor, memory);
Using cin>> leaves a newline in the buffer, so a getline() immediately following will be read as just an enter, resulting in storing an empty string. You need to ignore the newline from line 9 before using getline() on line 20.
I thought that's what cin.ignore(1000,'\n'); on line 20 did?
The ignore() is on line 24, after the getline() on line 20. It needs to be before it.
Thanks a bunch, this mystery is solved scooby doo.
Topic archived. No new replies allowed.