Thread: convert from C++ to C

  1. #1
    Unregistered
    Guest

    convert from C++ to C

    Hi all. Really need on how to convert this code from C++ to C.
    Its actually a program for an address book. Any help will be greatly appreciated

    #include <fstream.h>
    #include <cstring>
    #include <cstdio>

    class book {
    //these are only visible by class book and its member functions
    private:
    int choice;
    char *File;
    char name[30];
    char phone[13];
    char email[30];
    int srchtype;
    char srchstr[30];

    //these are visible to book objects
    public:

    //this constructor is called whenever an object of
    //class book is created to initialize our variables
    book() { File="addresses.dat"; choice = 0; };

    //function prototypes
    void ShowBook();
    void AddEntry();
    int Menu();
    void Print();
    void SearchMenu();
    bool Found(book&);

    //some helper functions
    void Flush() { while (cin.get() != '\n') cin.get(); }
    bool CheckOpen(fstream &f) {
    if (!f.is_open()) {
    cerr << "\nError opening " << File << endl;
    return false;
    }
    return true;
    }
    };

    /* the MAIN function */
    int main() {
    book b;

    while (b.Menu())
    ;

    return 0;
    }

    /* member function definitions */
    int book::Menu() {
    cout << "\n1. Add Contact\n"
    "2. Show Contacts\n"
    "3. Exit\n\n"
    "Choice: ";
    cin >> choice;

    switch (choice) {
    case 1: AddEntry(); break;
    case 2: ShowBook(); break;
    case 3: choice = 0; break;
    }
    return choice;
    }

    void book::AddEntry() {
    fstream fout(File,ios::end|ios::app|ios::binary);

    if (!CheckOpen(fout))
    return;
    else {
    Flush();
    cout << "\nEnter contact name: ";
    cin.getline(name,30);
    cout << "Phone number: ";
    cin.getline(phone,13);
    cout << "Email address: ";
    cin.getline(email,30);

    fout.write((char*)this,sizeof(*this));
    fout.close();
    }
    }

    void book::ShowBook() {
    fstream fin(File,ios::in|ios::binary);
    book temp;

    if (!CheckOpen(fin))
    return;
    else {
    SearchMenu();
    bool foundmatch = false;
    cout << "\n----- DISPLAYING CONTACTS -----\n";
    while (fin.read((char*)&temp,sizeof(temp)) && !fin.eof()) {
    if (Found(temp)) {
    temp.Print();
    foundmatch = true;
    }
    else if (srchtype == 4) {
    temp.Print();
    foundmatch = true;
    }
    }
    if (!foundmatch)
    cout << "\nNo matching entries found!\n";
    cout << "\n----- FINISHED DISPLAYING -----\n";
    fin.close();
    }
    }

    void book::Print() {
    cout << "\n Name: " << name << endl;
    cout << " Phone: " << phone << endl;
    cout << " Email: " << email << endl;
    }

    bool book::Found(book &temp) {
    if (srchtype == 1 && strstr(temp.name,srchstr) ||
    srchtype == 2 && strstr(temp.phone,srchstr) ||
    srchtype == 3 && strstr(temp.email,srchstr))
    return true;
    return false;
    }

    void book::SearchMenu() {
    cout << "\nSearch by:\n"
    "1. Name\n"
    "2. Phone#\n"
    "3. Email\n"
    "4. Show All\n\n"
    "Choice: ";
    (cin >> srchtype).get();
    if (srchtype != 4) {
    cout << "Enter the search value: ";
    cin.getline(srchstr,30);
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    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.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Unregistered
    Guest
    Tried to compile, but still have errors, about invalid external type for AddEntry and ShowBook. Also errors about missing ; before type , as shown below:
    NOt too sure about the file "ab''s function as well.

    ERRORS:

    // switch (b -> choice)
    // {
    // case 1: int AddEntry(b) ; break; // <----- missing ;
    // before 'type'
    // case 2: int ShowBook(b) ; break; // <----- missing ; //before 'type'
    // case 3: b -> choice = 0 ; break;
    }

    Start of Program:

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    typedef struct __book
    {
    int choice;
    char *File;
    char name[30];
    char phone[13];
    char email[30];
    int srchtype;
    char srchstr[30];
    } book;
    book * initBook (void)
    {
    book * b = malloc (sizeof (book));
    b -> File = malloc (14 * sizeof (char));
    strcpy (b -> File, "addresses.dat");
    b -> choice = 0;
    return 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: int AddEntry(b) ; break;
    case 2: int ShowBook(b) ; break;
    case 3: b -> choice = 0 ; break;
    }
    return b -> choice;
    }

    void Add_an_Entry (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);
    printf ("\nEnter phone number: ");
    fgets (b -> phone, 13, f);
    printf ("\nEnter email address: ");
    fgets (b -> email, 30, f);

    // I have no idea of what this line does...
    //fout.write((char*)this,sizeof(*this));
    fclose (f);
    }
    main()
    {
    book * b;
    b = initBook ();

    while (Menu(b)) ;

    return 0;
    }

  4. #4
    Unregistered
    Guest
    .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Convert char* time to const tm*
    By stickman in forum C++ Programming
    Replies: 10
    Last Post: 11-14-2006, 09:24 PM
  3. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM