/**************************************
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 = searchFrom(names,N,x,0); i < N; i = searchFrom(names,N,x,i+1))
      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)
{
  int i = pos;
  while(i < N && !match(A[i],x))
    i++;
  return i;
}