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;
}
Checking for punct and digits
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:
std::string is key.
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:
#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!"
The parameters of isdigit and ispunct are single characters, not strings. You need to check each character in the string.
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?
Quote:Original post by Punkittengumpy showed one way to do it. What John is saying though is that these functions take characters as arguments, that is: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?
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).
I was new once, and didn't want to learn boost or iostream or namespaces just yet.
Go from this:
to this:
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().
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().
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement