Thread: Problem implemeting strcmp

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    Problem implemeting strcmp

    I am trying to implement the strcmp function to be able to sort a list of businesses, but i keep getting an error on the line where it's supposed to be comparing them.

    Code:
    void business::Sort(vector<string>& list,string strcmp(const char *s1,const char *s2))
    {
       if(list.size()==1){return;}
       else
       {
          for(int x=0;x<list.size();x++)
          {
             for(int y=0;y<list.size()-1;y++)
             {
                double b=(strcmp(list[y],list[y+1])); // error always happens here no matter how i try and change it
                if(b==-1)
                {swap(list[y],list[y+1]);}
             }
          }
       }
    }
    
    int business::strcmp(const char *s1, const char *s2)
    {
    while (*s1==*s2)
    {
    if(*s1=='\0')
    return(0);
    s1++;
    s2++;
    }
    return(*s1-*s2);
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why do you need it? Why not just use std::string?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Looks like you're trying to pass a function pointer in to do the comparison. Yet the comparison function you want to use is already a member of the class. So, you can just call your strcmp in Sort(). Delete that parameter.

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    You have a number of problems here. First and biggest: You did not tell us *what* the error is.

    Second, a string is not a const char*. If you really want to use your own c style strcmp function, you can get the c-string representation of an std::string with the member function c_str().

    Why are you keeping the return value of strcmp as a double?

    Why are you explicitly comparing the return value to -1? What if strcmp returns -2 or -3?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM