How do you make one of them doohickies.

Hello, so my question is how do you make an string input that will only allow a specific format. Like a username thing. It won't allow spaces or symbols or numbers stuff like that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main ()
{
    string sUser;
    while (1)
    {
        cout << "Please do not use any symbols or spaces." << endl;
        cout << "Username: ";
        cin >> sUser;
        if (!bValidUser(sUser))
            cout << "Invalid Format";
        else
            break;
    }

    cout << endl << "Your username is:  " << sUser << endl;
    return 0;
}

bool bValidUser(string a)
{
     for (int iii = 26; iii > 0; ++iii)
          if (!a == (int)'a'+ 25 || !a == (int)'A'+25)
               return false;
     return true;
}


I think something like this would work for letters but I'm not sure what to do about spaces.
Last edited on
so you only want to allow letters? Try something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <cctype>

const std::string input( void )
{
    std::string user_name( "" );
    std::cout << "Please enter a username\n> " << std::flush;
    std::getline( std::cin , user_name );
    return( user_name );
}

const bool valid_name( std::string &name )
{
    for( const auto &it : name ) //c++11
    //for( auto it( name.begin() ); it != name.end(); ++it ); c++11
   //for( std::string::iterator it = name.begin(); it != name.end(); ++it ) c++98
    {
        if( !isalpha( it ) ) return( false ); //dereference for second or third loop
    }
    return( true );
}

int main()
{
    std::string user_name( "" );
    while( user_name = input() && !valid_name( user_name ) ) std::cout << "invalid username!" << std::endl;
}


**Did not test code but sounds like it will work

http://www.cplusplus.com/reference/regex/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <regex>
#include <string>

bool isValidUserName(const std::string& name)
{
    const unsigned minLength = 4 ;
    std::regex allowable("[a-zA-Z][_[:alnum:]]*") ;

    return name.length() >= minLength && std::regex_match(name, allowable) ;
}

int main ()
{
    std::string userName;
 
    bool isInvalidName = true ;

    while ( isInvalidName )
    {
        std::cout << "Username> " ;
        std::getline(std::cin, userName) ;

        if ( (isInvalidName = !isValidUserName(userName)) )
        {
            std::cout << "User name must be at least 4 letters long. The first letter must be a letter\n"
                         "of the alphabet.  Subsequent characters may be a letter of the alphabet, a\n"
                         "number or an underscore.\n\n" ;
        }
    }

    std::cout << "Your user name is \"" << userName << "\"\n" ;
}
Last edited on
@giblit It didn't work you used = instead of ==, then after it compiled it just closed.

@cire Cool, I didn't know there was regular expressions. I'm getting this error after I enter a name.

terminate called after throwing an instance of 'std::regex+error'
what(): regex_error


I did a google search it says that my compiler doesn't support regex. What compiler do you use? I'm using MinGW GCC 4.7.1.

I think I know how I can do this now but can anyone tell me why I am getting this error and how I can fix it?

Thanks for your replies.
Umm..no I was not comparing the two items. I was setting it equal to it. As in getting a new string if the old one was invalid...and I suppose there are probably better ways to do it than the while loop I did but I was giving you an idea not a "please copy/paste me then tell me if the code works or not" :p and yeah what did you expect it to do after compiling display hello world? It asks for an input and if the input is not all letters then it displays an error message and asks for the user to enter another username.

Also I don't know what I was thinking with that while loop and the string before. Try this instead for lines 24 and 25
while( !valid_name( input() ) ) std::cout << "invalid username!" << std::endl;

If you want to set the valid "input" to a string then maybe do this
1
2
std::string user_name( "" );
    while( !valid_name( user_name = input() ) ) std::cout << "invalid username!" << std::endl;

but I wouldn't suggest it because I think it looks very ill-formed to me. Although using a function as a parameter prob isn't the best idea anyways so people probably will get mad at me suggestions the code about 5 lines earlier...

**edit
btw you just need to enable c++11 go to the compiler settings and then flags and put this
-std=c++11

Last edited on
GCC's regex implementation was still quite buggy for versions 4.7.x. I don't know if that changed for 4.8. If you're stuck with a buggy implementation, you can always use boost.

http://www.boost.org/
@giblit Oh my mistake, I wasn't aware that you could do that so I assumed it was an error you didn't catch since you didn't test it.

Anyways I'm marking this as solved thanks for the help. ^^
Topic archived. No new replies allowed.