/**************************************
This program simply reads 10 names from
the file names.txt, then repeatedly
queries the user for a first name, and
prints all names with a first-name that
matches the query value.
**************************************/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

/**************************************
 ** PROTOTYPES
 **************************************/
bool match(string* name, string x);
int searchFrom(string** A, int N, string x, int pos);

/**************************************
 ** MAIN FUNCTION
 **************************************/
int main()
{
  // Open file names.txt containing 10 names
  ifstream fin("names.txt");

  // Create and populate 2D array of 10 names
  // names[i][0] is first name
  // names[i][1] is last name
  int N = 10;
  string **names = new string*[N];
  for(int i = 0; i < N; i++)
    names[i] = new string[2];
  for(int i = 0; i < N; i++)
    fin >> names[i][0] >> names[i][1];

  // get query and answer
  string x;
  while(cout << "Name (or quit): " && cin >> x && x != "quit")
  {
    for(int i = 0; i < N; i++)
    {
      i = searchFrom(names, N, x, i);
      if( i < N )
        cout << names[i][0] << ' ' << names[i][1] << endl;
    }
  }

  return 0;
}

/**************************************
 ** FUNCTION DEFINITIONS
 **************************************/
bool match(string* name, string x)
{
  return name[0] == x;
}

int searchFrom(string** A, int N, string x, int pos)
{
  for(int i=pos; i < N; i++)
    if ( match(A[i],x) )
      return i;

  return N;
}