Need help with getline()

Viewed 19894

Is there a reason why if in my program I am asking the user for input, and I do:

int number;
string str;
int accountNumber;

cout << "Enter number:";
cin >> number;
cout << "Enter name:";
getline(cin, str);
cout << "Enter account number:";
cin >> accountNumber;

Why after inputting the first number, it outputs "Enter Name", followed immediately by "Enter Account Number" before I even get to input my "str" for the getline(cin, str) line? Thanks!

7 Answers

Don't use getline(): it's a bad thing for memory allocation. Use fgets(). See fgets reference.

Related