Hi. I need help finishing this code. I had this code posted weeks ago. But I noticed a couple of things i was doing wrong. I tried to fix them. Also I have some comments in the code saying what each line is supposed to do. I'm asking of anyone can tell me how to write those lines. I seem to not know what I'm doing and I'm begging for someone's help.
My code is supposed to be able to add entries to a file and display them. The user can enter as many entries as desired by repeatedly selecting option 1 on the menu. If a user wishes to see the entries in the address book, choose option 2. The display should display each entry at a time, example:
SMITH, JANE
20 PARK AVE
NEW YORK, NY
212-342-1122
PRESS ANY KEY FOR THE NEXT ENTRY
When a key is pressed after the last record in the file is displayed,
the following screen should appear:
ALL ENTRIES HAVE BEEN DISPLAYED.
PRESS ANY KEY TO CONTINUE
Pressing any key will return to the menu.
Also the I need all entries to be converted to uppercase before saving them to a file. The display should display everything in caps.
Also, The last name and first name should be converted to the single form: LAST, FIRST. The resulting record should be written to the file with '#' characters separating the fields and <eol> (end of line - endl or '\n') after the telephone number. For example:
SMITH, JANE#20 PARK AVE#NEW YORK, NY###212-342-1122<eol><eof>
Note that if lines of address are missing (i.e. the field consists
of the empty string; notice the three consecutive # characters for
the entry above) then they should not be displayed between the last
address line that is not empty and the telephone number.
So the display should look like:
SMITH, JANE
20 PARK AVE
NEW YORK, NY
212-342-1122
I don't know how to do that part.
Can anyone help me out please, asap!!!
thank you and here is my code:
My code is supposed to be able to add entries to a file and display them. The user can enter as many entries as desired by repeatedly selecting option 1 on the menu. If a user wishes to see the entries in the address book, choose option 2. The display should display each entry at a time, example:
SMITH, JANE
20 PARK AVE
NEW YORK, NY
212-342-1122
PRESS ANY KEY FOR THE NEXT ENTRY
When a key is pressed after the last record in the file is displayed,
the following screen should appear:
ALL ENTRIES HAVE BEEN DISPLAYED.
PRESS ANY KEY TO CONTINUE
Pressing any key will return to the menu.
Also the I need all entries to be converted to uppercase before saving them to a file. The display should display everything in caps.
Also, The last name and first name should be converted to the single form: LAST, FIRST. The resulting record should be written to the file with '#' characters separating the fields and <eol> (end of line - endl or '\n') after the telephone number. For example:
SMITH, JANE#20 PARK AVE#NEW YORK, NY###212-342-1122<eol><eof>
Note that if lines of address are missing (i.e. the field consists
of the empty string; notice the three consecutive # characters for
the entry above) then they should not be displayed between the last
address line that is not empty and the telephone number.
So the display should look like:
SMITH, JANE
20 PARK AVE
NEW YORK, NY
212-342-1122
I don't know how to do that part.
Can anyone help me out please, asap!!!
thank you and here is my code:
Code:
const char FILENAME[80] = "C:\\MS Visual C++\\Programs\\AddressBook.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(struct addresstype &);
void output_info(struct addresstype &, ofstream &);
void input_info(struct addresstype &);
void display_address_book(struct addresstype);
void main()
{
struct addresstype addin;
int choice=0;
ifstream infile;
ofstream outfile;
infile.open(FILENAME, ios::app);
if(infile.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();
switch(choice)
{
case 1: add_to_address_book(addin);
input_info(addin);
break;
case 2:
output_info(addin, outfile);
display_address_book(addin);
break;
}
if(choice > 3)
{
system("cls");
cout<<"\t\t" << "INVALID ENTRY\n\n\n";
}
}
}
}
int menu()
{
int choice = 0;
cout<<endl<<endl;
cout<<"\t\t" << "WHAT DO YOU WANT TO DO?\n\n\n";
cout<<"\t\t" << "1. ADD AN ENTRY\n";
cout<<"\t\t" << "2. DISPLAY ENTRIES\n";
cout<<"\t\t" << "3. EXIT\n";
cout<<"\n\n\n";
cout<<"\t\t" << "ENTER YOUR CHOICE (1, 2 or 3): ";
cin>>choice;
cout<<"\n";
return choice;
}
void add_to_address_book(addresstype& addin)
{
system("cls");
cout<<"\n\n"<<"\t\tEnter the items requested.\n";
cout<<"\t\tFour lines are provided for the address.\n";
cout<<"\t\tPress <ENTER> if an address line is not needed.\n\n";
cout<<"\n\n\n";
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');
system("cls");
}
void input_info(addresstype &addin)
{
ifstream infile;
infile.open(FILENAME, ios::in);
if(infile.fail())
{
cout<<"\n";
cout<<FILENAME<<" could not be opened for writing into.\n";
cout<<"The file might be corrupted.\n";
exit(1);
}
/*char toupper(addin.lastname[21]); should this be here? And how
do I change all the cases to upper case
[21] is probably only changing the 20th element*/
while(!infile.eof())
{
infile >> addin.lastname;
infile.ignore(1);
infile >> addin.firstname;
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.close();
}
void output_info(addresstype& addin, ofstream& outfile)
{
outfile<<addin.lastname << "#"
<<addin.firstname << "#"
<<addin.line1 << "#"
<<addin.line2 << "#"
<<addin.line3 << "#"
<<addin.line4 << "#"
<<addin.phone << "\n";
}
void display_address_book(addresstype addin)
{
system("cls");
cout<<"All the entries are:\n";
//while(///) //don't know what to put in this loop
cout<<addin.lastname<<", "<<addin.firstname<<"\n"
<<addin.line1<<"\n"
<<addin.line2<<"\n"
<<addin.line3<<"\n"
<<addin.line4<<"\n"
<<addin.phone<<"\n"
<<"\n";
//or maybe I can have an if statement
if(addin.lastname[21] < '1') //don't know what to write for the if statement
{
//have a while loop here
cout<<"PRESS ANY KEY FOR THE NEXT ENTRY"<<endl;
cin.get();
//close while loop
cout<<"ALL ENTRIES HAVE BEEN DISPLAYED."<<endl;
cout<<"PRESS ANY KEY TO CONTINUE."<<endl;
cin.get();
}
}