Problem with function.

This is a discussion on Problem with function. within the C++ Programming forums, part of the General Programming Boards category; The function should return true if the string only contains hexadecimal chars. However it keeps returning true not matter what ...

  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
    21,647
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,647
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 06:52 PM
  2. Tough problem with function pointers
    By Montejo in forum C Programming
    Replies: 10
    Last Post: 12-22-2009, 12: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

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