Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help finishing code for addressbook....

Status
Not open for further replies.

ting

Technical User
Apr 27, 2001
9
US
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:
Code:
const char FILENAME[80] = &quot;C:\\MS Visual C++\\Programs\\AddressBook.dat&quot;;

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<<&quot; could not be opened for output.&quot;<<endl;
		cout<<&quot;The file might be corrupted.&quot;<<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(&quot;cls&quot;);
				cout<<&quot;\t\t&quot; << &quot;INVALID ENTRY\n\n\n&quot;;
			}  			
		}
	}
}

int menu()
{    
	int choice = 0;
	cout<<endl<<endl;
	cout<<&quot;\t\t&quot; << &quot;WHAT DO YOU WANT TO DO?\n\n\n&quot;; 
	cout<<&quot;\t\t&quot; << &quot;1. ADD AN ENTRY\n&quot;;
	cout<<&quot;\t\t&quot; << &quot;2. DISPLAY ENTRIES\n&quot;;
	cout<<&quot;\t\t&quot; << &quot;3. EXIT\n&quot;;
	cout<<&quot;\n\n\n&quot;;
	cout<<&quot;\t\t&quot; << &quot;ENTER YOUR CHOICE (1, 2 or 3):  &quot;;
	cin>>choice;
	cout<<&quot;\n&quot;;
	return choice;
}

void add_to_address_book(addresstype& addin)
{
	system(&quot;cls&quot;);
	cout<<&quot;\n\n&quot;<<&quot;\t\tEnter the items requested.\n&quot;;
	cout<<&quot;\t\tFour lines are provided for the address.\n&quot;;
	cout<<&quot;\t\tPress <ENTER> if an address line is not needed.\n\n&quot;;
	cout<<&quot;\n\n\n&quot;;

	cin.ignore(100, '\n');
	cout<<&quot;\t\t&quot; << &quot;LAST NAME: &quot;;
	cin.get(addin.lastname, 21, '\n');
 
	cin.ignore(100, '\n');
	cout<<&quot;\t\t&quot; << &quot;FIRST NAME: &quot;;
	cin.get(addin.firstname, 21, '\n');
 
	cin.ignore(100, '\n');
	cout<<&quot;\t\t&quot; << &quot;ADDRESS LINE 1: &quot;;
	cin.get(addin.line1, 31, '\n');

	cin.ignore(100, '\n');
	cout<<&quot;\t\t&quot; << &quot;ADDRESS LINE 2: &quot;;
	cin.get(addin.line2, 31, '\n');

	cin.ignore(100, '\n');
	cout<<&quot;\t\t&quot; << &quot;ADDRESS LINE 3: &quot;;
	cin.get(addin.line3, 31, '\n');

	cin.ignore(100, '\n');
	cout<<&quot;\t\t&quot; << &quot;ADDRESS LINE 4: &quot;;
	cin.get(addin.line4, 31, '\n');

	cin.ignore(100, '\n');
	cout<<&quot;\t\t&quot; << &quot;TELEPHONE NUMBER: &quot;;
	cin.get(addin.phone, 14, '\n');
	cin.ignore(100, '\n');    
	system(&quot;cls&quot;);
}

void input_info(addresstype &addin)
{
	ifstream infile;
	infile.open(FILENAME, ios::in);
	if(infile.fail())
	{
		cout<<&quot;\n&quot;;
		cout<<FILENAME<<&quot; could not be opened for writing into.\n&quot;;
		cout<<&quot;The file might be corrupted.\n&quot;;
		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 << &quot;#&quot;
		   <<addin.firstname << &quot;#&quot;
		   <<addin.line1 << &quot;#&quot;
		   <<addin.line2 << &quot;#&quot;
		   <<addin.line3 << &quot;#&quot;
		   <<addin.line4 << &quot;#&quot;
		   <<addin.phone << &quot;\n&quot;;
}

void display_address_book(addresstype addin)
{

	system(&quot;cls&quot;);
	cout<<&quot;All the entries are:\n&quot;;

	//while(///) //don't know what to put in this loop
	cout<<addin.lastname<<&quot;, &quot;<<addin.firstname<<&quot;\n&quot;
        <<addin.line1<<&quot;\n&quot;
        <<addin.line2<<&quot;\n&quot;
        <<addin.line3<<&quot;\n&quot;
        <<addin.line4<<&quot;\n&quot;
        <<addin.phone<<&quot;\n&quot;
        <<&quot;\n&quot;;     
	//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<<&quot;PRESS ANY KEY FOR THE NEXT ENTRY&quot;<<endl;
		cin.get();
		//close while loop
		cout<<&quot;ALL ENTRIES HAVE BEEN DISPLAYED.&quot;<<endl;
		cout<<&quot;PRESS ANY KEY TO CONTINUE.&quot;<<endl;
		cin.get();
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top