/********************************************************/
// R. A. Hillyard
// wordCount2.cpp
// March 2001
//
// See wordCount1.cpp for comments
//
// This version reads floating point numbers as a single word
/********************************************************/

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

using namespace std;

bool openFiles(ifstream& inData, ofstream& outData);
//pre conditions: inData and outData have been declared as file streams
//post condition: inData and outData will have been opened and function returns true
//                if a file fails to open - return false


int main()
  {
  char next;        //next character in the file
  int wordCount=0;  //keep track of count
  double temp;      //need a temp variable when reading numbers
  
  ifstream inData;  //declare file streams
  ofstream outData;

  //open files for reading - check for failure
  if( !openFiles(inData, outData))
    {
    exit(1);  
    }

  //Read data from file a character at a time
  inData.get(next);  
  while(!inData.eof())    //stop at end of file
    {
    //skip any stopping cases
    if(isspace(next) || next == ',' || next == '.')
      {inData.get(next);}
      
    //get next word - read characters until a stopping case or EOF.
    else
      {
      if(isdigit(next))          //check for number - read as float
        {
        inData.putback(next);    //put digit back
        inData >> temp;          //read as a double - will catch all legal numbers (ints, floats, doubles)
        outData << temp << endl; //echo number to output file
        wordCount++;             //increment count
        inData.get(next);        //get next character
        }
      
      else                       //read a word and echo to file one per line
        {
        do{
          outData.put(next);  //echo character to a file
          inData.get(next);   //get next char
          }while( !(isspace(next) || next == ',' || next == '.') && !inData.eof());
        
        wordCount++;         //update count
        outData.put('\n');   //print new line to file
        }
      }
    }//end while
   
  //report results 
  outData << "there were " << wordCount << " words in the file\n";
  }//end main

/*************************************************************/
/*************************************************************/
bool openFiles(ifstream& inData, ofstream& outData)
  {
  inData.open("hello.txt");
  if(inData.fail())
    {
    cout << "Failed to open input file\n";
    return false;  
    }
    
  outData.open("helloCount2.txt");
  if(outData.fail())
    {
    cout << "Failed to open output file\n";
    return false;  
    }
  return true;  
  }//end openFiles
  /*************************************************************/