Jump to content
  • Advertisement
Sign in to follow this  
Punkitten

Checking for punct and digits

This topic is 4594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a simple program that prompts the user for thier name. I would like to run a check so where if they enter digits or punctuatuin it returns invalid. I've tried a few things like this which doesnt seem to work:
int main()
     {
       char name[32];
       cout << " Please enter your name.";
       cin >> name;
       while(isdigit(name) || ispunct(name))
     {
       cout << " Invalid please try again.";
     }
       cout << " Hello " << name;
     }

Share this post


Link to post
Share on other sites
Advertisement
std::string is key.

#include <cstdlib>
#include <iostream>
#include <string>

const std::string alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main( int argc, char* argv[] )
{
// use strings, not char arrays
std::string input;

// ask for user's name
std::cout << "Please enter your name.\n>";

// this will store the entire line entered by the user into the string
std::getline( std::cin, input );

// find the position of the first non-alpha character
std::string::size_type pos = input.find_first_not_of( alpha );

// print the first non-alpha character if there is one
if( pos != std::string::npos )
std::cout << "first illegal character: [" << input.at( pos ) << "]" << std::endl;

// print the name anyway
std::cout << "Hello, " << input << ".\n";

// portable "pause" method
std::cin.get();

return EXIT_SUCCESS;
}



if (input.find_first_not_of(alpha) != std::string::npos) then there are characters in the string that you don't want to allow.

edit: if (input.find_first_not_of(alpha) == std::string::npos) then you still need to see if the string is empty:

if( input.empty() ) // "i said enter your name!"

Share this post


Link to post
Share on other sites
The parameters of isdigit and ispunct are single characters, not strings. You need to check each character in the string.

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
The parameters of isdigit and ispunct are single characters, not strings. You need to check each character in the string.


I feel stupid. How do i do that lol?

Share this post


Link to post
Share on other sites
Quote:
Original post by Punkitten
Quote:
Original post by JohnBolton
The parameters of isdigit and ispunct are single characters, not strings. You need to check each character in the string.


I feel stupid. How do i do that lol?
gumpy showed one way to do it. What John is saying though is that these functions take characters as arguments, that is:
std::string s = "a1.";
assert(std::isdigit(s[1]));
assert(std::ispunct(s[2]));
From there you should have no trouble writing a loop to test the string for non-alpha characters.

If you want a slightly better or more elegant solution though, you might look into the std algorithms, or the boost string algo library; the latter in particular has functions which will do what you want in one line of code (the std library probably does also, but I can't think of the appropriate function off the top of my head).

Share this post


Link to post
Share on other sites
I was new once, and didn't want to learn boost or iostream or namespaces just yet.

Go from this:


int main()
{
char name[32];
cout << " Please enter your name.";
cin >> name;
while(isdigit(name) || ispunct(name))
{
cout << " Invalid please try again.";
}
cout << " Hello " << name;
}








to this:


int main()
{
char name[32];
bool invalid;

do
{
cout << " Please enter your name.\n";
cin >> name;

invalid = false;
for(int i=0; i<32; i++)
{
if(isdigit(name) || ispunct(name))
{
invalid = true;
cout << " Invalid please try again.\n";
break;
}
}
}
while(invalid);

cout << " Hello " << name;

return 0;
}








Make sure you understand that (and make sure it's right!) I used the "[ ]" to access one character within the string. The "break" gets you out of the for loop as soon as you see a problem.

EDIT: Um, I'll leave this as an exercise to the reader: You should actually find the length of the string entered and only check up to that many characters, instead of the 32 I put in the for loop. Use strlen().

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!