Thread: Problem with function.

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    21

    Problem with function.

    The function should return true if the string only contains hexadecimal chars. However it keeps returning true not matter what the input is,
    Code:
    inline bool hex(string String, int Lenght)
    {
        // Integer that will have a value of 1 if a number is found.
        int FoundHex = 1;
        // The set of special characters
        string Hex = "abcdefABCDEF012345689";
    
        // When the counter is less or equals to the lenght of the string and no special character has been found go to the next string index.
        for(int counter = 0; counter <= Lenght && FoundHex != 1; counter++)
        {
            // Integer to hold the lenght of the string containing our specials.
            int HexSize = Hex.size();
    
            // Loop through all our special characters.
            for( int Scounter = 0; Scounter < HexSize; Scounter++)
            {
                // When the current special character does not matches the location we are pointing to of our string set FoundSpecial to 0.
                if(String[counter] != Hex[Scounter])
                {
                    FoundHex = 0;
                }
            }
        }
    
        // Return result.
        if(FoundHex == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    FoundHex should be a bool, not an int. Once you detect a non-hex character, set it to false, then break from the loop.

    By the way, counter <= Lenght should be counter < Lenght. Actually, Lenght should be Length.

    Also, take a look at some of the member functions of std::string. They can help you simplify your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    Thanks. I overlooked a function. Got it to work like this.
    Code:
    inline bool hex(string String, int Lenght)
    {
        // Go through the string
        for(int counter = 0; counter < Lenght; counter++)
        {
           // See if at the current location in the string (pointed to by the counter) there is NOT a hex  number,
            if(!isxdigit(String[counter]))
            {
                 // If NOT a hex number return false.
                return false;
            }
        }
        // Otherwise return true.
        return true;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good to hear. Based on your earlier idea, this is another way of doing it:
    Code:
    inline bool isHex(const string& str)
    {
        return str.find_first_not_of("abcdefABCDEF012345689") == string::npos;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    I will bookmark this page. I do not fully understand what you did there but it seems like a good way to optimize my code so I will look into it later when I understand C++ a bit better.

    Just coding my first program ever.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL DC Buffer Renders slow
    By Lane the Great in forum Game Programming
    Replies: 10
    Last Post: 01-07-2011, 07:52 PM
  2. Tough problem with function pointers
    By Montejo in forum C Programming
    Replies: 10
    Last Post: 12-22-2009, 01:17 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM