Hi. I need help creating an address book. I am using a structure for my code. I will post my code for anyone who can help me. I need this program to be able to add and display the entries. It also has to convert all the entries to upper case before saving them to a file.
Here is my code:
const char FILENAME[80] = "C:\\AddressBooktest.dat";
struct addresstype
{
char lastname[21];
char firstname[21];
char line1[31], line2[31], line3[31], line4[31];
char phone[14];
};
int menu();
void add_to_address_book(addresstype&);
void output_info(addresstype&, ofstream&);
void input_info(addresstype&);
void display_address_book(addresstype);
void main()
{
addresstype addin;
int choice=0;
ofstream outfile;
outfile.open(FILENAME, ios:ut);
if(outfile.fail())
{
cout<<endl;
cout<<FILENAME<<" could not be opened for output."
<<endl;
cout<<"The file might be corrupted."<<endl;
exit(1);
}
else
{
while(choice!=3)
{
choice=menu();
if(choice > 3)
{
system("cls"
cout<<"\t\t" << "INVALID"<<endl<<endl<<endl;
}
else
{
choice=menu();
switch(choice)
{
case 1: add_to_address_book(addin);
output_info(addin, outfile);
input_info(addin);
break;
case 2: display_address_book(addin);
}
}
}
}
outfile.close();
}
int menu()
{
int choice = 0;
//system("cls"
cout<<endl<<endl;
cout<<"\t\t" << "WHAT DO YOU WANT TO DO?"<<endl<<endl;
cout<<"\t\t" << "1. ADD AN ENTRY"<<endl;
cout<<"\t\t" << "2. DISPLAY ENTRIES"<<endl;
cout<<"\t\t" << "3. EXIT"<<endl;
cout<<endl<<endl<<endl;
cout<<"\t\t" << "ENTER YOUR CHOICE (1, 2 or 3): ";
cin>>choice;
return choice;
}
void add_to_address_book(addresstype& addin)
{
system("cls"
cin.ignore(100, '\n');
cout<<"\t\t" << "LAST NAME: ";
cin.get(addin.lastname, 21, '\n');
cin.ignore(100, '\n');
cout<<"\t\t" << "FIRST NAME: ";
cin.get(addin.firstname, 21, '\n');
cin.ignore(100, '\n');
cout<<"\t\t" << "ADDRESS LINE 1: ";
cin.get(addin.line1, 31, '\n');
cin.ignore(100, '\n');
cout<<"\t\t" << "ADDRESS LINE 2: ";
cin.get(addin.line2, 31, '\n');
cin.ignore(100, '\n');
cout<<"\t\t" << "ADDRESS LINE 3: ";
cin.get(addin.line3, 31, '\n');
cin.ignore(100, '\n');
cout<<"\t\t" << "ADDRESS LINE 4: ";
cin.get(addin.line4, 31, '\n');
cin.ignore(100, '\n');
cout<<"\t\t" << "TELEPHONE NUMBER: ";
cin.get(addin.phone, 14, '\n');
//cin.ignore(100, '\n');
}
void output_info(addresstype& addin, ofstream& outfile)
{
outfile<<addin.lastname << "#"
<<addin.firstname << "#"
<<addin.line1 << "#"
<<addin.line2 << "#"
<<addin.line3 << "#"
<<addin.line4 << "#"
<<addin.phone << endl;
}
void input_info(addresstype& addin)
{
ifstream infile;
infile.get(addin.lastname, 21, '#');
infile.ignore(1);
infile.get(addin.firstname, 21, '#');
infile.ignore(1);
infile >> addin.line1;
infile.ignore(1);
infile >> addin.line2;
infile.ignore(1);
infile >> addin.line3;
infile.ignore(1);
infile >> addin.line3;
infile.ignore(1);
infile >> addin.phone;
infile.ignore(100, '\n');
/*infile.get(addin.line1, 31, '#');
infile.ignore(1);
infile.get(addin.line2, 31, '#');
infile.ignore(1);
infile.get(addin.line3, 31, '#');
infile.ignore(1);
infile.get(addin.line4, 31, '#');
infile.ignore(1);
infile.get(addin.phone, 14, '#');
infile.ignore(1);*/
}
void display_address_book(addresstype addin)
{
system("cls"
cout<<"All the entries are:"<<endl;
cout<<addin.lastname<<", "<<addin.firstname<<endl
<<addin.line1<<endl
<<addin.line2<<endl
<<addin.line3<<endl
<<addin.line4<<endl
<<addin.phone<<endl
<<endl;
}
Here is my code:
const char FILENAME[80] = "C:\\AddressBooktest.dat";
struct addresstype
{
char lastname[21];
char firstname[21];
char line1[31], line2[31], line3[31], line4[31];
char phone[14];
};
int menu();
void add_to_address_book(addresstype&);
void output_info(addresstype&, ofstream&);
void input_info(addresstype&);
void display_address_book(addresstype);
void main()
{
addresstype addin;
int choice=0;
ofstream outfile;
outfile.open(FILENAME, ios:ut);
if(outfile.fail())
{
cout<<endl;
cout<<FILENAME<<" could not be opened for output."
<<endl;
cout<<"The file might be corrupted."<<endl;
exit(1);
}
else
{
while(choice!=3)
{
choice=menu();
if(choice > 3)
{
system("cls"
cout<<"\t\t" << "INVALID"<<endl<<endl<<endl;
}
else
{
choice=menu();
switch(choice)
{
case 1: add_to_address_book(addin);
output_info(addin, outfile);
input_info(addin);
break;
case 2: display_address_book(addin);
}
}
}
}
outfile.close();
}
int menu()
{
int choice = 0;
//system("cls"
cout<<endl<<endl;
cout<<"\t\t" << "WHAT DO YOU WANT TO DO?"<<endl<<endl;
cout<<"\t\t" << "1. ADD AN ENTRY"<<endl;
cout<<"\t\t" << "2. DISPLAY ENTRIES"<<endl;
cout<<"\t\t" << "3. EXIT"<<endl;
cout<<endl<<endl<<endl;
cout<<"\t\t" << "ENTER YOUR CHOICE (1, 2 or 3): ";
cin>>choice;
return choice;
}
void add_to_address_book(addresstype& addin)
{
system("cls"
cin.ignore(100, '\n');
cout<<"\t\t" << "LAST NAME: ";
cin.get(addin.lastname, 21, '\n');
cin.ignore(100, '\n');
cout<<"\t\t" << "FIRST NAME: ";
cin.get(addin.firstname, 21, '\n');
cin.ignore(100, '\n');
cout<<"\t\t" << "ADDRESS LINE 1: ";
cin.get(addin.line1, 31, '\n');
cin.ignore(100, '\n');
cout<<"\t\t" << "ADDRESS LINE 2: ";
cin.get(addin.line2, 31, '\n');
cin.ignore(100, '\n');
cout<<"\t\t" << "ADDRESS LINE 3: ";
cin.get(addin.line3, 31, '\n');
cin.ignore(100, '\n');
cout<<"\t\t" << "ADDRESS LINE 4: ";
cin.get(addin.line4, 31, '\n');
cin.ignore(100, '\n');
cout<<"\t\t" << "TELEPHONE NUMBER: ";
cin.get(addin.phone, 14, '\n');
//cin.ignore(100, '\n');
}
void output_info(addresstype& addin, ofstream& outfile)
{
outfile<<addin.lastname << "#"
<<addin.firstname << "#"
<<addin.line1 << "#"
<<addin.line2 << "#"
<<addin.line3 << "#"
<<addin.line4 << "#"
<<addin.phone << endl;
}
void input_info(addresstype& addin)
{
ifstream infile;
infile.get(addin.lastname, 21, '#');
infile.ignore(1);
infile.get(addin.firstname, 21, '#');
infile.ignore(1);
infile >> addin.line1;
infile.ignore(1);
infile >> addin.line2;
infile.ignore(1);
infile >> addin.line3;
infile.ignore(1);
infile >> addin.line3;
infile.ignore(1);
infile >> addin.phone;
infile.ignore(100, '\n');
/*infile.get(addin.line1, 31, '#');
infile.ignore(1);
infile.get(addin.line2, 31, '#');
infile.ignore(1);
infile.get(addin.line3, 31, '#');
infile.ignore(1);
infile.get(addin.line4, 31, '#');
infile.ignore(1);
infile.get(addin.phone, 14, '#');
infile.ignore(1);*/
}
void display_address_book(addresstype addin)
{
system("cls"
cout<<"All the entries are:"<<endl;
cout<<addin.lastname<<", "<<addin.firstname<<endl
<<addin.line1<<endl
<<addin.line2<<endl
<<addin.line3<<endl
<<addin.line4<<endl
<<addin.phone<<endl
<<endl;
}