Flow Control Flushing Annoyance?

Hi folks!

I came upon this site and was glad to see such a wealth of info. Like many here, I am new to c++ (but not to programming) and experiencing a silly problem.

I'm writing a simple program to open a text file, print out the ASCII codes of the file, then do some simple statistics like... count the total number of charaters, numbers, whitespaces...

Anyway... I seem to be able to either 1 - print out the file contents as ascii OR 2 - do the statistical counting...It all seems to depend on if I jump to checkit() before or after my "cout<<x;"

I am pretty sure this is due to flow control or flushing...

I've tried putting cout.flush(); in a number of places...

Anyway, the solution SHOULD be simple... it's just evading me... A suggestion or link to relevant info would be the best!

Thanks in advance!

I should mention that I'm developing under cygwin using notepad++ as my editor.

-----------------THE CODE BEGINS----------------------

#include <fstream>
#include <iostream>
#include <cctype>

using namespace std;
int letters=0;
int numbers=0;
int whitespaces=0;
int totalchars=0;

void checkit( char c )
{
if(isalpha(c)){letters++;}
if(isdigit(c)){numbers++;}
if(isspace(c)){whitespaces++;}
totalchars++;
}

int main ( int argc, char *argv[] )
{
if ( argc != 2 )
cout<<"usage: "<< argv[0] <<" <filename>\n";
else {
ifstream the_file ( argv[1] );
if ( !the_file.is_open() )
cout<<"Could not open file\n";
else {
char x;
cout<<"\nPrinting ASCII code contents of "<< argv[1]<<endl<<endl;
while ( the_file.get ( x ) )
checkit(x); //if before cout<<x;... file no printy
cout<<x;
}
cout<<"\n\nCharacter Statistics:"<<endl;
cout<<dec<<++totalchars<<" characters"<<endl;
cout<<letters<<" letters"<<endl;
cout<<numbers<<" numbers"<<endl;
cout<<whitespaces<<" whitespaces"<<endl;
}
}

----------------------THE CODE ENDS-------------------
This loop is incorrect
1
2
3
while ( the_file.get ( x ) )
checkit(x); //if before cout<<x;... file no printy
cout<<x;

it should be
1
2
3
4
5
while ( the_file.get ( x ) )
{
    checkit(x); //now printy
    cout<<x;
}
Ah Ha! It's good to have another pair of eyes.

Did you ever know that you're my hero?

Thanks!
Topic archived. No new replies allowed.