Thread: Need help comparing db field values in c++

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    10

    Need help comparing db field values in c++

    Hello,

    I am trying to compare 2 different text fields from ms sql db, and want to set a condition based on wether or not the 2 text fields contain the same text or not. I believe that c++ is setup to use a character array to hold the db fields, and I have tried to use strcmp to compare the values, but it seems like I am only getting comparisions on whether or not the db fields are NULL or not, and that it is not actually comparing the db field values.

    Basically all I need to do is:
    if field1 value is not equal to field2 value, then outofrange=1.

    I used != to specify inequality, but like I said before, it's almost like it's not looking at the value of the fields, only at whether or not the fields are NULL.

    Am I doing something wrong?

    I am pretty new to c++, apologies for my ignorence.

    Thanks in advance for any help!

    M

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    It depends on how you are accessing the db.

    If for instance you are using ADO, the return is a VARIANT so it pretty much depends on how you are linking to the db

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    If you write out the values of Field1 and Field2 are they what you expect them to be, (in other words, are you sure you are getting the correct info from your table)?

    If so show us the code you are using to effect the compare. strcmp() returns 0, (i.e. FALSE), if the strings are the same, so...
    Code:
    if (strcmp(Field1, Field2))
    {
        OutofRange = 1;
    }
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    10
    Im not sure how to write out the values of the fields to verify the info since I am such a newbie to C++.

    Here is the code specific to the compare:


    if(outofrange) continue;
    outofrange=0;
    for(i=0;i<numsc;i++) {
    if (strcmp(sc[i].birace1[],fam.accept_birace1[])) outofrange=1;
    }

    Thanks,

    M

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Code:
    cout << "sc = " << sc[i].birace1 << endl;
    cout << "fam = " << fam.accept_birace1 << endl;
    That will write the values out, (assuming you are in a console, and have the <iostream> header included and a using namespace std; line).

    And loose the empty square brackets in the strcmp() they are not needed.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    10

    Unhappy

    Excuse my ignorance but I do not know what being in a console means.

    It does not appear that iostream.h is included.

    I also do not know what using namespace std; means.

    Sorry so dumb!

    Thanks for being patient,

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> what being in a console means.

    Basically, are you in text mode? Is your programs interface simple white monospaced characters on a black background? This is typical of a console, the alternative is you could be in a graphical user interface, in which case basic I/O routines like cout won't work.

    To use cout, put this at the top of your program, with the other includes...
    Code:
    #include <iostream>
    using namespace std;
    ... note <iostream> not <iostream.h>, let's start things right!

    Do your other screen interactions use something like printf()?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    10
    The code I am working on is a COM object backend to a website that unfortunatly I inherited and have been tasked with making changes to non-gratis.

    So I am not in a console.
    I do see some refrences to sprintf within the code.

    Thanks again for your input.

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> So I am not in a console.

    Okay, so forget cout and printf()! sprintf() is not the same, printf() write to the screen in a console, sprintf() write to a string.

    What you could do is pop the strings up in a message box, but that will get really tedious if you have a lot of records. It might be worth trying just to see if you have anything remotely sensible in your variables. Just as an experiment, put this in your loop...
    Code:
    MessageBox(NULL,
               sc[i].birace1,
               "sc contains",
               MB_OK);
    MessageBox(NULL,
               fam.accept_birace1,
               "fam contains",
               MB_OK);
    ... and see if anything recognisable happens
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    10
    I tried adding the message box code to the loop, but unfortunately my COM object just stalled out when it reached this section of the code, and nothing else happend.

    Good idea though!

    Thanks,
    M

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Can you set a break point in the loop and check the values with the debugger?

    Unless we can find a way of getting some info out of your program, we're not going to be able to solve this remotely.

    *** EDIT ***

    After thought. The MessageBox() function will stop at the point you've inserted it until the "OK" button is pressed, the message box isn't behind your window is it?. I didn't want to get into this, but instead of NULL as the first parameter, if you have the handle of your active window, put that there, that should pop it up in front of that window.

    You could also try writing the values to a file and reading it after the program completes.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  12. #12
    Registered User
    Join Date
    Jul 2003
    Posts
    10
    Since my code is a COM object for a website I have to test it through my browser. Given this, where might one expect to see the message box popup?

    On the client browsing the site, or on the webserver itself?

    Thanks for all of your help.

    M

  13. #13
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    On the machine that has the COM object on it. Let's try something else, have a look at this little program, it outputs lines to a file called OutFile.tmp which should be created in the same directory as your COM program, (or you can always search for it!).
    Code:
    #include <fstream>
    using namespace std;
    
    int main()
    {
        int i;
        ofstream OutFile("OutFile.tmp");
        for (i=0; i<10; i++)
        {
            OutFile << "Hello There" i = " << i << endl;
        }
        OutFile.close();
        return 0;
    }
    Include the header and namespace directive as for cout, (note fstream not iostream), the line ofstream OutFile("OutFile.tmp") actually opens the output file for writing. The OutFile << line is inserting lines into the file, I have just output i, but you should put your strings in there as well, the OutFile.close() closes the file.

    See if you can get something out of it that way. We really must find out where the problem is. If we cannot check the contents of the strings are correct, there is no point trying to fix the compare, because we are comparing random stuff.

    (I'll try to get back later, I'm going out for a while - otherwise it'll be tomorrow now - getting late!).
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  14. #14
    Registered User
    Join Date
    Jul 2003
    Posts
    10
    Thanks I will give it a try and touch base with you tomorrow.

    Thanks again for all of your help, have a great day/night!

    M

  15. #15
    Registered User
    Join Date
    Jul 2003
    Posts
    10
    Hello,
    Sorry I have not responded lately, I am just cathing up from the July 4th holiday.

    I was not able to get anywhere with the output to file, because the additional code I added for this keeps my dll from compiling correctly.

    M

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing values
    By ICool in forum C Programming
    Replies: 8
    Last Post: 09-24-2007, 08:49 AM
  2. adding ASCII values
    By watshamacalit in forum C Programming
    Replies: 1
    Last Post: 12-26-2002, 07:16 PM
  3. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 10:59 PM
  4. linked lists problem
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-17-2002, 10:55 AM
  5. Comparing char values in an array using 'if'
    By Judy_528 in forum C++ Programming
    Replies: 18
    Last Post: 04-25-2002, 07:52 AM