Thread: Final project

  1. #1
    Registered User albireo's Avatar
    Join Date
    Mar 2014
    Posts
    30

    Final project

    I'm having an error when trying to use the strcmp function. Here's the jist of the code I'm working with:

    Code:
    class Seller
    {
    public:
      Seller();
      Seller( char [], char[], char [], double );
        
      void print();
      
      void setFirstName( char [] );
      void setLastName( char [] );
      void setID( char [] );
      void setSalesTotal( double );
      
      void increaseSalesTotal( double );
      char * getFirstName();
      char * getLastName();
      char * getID();
      
      double getSalesTotal();
    
    
    private:
      char firstName[20];
      char lastName[30];
      char sellerID[7];
      double sales;
    };
    
    
    
    in main:
    
        Seller sellerArray[30];
    
    methods:
    
    char* Seller::getID()
    {
        return sellerID;
    }
    
    
    
    
    void sortSellersByID(Seller sellerArray[], int size)
    {
        int i, j; // more variables then a structure variable. 
        Seller tmp; // well, this is more like a temporary variable in our structure... so that we could not have some weird compiler errors
        
        for (i=0; i<size; i++)
        {
    
    
            for (j = 0; j < size; j++)
            {
                
                if (strcmp(sellerArray[i].getID, sellerArray[j].getID) < 0)   
                {
                
                    tmp = sellerArray[i];
                    sellerArray[i] = sellerArray[j];
                    sellerArray[j] = tmp;
                } 
            }
        }
    }
    I'm getting a
    "[Error] cannot convert 'Seller::getID' from type 'char* (Seller:: )()' to type 'const char*'"
    on this line of code:
    "if (strcmp(sellerArray[i].getID, sellerArray[j].getID) < 0) "

    How do i have this method able to compare these values in my seller array? Granted, there is more to the code and things actually in the seller array at this point. Is there something I'm not seeing??

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    The compiler is telling you that you have mismatched types.

  3. #3
    Registered User albireo's Avatar
    Join Date
    Mar 2014
    Posts
    30
    Yeah, I was wondering how to fix it, what I was missing.

    Figured it out! Disregard this thread! in my stupidity, i missed parentheses after the calling function.

    fix:
    if (strcmp(sellerArray[i].getID(), sellerArray[j].getID()) < 0)

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why are you using C-strings instead of std::strings?

    And you really shouldn't be returning a pointer from the get?? functions unless they are const qualified. In your error message that "const" is very important!

    Jim

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Have you considered using std::sort and/or std::swap, apart from 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-19-2013, 11:01 PM
  2. Idea for final project
    By friedsandwich in forum General Discussions
    Replies: 55
    Last Post: 11-16-2012, 09:51 AM
  3. Must be final project time again....
    By VirtualAce in forum General Discussions
    Replies: 21
    Last Post: 05-19-2010, 07:04 PM
  4. Final year project
    By afisher in forum C# Programming
    Replies: 3
    Last Post: 07-04-2005, 08:15 AM
  5. Final year project
    By khpuce in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 10-10-2003, 07:04 AM