C++/Hello PLEASE HELP.
I wanted to know if you could give me the code using strings for a name. What I mean is for example:
Enter name: Cody Tabei 222
Enter name: cODY tABEi
(Asks the name again until you enter a name with no numbers but just alphabets)
"Hello Mr. Cody Tabei"
the standard C library function isdigit(int) in the header <ctype.h> returns non-zero (true) if its argument is the character code for a digit ie. in the range '0' and '9', zero (false) otherwise.
all we have to do is pass each char in the string to ::isdigit, if it returns true for any of the inputs, there is a literal character for a digit in the string.
the easy way to do this in C++ is to use the algorithm std::find_if (in header <algorithm>) to check if any element in a sequence of elements meets a particular condition.
std::find_if takes three arguments: an iterator to the start of the sequence, an iterator to the end (one past the last) of the sequence, and a unary predicate which specifies the test to be made (in our case ::isupper).
the find_if() function searches for the first element between start and end for which the condition holds (the unary predicate returns true).
if such an element is found, an iterator to that element is returned.
if no such element exists in the sequence, the iterator end is returned.
to check for punctuation characters, use ::ispunct instead of ::isdigit.
here is an example of using these functions:
#include <algorithm> // *** required for std::find_if
std::string name ;
std::getline( std::cin, name ) ;
if( std::find_if( name.begin(), name.end(), ::isdigit ) != name.end() )
std::cout << '"' << name << '"' << " contains a digit\n" ;
if( std::find_if( name.begin(), name.end(), ::ispunct ) != name.end() )
std::cout << '"' << name << '"' << " contains puctuation\n" ;
you will have to wrie a loop which
a. accepts name as a string.
b. check if name contains digits or punctuation.
c. if yes, print out an error message and repeat loop from a.
d. if no, the name is ok, print it out and we are done