Thread: limitation

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    58

    limitation

    I am looking for a function that limit my range? For example, I don't want the user to use two symbols like "^" and "$". So, Instead of using multiples of functions for each element that I do not want the use to use, it will be one function that I put all the odd elements in it. I ddon't know if I make sense to you guys or not..

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    If the symbols are single ascii characters you could put them in a string and use string.find() to check if the user input a wrong character.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you want your function to do, exactly? Do you just want to be able to call ValidInput(string_name) and have it return true/false? In this case, you'd still have to "hard code" in all your characters that are invalid into the function, but if they change you'll only have to change one thing. That's the most obvious thing I can think of, based on the code you've been posting.

    I suppose you'd be able to write something like this pseudocode:
    Code:
    valid = true;
    for all bad characters
        if bad character found
            valid = false
    return valid
    Your "for loop" would probably be an iterator over a string.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    yeah thanks

    Yeah. I am using this function string.find() in some parts of my program.

    My point is: I can't use this function this way found12 = y.find('&#37;,^');

    the proper way to use this function will be:

    found1 = y.find('%');
    found2 = y.find('^');


    And you know I am not going to limit only these symbols because my program will limit all the small letters, some capital letters, and all symbols. So, you can imagine how waste of time that would be and how big my code that would be..!


    *** and once my program detects any of these limitations, I will set a flag for another condition
    Last edited by aama100; 02-04-2008 at 08:51 PM.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    if y is your string with characters you don't want the user to use, you just need to do something like:

    Code:
    if (y.find(user_input_char) != std::string::npos)
        is_valid = false;
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by aama100 View Post
    yeah thanks

    Yeah. I am using this function string.find() in some parts of my program.

    My point is: I can't use this function this way found12 = y.find('&#37;,^');

    the proper way to use this function will be:

    found1 = y.find('%');
    found2 = y.find('^');


    And you know I am not going to limit only these symbols because my program will limit all the small letters, some capital letters, and all symbols. So, you can imagine how waste of time that would be and how big my code that would be..!
    This sounds like a job for isupper, which you will have once you #include <cctype>.

    And once you've gotten through that, you could do something like this completely untested bit of code which is probably wrong in several ways:
    Code:
    std::string bad_string = "EFRMNTZ"; //or whatever your bad letters are;
    std::string::iterator it;
    for (it = bad_string.first(); it != bad_string.last(); it++) {
        if (y.find(*it) != std::string::npos) {
            is_valid = false;
        }
    }
    Last edited by tabstop; 02-04-2008 at 08:59 PM. Reason: lost a namespace

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    why use iterators and loops when a simple algorithm like find() suffices on this case? Algorithms were created exactly to simplify many common tasks and consequently reduce code complexity and chances for mistakes.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Mario F. View Post
    why use iterators and loops when a simple algorithm like find() suffices on this case? Algorithms were created exactly to simplify many common tasks and consequently reduce code complexity and chances for mistakes.
    Because find will only find the exact match of the entire string? The way you have it set up, he has to loop through each input character to find it in the string of bad characters; the way I have it set up, he has to loop through each "bad" character to find it in the string of input. Either way, there's a loop. Edit to add: IIRC, the code is 16 characters long, but he had less than 16 "bad" characters, which is why I went my way instead of your way.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yes. It's possible.

    The op didn't clarify when the check is to me made. I'm assuming he's checking every key press. But chances are he's not if he's using cin.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    find_first_of may be the STL algorithm of choice here.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory/file handle limitation?
    By George2 in forum Linux Programming
    Replies: 9
    Last Post: 09-21-2006, 07:43 AM
  2. Linked List Size Limitation?
    By j0seph in forum C Programming
    Replies: 6
    Last Post: 06-27-2005, 09:36 PM
  3. SMS limitation ?
    By khpuce in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-12-2005, 05:12 PM
  4. GDI+ Limitation?
    By PsychoBrat in forum Windows Programming
    Replies: 4
    Last Post: 12-07-2003, 06:11 PM
  5. Replies: 0
    Last Post: 09-19-2003, 11:21 AM