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

Dec 21, 2008 at 12:31pm
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;
Dec 21, 2008 at 1:11pm
You've not initialised ch. Somewhere in the loop you need
ch = first[i];
Dec 21, 2008 at 2:47pm
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 Dec 21, 2008 at 2:50pm
Dec 21, 2008 at 2:51pm
That'll do.
Dec 21, 2008 at 3:00pm
thank you kbw.
Dec 21, 2008 at 3:59pm
You aren't using ch...
Topic archived. No new replies allowed.