Am I using isalpha() function in c++ correctly?

Solution 1:

The main problem is that you use C-Style char[] for strings. In C++, we use the datatype std::string for strings. std::string is that superior compared to-Style strings, that it is hard to understand, why anybody wants still to use the old stuff.

As a consequence you have the problems that you are facing now. You are using a magic constant 80 for the definition of your char array. You also use the number 80 hardcoded in the loops, so that if you change the 80 in one place, you might forget it in other places. And run into trouble.

Then, next, and that is the root cause of your problems, what will happen, if you enter shorter names than 80 charachters.

Example: You enter 'Mike'. This are 4 letters and a trailing 0. So, overall 5 characters. After this 5 letters there will be random garbage at the remaining array positions after this 5 relevant letters.

But your loop runs always until 80. So, after having check the first few correct characters you will continue to check garbage. And then you get random results in your first loop.

If you want to fix that, then you should also use the C-Style strlen function, to get the length of the string. And then you should loop up to this value.

Something like:

int length = strlen(newName);
for (int i = 0; (i < length) and (i < 80); i++) {

This will solve your problem.

But again the recomendation: Please consider using std::string