I have to program an Address book program in C++. So far I've done about 40% of the assignment.
The main requirements of the Address Book are the following (Which I am confused about are):
1. Display 20 Contacts at a time, using the space bar to go to the next screen.
2. Search for Contacts by Name.
3. Edit Contact.
So far I have programmed the following, let me know what I need to modify in order for me to display 20 contacts at a time. Any help will be greatly appreciated :cheesy:
#include <iostream>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;
struct Phonebook
{
char fname[15];
char lname[15];
char fullname[45];
char number[50];
char email[81];
};
bool openPhonebook(fstream &, char [81]);
void Displayphonelist(fstream &);
void Addnewcontacts(fstream &);
void Deletecontacts(fstream &);
void Editinfo();
void Searchcontact(fstream &);
int main()
{
fstream info;
int choice = 0;
int count = 0;
cout << "1. Display Contact List\n";
cout << "2. Add a new contact\n";
cout << "3. Delete a personal contact\n";
cout << "4. Edit personal contact information\n";
cout << "5. Search for contact\n";
cout << "6. Exit Program\n";
cout << "\nPlease enter choice number: \n";
cin >> choice;
switch(choice)
{
case 1:
{
if(!openPhonebook(info, "Contactlist.txt"))
{
cout << "File open error!\n";
exit(1); //Exits the program on error
}
cout << "File opened successfully.\n";
cout << "Now reading data from file.\n\n";
Displayphonelist(info);
info.close();
cout << "\nDone\n";
break;
}
case 2:
{
cout << "Add a new contact\n";
Addnewcontacts(info);
cout << "\nSaving Information....\n";
break;
}
case 3: cout << "Delete a personal contact\n";
break;
case 4: cout << "Edit personal contact information\n";
break;
case 5: cout << "Searching for contacts";
Searchcontact(info);
cout << "\nDone\n";
break;
case 6: cout << "Goodbye!\n";
exit(1);
default: cout << "Please enter valid choice\n";
cin >> choice;
}
return 0;
}
// END OF MAIN
bool openPhonebook(fstream &file, char *name)
{
bool status;
file.open(name, ios::in);
if (file.fail())
status = false;
else
status = true;
return status;
}
void Displayphonelist(fstream &file)
{
Phonebook Info;
file.getline(Info.fullname, 45);
file.getline(Info.number,50);
file.getline(Info.email, 81);
while(!file.eof())
{
cout << Info.fullname << endl;
cout << Info.number << endl;
cout << Info.email << endl;
file.getline(Info.fullname, 45);
file.getline(Info.number, 50);
file.getline(Info.email, 81);
}
}
void Addnewcontacts(fstream &file)
{
Phonebook Info;
char choice;
do
{
cout << "Opening file.....\n";
file.open("Contactlist.txt", ios::out| ios::app);
cout << "File open successfully\n";
if(!file)
{
cout << "Error opening Contactlist.txt\n";
exit(1);
}
if(!file.eof())
{
cout << "Enter first name: ";
cin >> Info.fname;
cout << "Enter last name: ";
cin >> Info.lname;
strcpy(Info.fullname, Info.fname);
strcat(Info.fullname, " ");
strcat(Info.fullname, Info.lname);
cout << "Enter number: ";
cin.ignore();
cin.getline(Info.number, 50);
cout << "Enter email: ";
cin.getline(Info.email, 81);
file << "Name: " << Info.fullname << endl;
file << "Number: " << Info.number << endl;
file << "Email: " << Info.email << endl;
file << "\n";
file.close();
}
cout << "Enter another contact?\n";
cin >> choice;
}while((choice == 'y')||(choice == 'Y'));
if((choice == 'n')||(choice == 'N'))
{
file.close();
}
}
void Searchcontact(fstream &file)
{
Phonebook Info;
char fname2[15];
char lname2[15];
char fullname2[45];
cout << "Opening file...\n";
file.open("Contactlist.txt", ios::in);
cout << "File open successfully\n";
while(!file)
{
cout << "Error opening file\n";
exit(1);
}
while(!file.eof())
{
cout << "Search by Full Name: \n";
cout << "First Name: \n";
cin >> fname2;
cout << "Last Name: \n";
cin >> lname2;
strcpy(fullname2, fname2);
strcat(fullname2, "");
strcat(fullname2, lname2);
if(strcmp(fullname2, Info.fullname))
{
cout << "There is no match\n";
file.close();
}
else
{
cout << "Name Found!\n";
cout << fullname2 << endl;
cout << Info.fullname << endl;
file.getline(Info.fullname, 45);
file.getline(Info.number, 50);
file.getline(Info.email, 81);
cout << "\nDone\n";
}
}
}
[edit]
I added code tags for you this time. Don't post again until you understand that code is easier to read with them. I also took the time to remove excessive newlines from your butt ugly formatting, changed tabs to spaces because tabs can be erratic, and made everything neat and tidy without actually changing the meat of your code. You're welcome. Now do me a favor and do the same thing without my help in the future. -Narue
[/edit]