cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
Counting words
Counting words
Nov 3, 2010 at 7:58pm UTC
slypenguinz
(6)
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();
Nov 3, 2010 at 8:38pm UTC
Computergeek01
(5613)
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.