I don't really know C++ too well, but this is interesting enough to try.
Code:
//these are only visible by class book and its member functions
typedef struct __book
{
int choice;
char *File;
char name[30];
char phone[13];
char email[30];
int srchtype;
char srchstr[30];
} book;
// I get the feeling there actually is a way to make the elements of
// this structure invisible to anything outside class book, but am
// not sure. As is, anyone can access these elements, although I
// don't see that as a big problem.
//this constructor is called whenever an object of
//class book is created to initialize our variables
book * initBook (void)
{
book * b = malloc (sizeof (book));
b -> File = malloc (14 * sizeof (char));
strcpy (b -> File, "addresses.dat";
b -> choice = 0;
}
// This is prolly the biggest difference the implementations
// will have. For this, you will have to initialize your book as
// book * b = initBook (); instead of
// book b;
void Flush (void)
{
int c;
for (c = 0; c != '\n'; c = getchar());
return;
}
int Menu(book * b)
{
printf ("\n1. Add Contact\n"
"2. Show Contacts\n"
"3. Exit\n\n"
"Choice: ");
scanf ("%d", &b -> choice);
switch (b -> choice) {
case 1: AddEntry(b); break;
case 2: ShowBook(b); break;
case 3: b -> choice = 0; break;
}
return b -> choice;
}
void AddEntry (book * b)
{
FILE * f;
f = fopen (b -> File, "ab"); // I'm not too sure about the ab
// Not gonna bother implementing this helper function, so let's
// pretend the file always opens.
//if (!CheckOpen(fout))
//return;
//else {
Flush();
printf ("\nEnter contact name: ");
fgets (b -> name, 30, f);
// Similar changes for other entries...
//cout << "Phone number: ";
//cin.getline(phone,13);
//cout << "Email address: ";
//cin.getline(email,30);
// I have no idea of what this line does...
//fout.write((char*)this,sizeof(*this));
fclose (f);
}
Blegh, that's hard. Actually, the main function is pretty important to see how it will change too...
Code:
int main() {
book * b; // C uses pointers....
b = initBook ();
while (Menu(b)) ;
return 0;
}
Pretty much just have to change it so that you pass the pointer to be from every function to function, change your input and output calls to use C functions, and instead of just using the names of the private variables of class book, you have to use book -> variableName.
It really isn't difficult if you know C, it's just awfully tedius.