don't click here

Converting my Database Manager to C++

Discussion in 'Technical Discussion' started by saxman, Dec 17, 2010.

  1. saxman

    saxman

    Oldbie Tech Member
    I wrote a database manager a while back. It's one portion of a larger project I am doing -- my own point-of-sale application. For the second part (creating a user interface), I downloaded Microsoft Visual C++. The thing is, it likes C++ for a lot of things, and my database code was written in C (because that's the language I know). Obviously VC++ is having issues with some of the code as it doesn't strictly comply with C++ standards (or something). To make further production of my software as painless as possible, I want to get my database manager C++ compliant so that VC++ won't complain and can compile it as a ".CPP" file.

    Here's my database manager:
    http://www.4shared.com/file/765LzJNV/poscode.html

    Since my knowledge of C++ isn't that great, it would be great if someone who is could take a look at my code and tell me very specifically what I need to change. I also usually struggle with #include statements as I don't always use them correctly in a project (included files get included multiple times it seems). So if anyone could help me with these things, it would be a great help to me. Then I could figure the API portion of the project out myself, I think, and would be well on my way to completing my POS program! It's for my business.
     
  2. FraGag

    FraGag

    Tech Member
    If you're using Visual Studio 2010, you won't get editor support for C, only for C++ (this is apparently going to be fixed in the next version; they dropped it in VS2010 because the editor was rewritten using WPF).

    It would be good to convert this code to C++ using classes and encapsulation (I don't think you need inheritance, let alone polymorphism). I can convert the code this way if you want, or I can simply make the necessary changes to make it valid C++ so that you can continue using it the way you used to.

    I've also noticed that you are declaring global variables without extern... either you put everything in 2 files for simplicity, or you were only including the header once... I know for sure that it won't work in C++.

    Also, to avoid multiple-include problems, the recommended solution is to put a header guard:

    Code (Text):
    1. #ifndef MY_HEADER_H
    2. #define MY_HEADER_H
    3.  
    4. /* your header here */
    5.  
    6. #endif /* MY_HEADER_H */
    Alternatively, if you're only using VC++, you can simply put #pragma once at the beginning of a header.
     
  3. saxman

    saxman

    Oldbie Tech Member
    It'd be interesting to see it converted to C++ code with classes and encapsulation.
     
  4. Revival

    Revival

    The AppleTalk Network System Member
    200
    0
    16
    The big incompatibility between C++ and C, in my opinion, is that C++ is more strongly-typed, so simply compiling C code as C++ will encounter problems (I found this was very common with malloc()). The solution is to explicitly cast wherever you see an 'invalid type conversion' or similar error.
     
  5. FraGag

    FraGag

    Tech Member
    I turned your little innocent database manager into a monster (actually, the demo app is a monster, the database manager is still pretty innocent). I tried to keep the same file format, hopefully I didn't break anything (I didn't run the original code at all...).

    I noticed a few bugs in your original code:
    • len_notes in VENDOR was of type char, but v_work.notes is initialized to a buffer of length 16384. However, SaveDatabase() writes an object of size 2. All other len_notes members are shorts, so I assumed it was a mistake.
    • The Write* functions were setting the time_modified on an object, then overwriting the whole object (including the time_modified). I swapped the operations so that the time_modified would actually be updated.
    • In OpenDatabase() you have this:
      Code (Text):
      1. if(wp_list[n] != 0){
      2.     fseek(db_file[databases-1], wp_list[I], SEEK_SET);
      I think you meant to use I in both places (the same error also appears with wi_list and ws_list).
    • OpenDatabase() doesn't initialize the vacant field in any of the structs.

    I split each class to its own pair of files. I tried to put the constants where they were used -- I put TENDER_CASH in Payment.h, not sure if it's the right place... I removed the "database manager" concept; now, you instantiate a Database object and work on it. main.cpp is a demo application that visits pretty much everything in the code; it may be hard to read, though.

    My code uses classes, templates and some features that were introduced in C++0x (std::array). Therefore, you'll need Visual C++ 2010 to compile it. I didn't put a lot of comments; if you have any questions (and I'm sure you'll have a lot of them), you can ask me, or you can do your own research on the net.

    Download