Thread: Can't figure out strcmp error

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    52

    Can't figure out strcmp error

    I've searched the board and the books and can not figure out why I can not compare the value of two string objects in my 2D array. Here's the code...

    Code:
    if(strcmp(*p2EmpInfo[a][e], *p2EmpInfo[a-1][e]) > 0)
    			{
    				string temp;
    				*p2EmpInfo[a][e] = temp;
    				*p2EmpInfo[a-1][e] = *p2EmpInfo[a][e];
    				temp = *p2EmpInfo[a-1][e];
    			}
    I have tried to cast it as a char variable....used a str1<str2? type comparison and also chanted in Swahili while letting the blood of a 2 month old chicken onto a plate with the bones of a sparrow, all to no avail. Where am I going wrong? Is it the logic? Do I need a younger chicken?

    I can get the program to execute, but when it gets to the sorting section (I just kept it in main()...), it encounters an error and the program bails on me.
    MS VC++ 6.0

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you are using #include <string> and string objects in your code then you should just be able to use the boolean operators >, <, ==, etc to do comparisons, these operators are overloaded for this type of object. strcmp is for character arrays/pointers.
    "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
    Apr 2002
    Posts
    52
    That would explain the error messages, I have included the <string> header. Would including the <string.h> nullify the ability to use these operators?

    By the way, thank you for the prompt reply to my initial question.
    MS VC++ 6.0

  4. #4
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    If its a 2D array of single chars then you can use == without including stuff.
    Of you have made a 2D array of strings (Can you really do that?) then you just make it hold strings form the string class and use ==

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    52
    Yeah, you can use a 2d array for strings, but sorting it seems to be a pain in the rectum. The first position is more of an index, the second part is the name/social-security-number portion. It's easier to display it after the sort....Should I ever have success with it.
    MS VC++ 6.0

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Are you just trying to sort an array of strings? Are you familiar with any of this?:
    Code:
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        vector<string> array;
        vector<string>::iterator itArray;
        array.push_back( "Mike" );
        array.push_back( "Andrew" );
        array.push_back( "Smith" );
        array.push_back( "Barry" );
        array.push_back( "John" );
        array.push_back( "Larry" );
        cout << "Unsorted list:" << endl;
        for( itArray = array.begin(); itArray != array.end(); itArray++ )
            cout << *itArray << endl;
        sort( array.begin(), array.end() );
        cout << "Sorted list:" << endl;
        for( itArray = array.begin(); itArray != array.end(); itArray++ )
            cout << *itArray << endl;
        return 0;
    }
    On my computer this gives the following:

    Unsorted list:
    Mike
    Andrew
    Smith
    Barry
    John
    Larry
    Sorted list:
    Andrew
    Barry
    John
    Larry
    Mike
    Smith
    "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

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Why don't you do this

    I quote some of your code and I edit it

    Code:
     string x = *p2EmpInfo[a][e];
     string y = *p2EmpInfo[a-1][e]; 
     if(strcmp(x,y) > 0){
       /* string temp;
       *p2EmpInfo[a][e] = temp;
       *p2EmpInfo[a-1][e] = *p2EmpInfo[a][e];
       temp = *p2EmpInfo[a-1][e];
       */
      // ARE YOU SWAPING THEM ... IF TRUE YOU SHOULD DO THIS
     
      temp = x;
      x = y;
      y= temp ;
    
     } // this should work Else YOU HAVE A RPOBLEM WITH YOUR ARRAY
    C++
    The best

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    strcmp is for comparing char arrays. It is not compatible with class string!!! Look at the post by " hk_mp5kpdw". This is the correct way. (Unless of course you feel like overloading strcmp???)
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Lightbulb but he can overload the ctrcpm func

    Could he overload Strcomp function... ?
    C++
    The best

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    52
    Thanks a lot for all the help, I used the syntax used by Nano and it worked perfectly. The concept was where I was falling apart, but it's working perfectly now....Man, I'm feeling all warm and fuzzy inside.....
    MS VC++ 6.0

  11. #11
    Unregistered
    Guest
    try to get the string or chars out of the two dimensional array

    char *p, *q;

    p = *p2EmpInfo[a][e]; // some how
    q = *p2EmpInfo[a-1][e]; // some how

    and then do the following:

    if(strcmp(p, q) > 0)
    {
    string temp;
    p = temp;
    q = p
    temp = p;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM