// searchList.cc   P. Conrad  for CISC181 04/06/2007
// Read numbers from a file into a linked list of ints
// Then allow users to search that list for numbers

#include <iostream>
#include <fstream>
#include <cstdlib>

using std::cout;
using std::cerr;
using std::cin;
using std::endl;
using std::istream;
using std::ifstream;
using std::ios;

#include "intLinkedList.h"

// function prototypes 

void letUserLookForNumbers(Node_S *head);

int main()
{
  // open file
  const char * const filename = "nums.dat";
  ifstream infile(filename,ios::in);
  if (!infile)
    {
      cerr << "Could not open " << filename << endl; 
      exit(1);
    }

  Node_S *head,*tail;
  head = tail = NULL;  // create empty list

  // read file into linked list
  addIntsFromFileToEndOfLinkedList(head, tail, infile);

#ifdef DEBUG
  cerr << "DEBUG: Here's the list: ";
  printListOnOneLine(head, cerr);
  cerr << endl;
#endif

  letUserLookForNumbers(head);

  return 0;

} // end of main


void letUserLookForNumbers(Node_S *head)
{  
  cout << "At each prompt, enter a number to search for, or q to quit,\n"
       << " and the program will tell you whether\n"
       << " that number is in the list \n\n";
  
  const int INPUTLINESIZE = 1024;
  char inputLine[INPUTLINESIZE];
  
  cout << "number (or q to quit): "; cin.getline(inputLine,INPUTLINESIZE);
  while (!cin.eof() && inputLine[0] != 'q') // stop at EOF or the letter 'q'
    {
      int num = atoi(inputLine); // convert input to an integer
      
      if (isInList(head,num))
	cout << num << " is in the list" << endl;
      else
	cout << num << " is not in the list" << endl;
      
      cout << "number (or q to quit): "; cin.getline(inputLine,INPUTLINESIZE);
    }
} // letUserLookForNumbers 




