Inputing data into a file Issue

Apr 30, 2010 at 11:26pm
So I'm writing a simple program to mimic an address book for my C++ class. All the data inputted needs to be stored in an external file. I can't seem to get the program to store all the info into a file.

Here's the Code I'm using to input the info into a file:


string fname;
string lname;
string address;
string city;
string state;
int zip=0;
string phone;

ofstream file;
file.open("address.txt", ios::app);
cout<<"Please enter the First Name: ";
getline(cin, fname, '\n');
file<<fname<<endl;
cin.ignore(200, '\n');

cout<<"Please enter the Last Name: ";
getline(cin,lname, '\n');
cin.ignore(200, '\n');

cout<<"Please enter the address: ";
getline(cin,address, '\n');
file<<address<<endl;
cin.ignore(200, '\n');;

cout<<"Please enter city: ";
getline(cin,city, '\n');
file<<city<<endl;
cin.ignore(200, '\n');

cout<<"Please enter State: ";
getline(cin,state, '\n');
file<<state<<endl;
cin.ignore(200, '\n');

cout<<"Please enter zip: ";
cin>>zip;
file<<zip<<endl;

cout<<"Please enter phone number: ";
getline(cin,phone, '\n');
file<<phone<<endl;
cin.ignore(200, '\n');

Then here is what the input screen(pardon termonology) looks like with the input:
Menu
Add (N)ew Record
(V)iew Address Book
(D)elete address
(E)xit
n
Please enter the First Name: Kyle
Please enter the Last Name: Clayson

Please enter the address: Phony Address

Please enter city: Rockafeller Center

Please enter State: Russia

Please enter zip: 998865
Please enter phone number: Sorry not giving u this info

And finally here is what was stored in the file.

Phony Address
Rockafeller Center
Russia
998865

I can't seem to get all the data needed stored into the file. Any help or suggestions would be greatly appreciated. Thanx
Last edited on Apr 30, 2010 at 11:27pm
May 1, 2010 at 12:53am
Since you use getline to get the strings try omiting cin.ignore(200,'\n');s. Put cin.ignore(200,'\n'); only after you get the int variable zip (cin>>zip;). Let me know if this works.
Last edited on May 1, 2010 at 12:59am
May 1, 2010 at 2:05am
Thanx roshi, That worked out wonderful. just had to throw another another cin.ignore at the beginning as well.
Topic archived. No new replies allowed.