English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

A program I wrote in c++ to store contact information to a text file only works correctly the first time through. The second time I try to run the file, I can only type in the file name and then hit [enter]. but nothing happens.
please help

2007-11-29 14:38:31 · 2 answers · asked by eman m 2 in Computers & Internet Programming & Design

//This program takes in contact information and lists them for the user
#include
#include
#include
#include
using namespace std;
struct Date
{
int month, day, year;
};

struct Address
{
string street, city, state, zipcode;
};

struct Person
{
string firstname, lastname, homephone, mobilephone, email;
Address address;
Date birthdate;
};
//Function protptypes
Person enterContact();
void saveContacts(Person contacts[], int);
void searchContacts(Person contacts[],int);
int main()
{
int numOfContacts = 0;
char choice;
int arraySize = 100;
ifstream input;
input.open("contacts.txt");

if(input)
{
input >> numOfContacts;
arraySize += numOfContacts;
}
Person contact[arraySize];
if(input)
{
for(int i = 0; i < numOfContacts; i++)
{
input.ignore();
getline(cin, contact[i].firstname);
getline(cin, contact[i].firstname);
getline(cin, contact[i].lastna

2007-11-29 14:58:18 · update #1

getline(cin, contact[i].address.street);
getline(cin, contact[i].address.city);
getline(cin, contact[i].address.state);
getline(cin, contact[i].address.zipcode);
getline(cin, contact[i].homephone);
getline(cin, contact[i].mobilephone);
getline(cin, contact[i].email);
input >> contact[i].birthdate.month;
input >> contact[i].birthdate.day;
input >> contact[i].birthdate.year;
}
input.close();
}
do
{
cout << endl;
cout << "Personal Information manager \n \n";
cout << "A = Add contact ";
cout << "\nS = Search For Contact";
cout << "\nX = exit";
cout << endl << endl;
cout << "Enter choice: ";
cin >> choice;

2007-11-29 15:01:04 · update #2

if( choice == 'A' || choice == 'a')
{
contact[numOfContacts] = enterContact();
numOfContacts++;
}

else if( choice == 'S' || choice == 's')
{
searchContacts(contact, numOfContacts);
}
else if( choice == 'X' || choice == 'x')
{
saveContacts(contact, numOfContacts);
}
}


while(choice != 'X');

return 0;
}
void saveContacts(Person contacts[], int numOfContacts)

2007-11-29 15:01:59 · update #3

{
ofstream output;
output.open("contacts.txt");
output << numOfContacts << endl;

for(int i = 0; i < numOfContacts; i++)
{
output << contacts[i].firstname << endl;
output << contacts[i].lastname << endl;
output << contacts[i].address.street << endl;
output << contacts[i].address.city << endl;
output << contacts[i].address.state << endl;
output << contacts[i].address.zipcode << endl;
output << contacts[i].homephone << endl;
output << contacts[i].mobilephone << endl;
output << contacts[i].email << endl;
output << contacts[i].birthdate.month << endl;
output << contacts[i].birthdate.day << endl;
output << contacts[i].birthdate.year << endl;
}

2007-11-29 15:02:22 · update #4

output.close();
}
void printContact(Person contact)
{
cout << contact.firstname << " " << contact.lastname << endl;
cout << contact.address.street << endl;
cout << contact.address.city << ", " << contact.address.state << " ";
cout << contact.address.zipcode << endl;
cout << "Home: " << contact.homephone << " ";
cout << "Mobile: " << contact.mobilephone << endl;
cout << contact.email << endl;
cout << "Birthday: " << contact.birthdate.month << "/";
cout << contact.birthdate.day << "/";
cout << contact.birthdate.year;
cout << endl << endl;
}
void searchContacts(Person contacts[], int numOfContacts)
{
int matches = 0;
string name;
cout << "/n Enter first or last name to print: ";
cin >> name;
cout << endl;

2007-11-29 15:03:20 · update #5

for( int i=0; i < numOfContacts; i++)
{
if (contacts[i].firstname == name || contacts[i].lastname == name)
{
matches++;
cout << "/nContact " << (i + 1) << ":" << endl;
printContact(contacts[i]);
}
}
cout << "\nTotal number of matching contacts: " << matches << endl;
}
Person enterContact()
{
Person contact;

cout << "Enter first name: ";
cin >> contact.firstname;
cout << "Enter last name: ";
cin >> contact.lastname;
cout << "Enter street: ";
cin.ignore();

2007-11-29 15:03:42 · update #6

cin >> contact.address.street;
cout << "Enter city: ";
cin >> contact.address.city;
cout << "Enter state: ";
cin >> contact.address.state;
cout << "Enter zip code: ";
cin >> contact.address.zipcode;
cout << "Enter home phone: ";
cin >> contact.homephone;
cout << "Enter mobile phone: ";
cin >> contact.mobilephone;
cout << "Enter email: ";
cin >> contact.email;
cout << "Enter birth month: ";
cin >> contact.birthdate.month;
cout << "Enter birth day: ";
cin >> contact.birthdate.day;
cout << "Enter birth year: ";
cin >> contact.birthdate.year;
cout << endl;
return contact;
}

2007-11-29 15:04:00 · update #7

PLease email me. Any Help Is appreciated.

2007-11-29 15:05:58 · update #8

2 answers

Ok, there's two problems here that I can see.

1) All your getline calls inside your main function should be getting input from "input", not from "cin". Fix that, and you'll get your data from the file.

Example:
getline(cin, contact[i].firstname);

should be changed to

getline(input, contact[i].firstname);

Also, in the code you posted, you do a getline to get the firstname twice. When you fix your getlines, make sure you're only doing that once or you'll throw off your data reading. I don't know if that was caused by copy/pasting the code into Yahoo or not, but just giving you the heads up.

2) It's not a good idea to declare your contact array using a dynamic variable. I'm referring to the following line:

Person contact[arraySize];

You should do one of two things here. Either assume a maximum number of "Persons" you're going to be dealing with (e.g., 100) and just declare the array to that size, or use a pointer to create a dynamic array. The easiest solution is to assume a value, the best solution is to use a dynamic array.

Hope that helps!

2007-11-29 14:41:17 · answer #1 · answered by flangeku 2 · 0 0

Check how you initialize your variables. It may work the first time only because those memory locations happen to be zero. After that whatever is in there will remain and interfere unless you are careful with clearing them before using them.

2007-11-29 14:42:16 · answer #2 · answered by Rich Z 7 · 0 0

fedest.com, questions and answers