// Name:  McCabe, Micheal H.
// Date:  March 18, 2009
// Project:   Simple_Calculator_V5
// Filename:  ReadInputFile.cpp

// File Edit History:
//
// Ouch!  This is not the file I wanted to use -- this is the original one I
// submitted on 03/17.  It does NOT handle input correctly according to Prof.
// Schuyler's plan.  Needs to be revised ASAP.
//

#include "calc_headers.h"

int ReadInputFile(int start, float data[])
{
    int records=0;
    int count=0;
    float element;
    int return_value=0;
    string inputfilename;
    cin.ignore(100, '\n');
    cout << "Please enter a filename to open:  ";
    cin >> inputfilename;
    if (cin)
       {
       cout << "Thank you.  Attempting to open file " << inputfilename << endl;  
       ifstream inputfile;
       inputfile.open(inputfilename.c_str());
       if (inputfile.is_open())
          {
          inputfile >> records;
          cout << "Records: " << records << endl;
          if (inputfile.fail()) 
             {
             cout << "Error:  Data Error in the file. \n";
             return_value=0;
             }
          else
              {
              while (count<=records and !inputfile.eof())
                 {
                 inputfile >> element;
                 if (inputfile.good())
                    {
                    data[start+count]=element;
                    count++;
                    }
                 }
              if (count<records)
                 {
                 cout << "Error: Unexpected end of file.  (Insufficient data)\n";
                 return_value=0;
                 }
              else
                  {
                  data[0]=count;
                  return_value=count;
                  }
                  }
                  }
          else
              {
              cout << "Error:  Could not open file. \n";
              return_value=0;
              }
       inputfile.close();
       }
    else
        {
        cout << "Filename input not recognized.  Try Again.\n";
        }
    return return_value;      
}
