missing getline

can anyone help me with this. the last getline in my code doesn't work. can anyone explain and improve this code?. any help will be highly appreciated.

here 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
36
37
38
39
40
41
42
43
44
45
#include<iostream>
using namespace std;

int main()
{
    char gender;
    string note, minitial;
    string fname, lname, g1;
    
    cout << "REGISTER FIRST TO ACCESS THIS PROGRAM!" << endl;
    cout << "\n" << endl;
    
    cout << "ENTER YOUR... " << endl;
    cout << endl;
    
    cout << "FIRST NAME: ";
    getline(cin, fname);
    cout << endl;
    
    cout << "MIDDLE INITIAL: ";
    getline(cin,minitial);
    cout << endl;
    
    cout << "LAST NAME: ";
    getline(cin, lname);
    cout << endl;
    
    cout << "GENDER(M or F): ";
    cin >> gender;
    if(gender=='M' || gender=='m')
          g1 = "Mr.";
    else if(gender=='F' || gender=='f')
          g1 = "Ms.";
    cout << endl;
    
    
    cout << "THIS IS YOUR NOTE " << g1 << fname << " " << minitial << ". " << lname << endl;
    
    getline(cin, note);
    
    cout << note << endl;
    
    system("pause");
    return 0;
}

The problem is that cin >> gender; leaves the new line in the stream. getline() takes it as an empty string.

Use ignore() to remove the new line from the stream:

http://www.cplusplus.com/reference/istream/istream/ignore/
thank you very much coder777!!! it is now working...thank you very much ^_^
This is an example
1
2
    cin >> gender;
    cin.ignore(100, '\n');
The 100 doesn't mean that much. It ignores 100 or less new lines until there's nothing to ignore.
It ignores 100 or less new lines

Almost. It ignores 100 or less characters other than newline. It stops after the first newline.
http://www.cplusplus.com/reference/istream/istream/ignore/
Topic archived. No new replies allowed.