/***************************************************
Analyzing Census data:  Town/City/ADP Count

Write a program that reads in a datafile from:

www.census.gov/population/www/censusdata/places.html

and prints out the number of towns, cities, and 
CDP's.  The user should specify the name of the
file to be analyzed.  This way, if you have data
files from several states saved, you can analyze
all of them with the same program.

Note:  If s is an object of type string that
contains the name of the file you want to open
you can't say:

         ifstream fin(s);

which would seem pretty likely.  Instead, you have
to say:

         ifstream fin(s.begin());

Why and what that means we'll discuss later.  For
now just keep in mind that this is how it works.
***************************************************/
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
  // Get input file name
  string filename;
  cout << "Enter file name: ";
  cin >> filename;
  
  // Open input file
  ifstream fin(filename.c_str());

  if (fin)
  {
    /****** INPUT FILE EXISTS ****************/
    // Initialize town count
    int count_t, count_c, count_cdp;
    count_t = count_c = count_cdp = 0;

    // Read in string until file is finished
    string s;
    while(fin >> s)
    {
      if (s == "town")
	count_t++;
      else if (s == "city")
	count_c++;
      else if (s == "CDP")
	count_cdp++;
    }
    
    // Write results
    cout << "There are " << count_t
	 << " towns" << endl;
    cout << "There are " << count_c
	 << " cities" << endl;
    cout << "There are " << count_cdp
	 << " CDP's" << endl;
  }
  else
    /****** INPUT FILE DOES NOT EXIST ********/
    cout << "File not found!" << endl;
  
  return 0;
}