// phone.cp
// Look up matches in a simple telephone database.

#include <iostream>
#include <fstream>
#include <string>

int main()
{
	// Introduce ourselves.

	std::cout << "Here is the Phonebook --\n" << std::endl;

	// Get the match string from the user.

	std::cout << "do you want to a) enter a new book or b) search? ";
	char input;
	cin >> input;

	if (input == 'b') {

	std::string match;
	std::cout << "enter a string to match: ";
	std::cin >> match;

	std::cout << std::endl;

	// Search the phonebook for matching records.

	std::ifstream phonebook("phonebook");
	std::string record;

	if (!phonebook.good())
		std::cout << "No such book.  Sorry." << std::endl;
		
	while(getline(phonebook,record)) {
		if (record.find(match) != std::string::npos)
			std::cout << record << std::endl;
	}


	// We're done.
	}

	//if input == 'a'
	else {

		std::string bookname = "";
		std::cout << "enter the name of the phonebook: ";
		std::cin >> bookname;

		std::ofstream newbook(bookname.c_str());

		cout << "enter QUIT to finish\n";
		std::string record = "";

		while (record.find("QUIT") != 0) {
			newbook << record << "\n";
			cin >> record;

		}
	}
		

	return 0;
}

