Thread: traverse a string to look for certain chars

  1. #1
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20

    traverse a string to look for certain chars

    Hey people I am trying to do a validation type thing for the names of people. I prompt for the name and then validate it. I am using getline to store the string because some names contain spaces. i would like to check the string to make sure there are no unwanted chars like "@" and "," etc. so far i can traverse a string to find 1 character using name.find() i will paste the code below, but i cant seem to get that to find more than one char. how do i fix it?

    Code:
    int main ()
    {
    string name;
    int i = 0;
    int dot_appearances = 0;
    
    getline(cin, name, '\n');
    
    for(i = name.find(".", 0); i != string::npos; i = name.find(".", i))
    {
        dot_appearances++;
        i++;  
    }
    cout << dot_appearances;
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    So, are you wanting it to find a different symbol other than a period, or just that it won't recognize more than one period?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    find_first_of is a good alternative if you want to look for multiple characters at the same time.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I think the for loop would be better replaced with a while loop

    while( (i=name.find(".", i)) != string::npos ){ [...] }

    I think would do it. Just ++ i just before the end of the loop

  5. #5
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20
    well it already finds all the periods in the string. what i want it to do is find other stuff too. so for example if i prompt NAME: d@n T33ry Br0wn
    i want to be able to check that string and say its invalid based on the @, 3, 0 characters there. i also do not want to use that find method i pasted because i would have to type out an individual case for each char i want, there must be an easier way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use regular expressions.

    There are a couple of regular expression libraries you can use, eg
    http://www.boost.org/libs/regex/doc/syntax.html

    Basically, you describe a pattern which matches all possible valid inputs
    Eg.
    "\w+@" matches one or more word characters followed by a literal @
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> i would have to type out an individual case for each char i want, there must be an easier way
    That's what find_first_of allows you to do. Put all the chars into a single string, and you only need one loop.

  8. #8
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20
    thanks. that find_first_of looks promising. i think i can get that to do what i want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM