hi everyone :)
what i'm trying to do is to check if word in a string has numbers in it
for example:
if i enter string of one word "he11o"
i want to show error message that says
"One or more characters are numeric! Please re-enter the word!"
and if every character is alphabetic i want to let program continue working(this is just a small part of my overall program.) i have no idea how to check each character, please help.
i'm working in DevC++ if that matters
my overall program is about converting NOUN words to PLURALS
i have everything done! this is the only part i cant find solution to :(

Recommended Answers

All 3 Replies

In C++, you can access each letter of a string (you are using proper C++ string objects, yes? Not a char-pointer) as a char using the [] operator

string x;
cin >> x;
char letter = x[0]; // letter is now the first letter of the string

You might then find this function useful:

I have tried this but still not working, it keeps looping even if there is no numbers in the word.
i've tried word (hello) and it's not working.

string word;
  int i = 0;
  while (word[i])
        if (isalnum(word[i]))
              cout<<"One or more characters are not alphabetic, please enter another word! \n";

You've got if (isalnum(word[i])), which basically means if word[i] is either a letter of the alphabet or numeric, you're failing it.
What you want to do is contained in Moschops reply. Check whether isalpha(word[i]) is false.
Also, when you get the new word on failure, you're not resetting i and you'd also want to use continue; after that so that you skip the i++ at line 12.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.