Link to home
Start Free TrialLog in
Avatar of fatimao
fatimao

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of avizit
avizit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of avizit
avizit

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
SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial