/* qualitycontrol1.cpp shows the distribution of component failure rates
 * that are stored in an input file.
 *
 *  Input(file):    a sequence of failure rates
 *  Output(screen): the number and percentage of occurrences
 *                    of each failure rate
 *************************************************************************/

#include <iostream.h>                       // cout, <<, fixed, showpoint
#include <fstream.h>                        // ifstream, >>, eof(), close()
#include <iomanip.h>                        // setprecision()
#include <string>                           // string, getline()

void InteractiveOpen(ifstream & theIFStream);

const int CAPACITY = 6;                     // # of array elements

int main()
{
   cout << "Quality Control: Component Failure Frequency Distribution.\n\n";

   ifstream inStream;
   InteractiveOpen(inStream);
   int count[CAPACITY] = {0},               // array of counters
      numFailures,                          // input variable
      numCircuitBoards = 0;                 // # of input values

   for (;;)                                 // loop:
   {
      inStream >> numFailures;              //   read input value
      if (inStream.eof()) break;            //   if done, quit
      count[numFailures]++;                 //   increment its counter
      numCircuitBoards++;                   //   one more input value
    }                                        // end loop
   inStream.close();                        // close the stream

   cout << "\nOut of " << numCircuitBoards << " circuit boards:\n"
        << setprecision(1) << setiosflags(ios::fixed | ios::showpoint);

   for (int i = 0; i < CAPACITY; i++)       // output counters
      cout << count[i] << " had " << i
           << " failed components ("        // and percentages
           << double(count[i]) / numCircuitBoards * 100
           << "%)" << endl;
   return 0;
}

void InteractiveOpen(ifstream & theIFStream)
{
   cout << "Enter the name of the input file: ";
   string inputFileName;
   getline(cin, inputFileName);

   theIFStream.open(inputFileName.data());

   if (!theIFStream.is_open())
   {
      cerr << "\n***InteractiveOpen(): unable to open "
           << inputFileName << endl;
      exit(1);
   }
}


