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