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!

how to work with structures...

Status
Not open for further replies.

ting

Technical User
Apr 27, 2001
9
US
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::eek:ut);
if(outfile.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();
if(choice > 3)
{
system(&quot;cls&quot;);
cout<<&quot;\t\t&quot; << &quot;INVALID&quot;<<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(&quot;cls&quot;);
cout<<endl<<endl;
cout<<&quot;\t\t&quot; << &quot;WHAT DO YOU WANT TO DO?&quot;<<endl<<endl;
cout<<&quot;\t\t&quot; << &quot;1. ADD AN ENTRY&quot;<<endl;
cout<<&quot;\t\t&quot; << &quot;2. DISPLAY ENTRIES&quot;<<endl;
cout<<&quot;\t\t&quot; << &quot;3. EXIT&quot;<<endl;
cout<<endl<<endl<<endl;
cout<<&quot;\t\t&quot; << &quot;ENTER YOUR CHOICE (1, 2 or 3): &quot;;
cin>>choice;
return choice;
}

void add_to_address_book(addresstype& addin)
{
system(&quot;cls&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');
}

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 << 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(&quot;cls&quot;);
cout<<&quot;All the entries are:&quot;<<endl;
cout<<addin.lastname<<&quot;, &quot;<<addin.firstname<<endl
<<addin.line1<<endl
<<addin.line2<<endl
<<addin.line3<<endl
<<addin.line4<<endl
<<addin.phone<<endl
<<endl;

}
 
Could you just say waht the trouble is? John Fill
1c.bmp


ivfmd@mail.md
 
Hi, I am finally able to log in again. I just finished sending you an email (don't know if it went thru) explaining what my problem is. So sorry I had to email you though. Well here it is again:
My problem is really the display function. When the program is run, the menu pops up and if choice 1 is picked to enter an entry it works. Then it returns to the main menu. Now when choice 2 is selected it does not display the entries. I need the entries to display the same way they were input. (it allows more than one entry after each entry is entered it returns to main menu and it allows you to choose choice 1 to enter another entry)
If the user enters more than one entry the display function should be able to display each entry one at a time, on its own screen and at the bottom of the screen a message saying 'press enter for the next entry'. After each entry has been viewed a message should appear saying 'all entries displayed' and then the user should be able to press enter to return to the main menu.
Also, my add_to_address_book function should convert all entries to upper case before saving them to the file (AddressBooktest.dat).
I thank you so much for replying to my message. Your help is gladly appreciated.
ting
 
Hi ting,

I read your problem and as far as I think there were certain direct errors like not specifying struct keyword before struct name in the prototype, inclusion of header files and the one wich is troubling you the most is U opened the input file stream but did not attach it to the file and hence the problem in the prog. If it was giving only logical error in your compiler then u may remove the struct keyword I have added and the heder file however in Dev-C++ which I m using compiler complained when I did not do the same.

So here goes the code ,...... Hope it helps you.

# include <iostream.h>
# include <iomanip.h>
# include <fstream.h>
# include <dos.h>
# include <stdlib.h>

const char FILENAME[80] = &quot;AddressBooktest.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;
ofstream outfile;

outfile.open(FILENAME, ios::eek:ut);
if(outfile.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();
if(choice > 3)
{
system(&quot;cls&quot;);
cout<<&quot;\t\t&quot; << &quot;INVALID&quot;<<endl<<endl<<endl;
}
else
{
//choice=menu();
switch(choice)
{
case 1: add_to_address_book(addin);
output_info(addin, outfile);

break;
case 2:
input_info(addin);
display_address_book(addin);
break;
}
}
}
}
outfile.close();
}

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

void add_to_address_book(addresstype& addin)
{
system(&quot;cls&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');
}

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 << endl;
}

void input_info(addresstype &addin)
{
ifstream infile;
infile.open(FILENAME, ios::in);
if(infile.fail())
{
cout<<endl;
cout<<FILENAME<<&quot; could not be opened for writing into.&quot;
<<endl;
cout<<&quot;The file might be corrupted.&quot;<<endl;
exit(1);
}
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');
}

void display_address_book(addresstype addin)
{

system(&quot;cls&quot;);
cout<<&quot;All the entries are:&quot;<<endl;
cout<<addin.lastname<<&quot;, &quot;<<addin.firstname<<endl
<<addin.line1<<endl
<<addin.line2<<endl
<<addin.line3<<endl
<<addin.line4<<endl
<<addin.phone<<endl
<<endl;

}

Regards,
SwapSawe.
 
Hi SwapSawe.
Thanks so much for your help.
I tried the code, but the display is not displaying all of the entries. I guess I have to put a loop in the input or display function??? Also I need each entry to display one at a time? Can I get a hint on that?
 
Merge the input_info function with display_address_book function and read the file char by char to terminate on each '#' sign while(!infile.eof()). I think this gives u a food for thought.

Regards,
SwapSawe.
 
Why don't you use classes? <- This is just a suggestion!
Welcome to the Pythian Games!
Long live Godess Athena!
dArktEmplAr of Delphi Oracle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top