Counting words

I have to count characters, spaces, and words. how can i do this, my codes keeps adding words. thank you

int main()
{
ifstream in_file;
char ch, old;
int ch_cnt=0;
int sp_cnt=0;
int wrd_cnt=0;

in_file.open("C:\\Users\\Work\\Desktop\\red.txt");

if(in_file.fail() )
{
cout<<"Error in file\n";
getch ();
exit(1);
}

ch = in_file.get();
while( !in_file.eof() )

{
cout<<ch;
ch_cnt ++;

if( isspace(ch))
{
sp_cnt ++;

if( !isspace(old))

wrd_cnt ++;
}
ch=old;
ch = in_file.get();
}


in_file.close();
Shouldn't your second if(...) statement test to ch to see if it isn't a space? Acctually you should probably just use an else{...} here since it's really only one or the other assuming you aren't going to do something else with numbers.
Topic archived. No new replies allowed.