using isalph(), isdigit() and ispunct()

Hi can anyone help? I'm trying to read strings from a file and using a function chartype() that uses isalph(), isdigit() and ispunct() to determine the character type. The function will print each letter of the word on a new line and next to it on the same line print a message "alphabetic character", "digit" or "punctuation". Please help! this is my code

void chartype(string first)
{
char ch;

for(int i = 0; i < first.length(); i++)
if(isalpha(ch))
textout<<first[i]<<" is an alphabetic character"<<endl;
else if(isdigit(ch))
textout<<first[i]<<" is a digit"<<endl;
else if(ispunct(ch))
textout<<first[i]<<" is a punctuation"<<endl;


return;
You've not initialised ch. Somewhere in the loop you need
ch = first[i];
Is this correct? It seems to print the correct info
void chartype(string first)
{
char ch;
int len;
len=first.length();

for(int i = 0; i < len; i++)
if(isalpha(first[i]))
textout<<first[i]<<" is an alphabetic character"<<endl;
else if(isdigit(first[i]))
textout<<first[i]<<" is a digit"<<endl;
else if(ispunct(first[i]))
textout<<first[i]<<" is a punctuation"<<endl;
return;
}
Last edited on
That'll do.
thank you kbw.
You aren't using ch...
Topic archived. No new replies allowed.