// File: inclass21.cpp
// This program reads in strings from a file into a vector, prints
// them out on the screen, and searches for a target
//
// Input (file): strings
// Input (keyboard): target to search for
// Output (screen): strings
// ----------------------------------------------------------------------
// Class: CS 210                     Instructor: Drs. Hwang & Roberts
// Assignment: Programming Exercise for 3/28/07 & 3/29/07
// Programmer(s): <fill in your name(s)>

#include <fstream>     // ifstream, ofstream
#include <iostream>    // cin, cout, <<, >>, endl
#include <cstdlib>     // exit
#include <string>      // string
#include <vector>      // vector
using namespace std;

// Function prototypes
void InteractiveOpen (ifstream & theIFStream);
void ReadVector (vector<string> & theVector);
void PrintVector (const vector<string> & theVector);

const int MAX_FILE_NAME = 80;

int main ()
{
   // Local data
   vector<string> names;    // Vector of strings
   string target;           // Search target
   int position;            // Position of target in vector

   // Greet the user
   cout << "This program reads in a list of strings from a file, prints\n"
        << "them out, and searches for a target\n";

   // Read in strings from a file
   ReadVector (names);

   // Print the numbers
   cout << "\nThe strings read from the file are:\n\n";
   PrintVector (names);

   // Search the vector for a target
   cout << "\nPlease enter a string to be searched for: ";
   cin >> target;
   position = -1;  // Replace -1 with call to search function
   if (position != -1)
      cout << target << " is in position" << position << endl;
   else
      cout << target << " is not in the vector\n";

   return 0;
}  // end main


// Function: InteractiveOpen (for input file streams)
// Asks the user for a file name and connects the file to theIFStream
void InteractiveOpen (ifstream & theIFStream)  
                      // RECEIVED & PASSED BACK: input file stream
{
   char inputFileName[MAX_FILE_NAME];  // input file name

   cout << "\nEnter the name of the input file: ";
   cin >> inputFileName;

   theIFStream.open(inputFileName);

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


// Function: ReadVector
// This function adds data items from a file to the end of a vector
// (Generally intended to be used on an initially empty vector.)
void ReadVector (vector<string> & theVector) 
                    // PASSED BACK: vector to be added to
{
   ifstream inStream;  // input file stream
   string value;       // input variable

   InteractiveOpen (inStream);

   // Read first value
   inStream >> value;

   // Loop until end of file
   while (!inStream.eof())
   {
      // Put value into vector
      theVector.push_back(value);

      // Read next value
      inStream >> value;
   }  // end while
   inStream.close();
}  // end ReadVector


// Function: PrintVector
// This function prints out the elements of a vector one on a line
void PrintVector (const vector<string> & theVector)
                     // RECEIVED: vector to be printed
{
   for (unsigned int i = 0; i < theVector.size(); i++)
   {
      cout << theVector[i] << endl;
   }  // end for
}  // end PrintVector
