Try something like this, it will get you a lot closer to your goal I think...
cpp
#include <stdio.h>
#include <ctype.h>
#define EOL '\n'
int main()
{
int linecount = 1,wordcount = 1,character = 1;
int ch;
// Prompt message to enter a sentence
printf("Enter your sentence and end it with a '*'...\n");
// Loop through each character and compare it to terminator char
while ((ch = getchar()) != '*') {
if ((ch == ' ') || (ch == '\n'))
wordcount++; //increment of words
if (ch=='\n')
linecount++; //increment of line
if (isalpha(ch)) //increment of char
character++;
}
// Print our results
printf("\n\n\t\tWORDS LINE CHARACTER \n ");
printf("\t\t%d \t%d \t%d", wordcount,linecount,character); //giving output
printf("\nGood Bye");
return 0;
}
The idea is that we just keep a tally of the characters as you go along. Notice that we store the character, compare it, increment our counters and keep going until we hit the asterisk. Then we go ahead and print out the counters.
It cleans out a bunch of unused variables, shortens up some of your lines, saves you on having to run other loops etc.
Give it a whirl and see if it can be something you would use.
Enjoy!
"At DIC we be char counting, card counting, and sesame street snufflupagus counting code ninjas... sesame street's count has nothing on us!"
This post has been edited by Martyr2: 29 Aug, 2008 - 10:07 PM