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.
**************************
# 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.