Hey guys, i'm running into trouble attempting to validate an user input string.
Basically they put in a variable length string of letters (a-z) and/or numbers (0-9). I'm attempting to go through the string and check if the inputs is valid (using isalnum(stringname[i])) but it will crash if it has multiple errors, or if there are invalid characters followed by valid ones. Also i was try to get it to where if it finds an error, it will prompt for another input string until the user enters a valid string...
1 2 3 4 5 6 7 8 9 10 11 12
string plaintext;
for (c=0;c<plaintext.size();c++)
{
if (isalnum(plaintext[c])
{
}
else
{
cout <<"invalid character found in plaintext; Returning to Menu\n\n";
break;;
}
}
#include <iostream>
#include <string>
#include <cctype>
int main()
{
std::string input ;
while( input.empty() )
{
std::cout << "please type in a string consisting of only alphanumeric characters\n" ;
std::cin >> input ;
// validate
for( char c : input ) // for each character in input
{
if( !std::isalnum(c) ) // if it is not alphanumeric
{
std::cout << "invalid character '" << c << "' found in input '"
<< input << "'. please try again\n" ;
input.clear() ; // make input empty so that we go through the loop again
break ;
}
}
}
std::cout << "validated input is: '" << input << "'\n" ;
}