Hi, i'm a newbie, and i have a trouble with a program.
The program doesn't work as i desire. This is a little fragment of my program:

//Requesting data from users.
            cout<<"\n\nEnter your last name:\n\t";
            getline(cin,flast);
            cout<<"\nEnter your mother's maiden last name:\n\t";
            getline(cin,mlast);
            cout<<"\nEnter your first name:\n\t";
            getline(cin,name);

When i run the program, it skips the first getline() statement, but runs the other statements perfectly, doing something like that:

Enter your last name:
<--This line is skipped
Enter your mother's maiden last name:
McCoy
Enter your first name:
Paul

Please, help me with this!!!

P.S.: I'm sorry if you can't understand me well, but i speak a little english.

Recommended Answers

All 4 Replies

Do you have a line like cin >> somevariable; up above that point? There's probably a stray newline left in the stream from where you pressed enter during the cin operation.

int i;
string str;
cin >> i; //enter in 10 + <enter>, 10 goes into i
getline(cin,str); //getline picks up enter ('\n') and thinks input is complete

See the sticky thread in this forum about flushing the input buffer. But, if it's just that one stray '\n' putting in a cin.ignore(); before your getline statement should do the trick.

check your original program,sometimes other syntax errors produce no errors but they r masked by your other errors ;)
anyway check header file?
declared that string?
semicolon?

Hey try getting the input name using cin only like

cin>>flast;

see whether this works and then proceed

anyway check header file?
declared that string?
semicolon?

What would any of those have to do with the "skipping over" of the getline at runtime?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.