while loop keeps looping without getting input from user

struct stu_dat //outside main function
{
int rollno;
char name[45],
float average;
}s1;

char ans='y'; //inside main function
while(ans=='y')
{
cin>>s1.rollno;
cin.get(s1.name,45);
cin>>s1.average;
cout<<"want to enter more data?';
cin>>ans;
}


No compilation problem.when executing prompt waits for inputting rollno
but, as soon as i enter a char string it keeps looping displaying the "want to enter more data?".i cant understand what is going on,as there is no compilation problem and runs good till i input the name.
replace cin.get with cin >>. Or if you insist with going with methods from istream, use cin.getline instead
could you please explain why get() function is causing this ?
Because it leaves a trailing newline character in the input stream. See this answer for a fix:

http://www.cplusplus.com/forum/beginner/118890/#msg647856
Topic archived. No new replies allowed.