huge string-pointer problem

This is a discussion on huge string-pointer problem within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a big problem with pointers and strings -- it seems that I cannot compare string with another ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    24

    Question huge string-pointer problem

    Hello,
    I have a big problem with pointers and strings -- it seems that I cannot compare string with another string. In the code snippet below, i am trying to compare normal. with whatever value is in the word variable at the moment. If there is a "normal." then the script ouputs 0, if not, then an 1.

    The problem is that where the script should output 0, it outputs 1. And where there it should output 1, it outputs 1 (no problem here).

    The code deals with the input of many lines, each line having 42 fields separated from each other by comma. The code finds the 42nd field which contains either normal. value or any other word with dot at the end.

    What should I do? Please help ASAP: please answer on the same day this message was posted.

    Code:
            for(a=0; !infile.eof();){
    // read in one line
                    char *p;
                    char word[20] = "";
                    infile.getline(buff,200);
                    p = strtok(buff,",");
                    for(int i=0; i<42; i++)
                    {
                            if(i==41)
                            {
                                    strcpy(word,p);
    
                                    if (word=="normal.")
                                    {
                                            cout<<"0";
                                            outfile<<"0";
                                    }
                                    else
                                    {
                                          cout<<"1";
                                          outfile<<"1";
                                    }
                            }
    
                            p = strtok(NULL,",");
                    }
                    cout<<endl;
                    outfile << endl;
                    a++;
            }
    Dmitry Kashlev

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    You need to use to use the strcmp function to compare character strings in the manner you are attempting. You can do it the way you have it in your sample if you use string objects as variables instead of character arrays.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    24

    Thumbs up thanks!

    thanks alot!
    Dmitry Kashlev

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Just wanted to elaborate a little bit on why you can't use the == operator to compare null-terminated char*s.

    What's commonly known as a string is actually a char* which is a pointer to an array of chars (of course with a null byte to end the string).
    When you do something like:
    char *a = "aaa";
    char *b = "bbb";

    if (a == b) ...

    you are saying "if the address that a points to is equal to the address that b points to..." which of course is not what you want to ask.

    what the strcmp() function does is step through each character of both arrays and does a one-to-one comparison until they are determined as not equal or until they are

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with passing back pointer pointed to string
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-21-2007, 06:55 AM
  2. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21