Why doesn't getline work in a struct?

So I've got this weird problem. This is the struct I'm using:

#define DIM 50
struct user {
char name[DIM];
int age;
char sex;
user* next;
};
typedef user* puser;

Now, when I try to get the name with getline like this, it doesn't work:

puser ptr;

ptr=new user[1];
cout << "Enter user's name:" << endl;
cin.getline(ptr->name,DIM);
cout << "Enter user's age:" << endl;
cin >> ptr->age;
........

So I thought it was because I couldn't use getline like that, I mean with ptr->name. So I tried this:

puser ptr;
char A[DIM];
int i;

ptr=new user[1];
cout << "Enter user's name:" << endl;
cin.getline(A,DIM);
i=0;
while (A[i]!='\0')
{
ptr->name[i]=A[i];
i++;
}
ptr->name[i]='\0';
cout << "Enter user's age:" << endl;
cin >> ptr->age;
...........

In this way I thought I would get around the problem, but it doesn't work as well. In fact, I get the same output! Which, by the way, is this:

Enter user's name:
Enter user's age:

Like it doesn't care to wait for you to type, it just jumps to the next instruction. Of course, if I try to use getline in a standard situation, it works just fine. So where did I mistake?
Let me guess, there is a line std::cin >> <something> before getline, right?
In that case: http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction

TL;DR: std::cin >> std::ws; before getline;
It was in the main, but yes, that was the problem! Thank you so much! I didn't think that was the problem because I thought the buffer would be cleared when calling a new function... apparently it doesn't.
I thought the buffer would be cleared when calling a new function... apparently it doesn't.
Of cource it wouldn't. That would be silly: there could be megabytes of important data in that buffer.
Topic archived. No new replies allowed.