what is wrong with this input validation loop. Once it goes in the if statement it doesent come out.
1 2 3 4 5 6 7 8 9 10 11
int t = 0;
while(chWord[t] != '\0')
{
if(!isalpha(chWord[t]))
{
cout << "Error, a valid word should only contain letters.\n";
cout << "Enter a word: ";
cin >> userWord;
}
t++;
}
would it be simpler to do this input validation if the word was a string object?
Well, it's your code. A char array is fine, but with c and c++ is is advisable to initialise your variables first. When you declare a char array either on the stack or heap (i.e. new char[...] ) you must add sufficient room for the null terminating character and then you won't go far wrong.
If I declare a char array on the stack I normally do: