title: C++ ofstream not writing to output file?
body: <p>The code is supposed to count the number of a, b, c, d, e, and f characters in the input text file and print the output into a second text file. When I run the code, it creates the output file but doesn't write anything into it.</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;fstream&gt;
  #include&lt;cmath&gt;
  using namespace std;
  
  
  int main(){
  
  // establish counters for the number of each character
  char x;
  int acount=0;
  int bcount=0;
  int ccount=0;
  int dcount=0;
  int ecount=0;
  int fcount=0;
  
   ifstream iFile("plato.txt"); //define &amp; open files
   ofstream oFile("statistics.txt");
  
   if(!iFile){
    cout&lt;&lt;"The file could not be opened.";
    exit(1);
   }
  
   if(!oFile){
    cout&lt;&lt;"The file could not be opened.";
    exit(1);
   }
  
   iFile&gt;&gt;x;
  
   while(!iFile.eof()){
    if(x=='a'||x=='A'){
     acount++;
    }
    else if(x=='b'||x=='B'){
     bcount++;
    }
    else if(x=='c'||x=='C'){
     ccount++;
    }
    else if(x=='d'||x=='D'){
     dcount++;
    }
    else if(x=='d'||x=='D'){
     dcount++;
    }
    else if(x=='f'||x=='F'){
     fcount++;
    }
  }
  
  
  
      oFile&lt;&lt;"Number of a/A characters: "&lt;&lt;acount; //write number of characters into statistics file
      oFile&lt;&lt;"\nNumber of b/B characters: "&lt;&lt;bcount;
      oFile&lt;&lt;"\nNumber of c/C characters: "&lt;&lt;ccount;
      oFile&lt;&lt;"\nNumber of d/D characters: "&lt;&lt;dcount;
      oFile&lt;&lt;"\nNumber of e/E characters: "&lt;&lt;ecount;
      oFile&lt;&lt;"\nNumber of f/F characters: "&lt;&lt;fcount;
  
  
  //close files
   iFile.close();
   oFile.close();
  }
  </code></pre>