getline and cin output incorrect

hey guys,

i have a simple code, i know it's something i'm just missing but i have no clue and tried searching online. so... i have a structure with two strings and one in as fields, now i use 'getline' to get both name and address but when i type in the phone number with cin.ignore i'm missing the first digit. here it is:

#include <iostream>
#include <string>

using namespace std;

struct Person_Information {
string Name;
string Address;
int phone_number;
};

int main ()
{

Person_Information Person;

cout << "Enter your Name: " << endl;
getline(cin, Person.Name, '\n');
cout << "Enter your address: " << endl;
getline(cin, Person.Address, '\n');
cin.ignore();
cout << "Lastly enter your phone number with (no '-' necessary)" << endl;
cin >> Person.phone_number;
cout << endl << endl;

cout << "Name: " << Person.Name << endl << "Address: " << Person.Address << endl << "Phone Number: " << Person.phone_number;
}
Avrage wrote:
when i type in the phone number with cin.ignore i'm missing the first digit.

That's exactly what cin.ignore() does, it reads and discards one character. In your case, it reads and discards the first digit of the phone number. If you need that digit, remove cin.ignore().
Last edited on
Another thing to consider is that phone numbers may exceed the max value of an int variable.

http://www.cplusplus.com/reference/climits/

For example, I entered the following:
John Doe
123 Broadway
6462221111

and the output was this:
Name: John Doe
Address: 123 Broadway
Phone Number: 2147483647
Ok I added cin.ignore() because i was getting the number 2147483647 that wildblue received back. wildblue what type should i use here ? i didn't know int had a limit. Thank you both
I would probably store it as another string of data for the person.
thanks blue, i ended up taking this route. before going with string i tried long (because of the link you sent) i still got the 214 number
Topic archived. No new replies allowed.