getline function

hi, i am facing a strange problem in use of getline. i have used    ---   cin.getline(p.name,'\n' ) --- but it only picks first 10 characters and as a result corrupts next inputs. if i use - cin.getline(p.name,80 ) then it works well. please help me that why the first statement is not working correctly. for the reference i m pasting the code below  

***********************************
# include <iostream.h>
# include <conio.h>
     struct Person{
     char name[50];
     long phoneNumber;
     char gender;
     int age;
     };
void main()
{
     Person p;
     cout << "Enter name of person: " ;
     cin.getline(p.name,'\n' );                            // problem line
     cout << "Enter phone number: ";
     cin >> p.phoneNumber;
     cout << "Enter gender: ";
     cin >> p.gender;
     cout << "Enter age: ";
     cin >> p.age;
     cout<<"Name: "<<p.name<<"\n";
     cout<<"Phone Number: "<<p.phoneNumber<<"\n";
     cout<<"Gender: "<<p.gender<<"\n";
     cout<<"Age: "<<p.age<<"\n";
     getch ();
}
*****************************************

regards
fa.
fatimaoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

avizitCommented:
http://www.augustcouncil.com/~tgibson/tutorial/iotips.html#problems


std::cin.getline() can run into problems when used with std::cin >> var.

    * getline can be provided a third argument--a "stop" character. This character ends getline's input. The character is eaten and the string is terminated. Example:
      std::cin.getline(str, 100, '|')
    * If std::cin.getline() is not provided a "stop" character as a third argument, it will stop when it reaches a newline.



i.,e the stop charcater has to be third argument , if not it uses the newline
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
avizitCommented:
The second argument to "getline" is the number of characters to read. Hence when you put '\n' its read 10 as 10 is the ascii value of the newline
0
AxterCommented:
I recommend you use the global getline function instead, and use std::string instead of a c-style char array.

Also <iostream.h> is not part of the C++ standard.
To make your code portable, use <iostream> extensionless version instead, which is part of the C++ standard.

#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

     struct Person{
       string name;
     long phoneNumber;
     char gender;
     int age;
     };
void main()
{
     Person p;
     cout << "Enter name of person: " ;
       getline(cin, p.name, '\n');
     cout << "Enter phone number: ";
     cin >> p.phoneNumber;
     cout << "Enter gender: ";
     cin >> p.gender;
     cout << "Enter age: ";
     cin >> p.age;
     cout<<"Name: "<<p.name<<"\n";
     cout<<"Phone Number: "<<p.phoneNumber<<"\n";
     cout<<"Gender: "<<p.gender<<"\n";
     cout<<"Age: "<<p.age<<"\n";
     getch ();
}


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.