Thread: What am I doing wrong here?

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

    What am I doing wrong here?

    What is wrong with these two snippets of code, they both return false no matter what???
    Code:
    bool LM(string Name)
    {
        string Control = Name.substr(5);
    
        if(Control == "aName")
        {
            return true;
        }
    
        return false;
    }
    Code:
    bool REALLY(string String, int Lenght)
    {
        // ASCII "x".
        char Symbol = 120;
        // Go through the string.
        for(int counter = 0; counter > Lenght; counter++)
        {
            // Check if the location pointed to equals "x".
            if(String[counter] == Symbol)
            {
                return true;
            }
        }
    
        return false;
    }
    Last edited by Crux; 02-03-2011 at 02:50 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    One) Name.substr(5) gets everything in Name except the first five characters.

    Two) Your for loop will never run.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    Quote Originally Posted by tabstop View Post
    One) Name.substr(5) gets everything in Name except the first five characters.

    Two) Your for loop will never run.
    How would I get the first 5 characters then?

    Did I had to use < instead of > for the second problem?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Name.substr(0,5) gives you the five characters starting at position zero. And yes you will need <.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If 120 is x and you want your code to be readable then why on earth not just do:
    Code:
    char Symbol = 'x';
    Self-documenting code FTW!

    By the way, you misspelt "Length"
    Last edited by iMalc; 02-04-2011 at 12:40 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM