Ryan Bury

Baby Names Ranking Program

BabyNames in Action

Download Executable



Chap12Honors1.cpp

Here is the source code that powers the whole project. This was before I learned about the importance of separation in coding!

Download Source Code

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <time.h>

using namespace std;

void program1()
{
	bool found = false;
	int year = 0;
	char gender;
	string name;

	while (!(year >= 2000 && year <= 2013))
	{
		cout << "Enter a year to search: " << endl;
		cin >> year;
		if (!(year >= 2000 && year <= 2013))
		{
			cout << "Invalid Year" << endl;
		}
	}
	cout << "Gender (M/F): " << endl;
	cin >> gender;
	cout << "Name: " << endl;
	cin >> name;

	string str = "babynamesrank";
	str.append(to_string(year));
	str.append(".dat");

	fstream bFile(str, ios::in | ios::binary | ios::ate);
	streampos size = bFile.tellg();
	char *arr = new char[size];
	bFile.seekg(0, ios::beg);
	bFile.read(arr, size);
	bFile.close();

	string S(arr, size);

	while (S.find('\n') != string::npos)
	{
		str = S.substr(0, S.find('\n'));

		if (str.find(name) != string::npos)
		{
			string rank = str.substr(0, str.find(' '));
			str.erase(0, str.find(' ') + 1);
			string mName = str.substr(0, str.find(' '));
			str.erase(0, str.find(' ') + 1);
			string mNum = str.substr(0, str.find(' '));
			str.erase(0, str.find(' ') + 1);
			string fName = str.substr(0, str.find(' '));
			str.erase(0, str.find(' ') + 1);
			string fNum = str.substr(0, str.find(' '));

			if (gender == 'M' && mName == name)
			{
				cout << mName << " is ranked #" << rank << " in " << year << " with " << mNum << " members." << endl;
			}
			else if (gender == 'F' && fName == name)
			{
				cout << fName << " is ranked #" << rank << " in " << year << " with " << fNum << " members." << endl;
			}
			found = true;
		}

		S.erase(0, S.find('\n') + 1);
	}
	if (found == false)
	{
		cout << name << " is not ranked in year " << year << endl;
	}

	bFile.close();
}

void program2()
{
	const int fileSize = 1000;
	int year = 0;
	string *male = new string[fileSize];
	string *female = new string[fileSize];
	string *common = new string[fileSize];

	while (!(year >= 2000 && year <= 2013))
	{
		cout << "Enter a year to search: " << endl;
		cin >> year;
		if (!(year >= 2000 && year <= 2013))
		{
			cout << "Invalid Year" << endl;
		}
	}

	time_t time0 = time(NULL), time1 = 0;

	string str = "babynamesrank";
	str.append(to_string(year));
	str.append(".dat");

	fstream bFile(str, ios::in | ios::binary | ios::ate);
	streampos size = bFile.tellg();
	char *arr = new char[size];
	bFile.seekg(0, ios::beg);
	bFile.read(arr, size);
	bFile.close();

	string S(arr, size);

	for (int i = 0; S.find('\n') != string::npos; i++)
	{
		str = S.substr(0, S.find('\n'));
		string rank = str.substr(0, str.find(' '));
		str.erase(0, str.find(' ') + 1);
		string mName = str.substr(0, str.find(' '));
		str.erase(0, str.find(' ') + 1);
		string mNum = str.substr(0, str.find(' '));
		str.erase(0, str.find(' ') + 1);
		string fName = str.substr(0, str.find(' '));
		str.erase(0, str.find(' ') + 1);
		string fNum = str.substr(0, str.find(' '));

		male[i] = mName;
		female[i] = fName;
		S.erase(0, S.find('\n') + 1);
	}

	int inc = 0;
	for (int i = 0; i < fileSize; i++)
	{
		for (int j = 0; j < fileSize; j++)
		{
			if (male[i] == female[j])
			{
				common[inc] = male[i];
				inc++;
			}
		}
	}

	cout << "There were " << inc << " names in common with both genders." << endl;
	cout << "They are: ";
	for (int i = 0; i < inc; i++)
	{
		cout << common[i] << ", ";
	}
	cout << endl;

	delete[] male;
	delete[] female;
	delete[] common;
	bFile.close();

	time1 = time(NULL);
	cout << "Time to completion: " << time1 - time0 << endl;
}

void program3()
{

	string name;
	char gender;
	string ranks[14];

	for (int i = 0; i < 14; i++)
	{
		ranks[i] = "Not Ranked";
	}

	cout << "Name: " << endl;
	cin >> name;
	cout << "Gender (M/F): " << endl;
	cin >> gender;

	int year[14] = { 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 };

	fstream *bFile = new fstream[14];
	string *S = new string[14];
	for (int i = 0; i < 14; i++)
	{
		string str = "babynamesrank";
		str.append(to_string(year[i]));
		str.append(".dat");

		bFile[i].open(str, ios::in | ios::binary | ios::ate);
		streampos size = bFile[i].tellg();
		char *arr = new char[size];
		bFile[i].seekg(0, ios::beg);
		bFile[i].read(arr, size);
		bFile[i].close();

		S[i] = arr;
	}

	string str;

	for (int i = 0; i < 14; i++)
	{
		while (S[i].find('\n') != string::npos)
		{
			str = S[i].substr(0, S[i].find('\n'));
			string rank = str.substr(0, str.find(' '));
			str.erase(0, str.find(' ') + 1);
			string mName = str.substr(0, str.find(' '));
			str.erase(0, str.find(' ') + 1);
			string mNum = str.substr(0, str.find(' '));
			str.erase(0, str.find(' ') + 1);
			string fName = str.substr(0, str.find(' '));
			str.erase(0, str.find(' ') + 1);
			string fNum = str.substr(0, str.find(' '));

			if (gender == 'F' && fName == name)
			{
				ranks[i] = rank;
			}
			if (gender == 'M' && mName == name)
			{
				ranks[i] = rank;
			}
			S[i].erase(0, S[i].find('\n') + 1);
		}
	}

	cout << "For: " << name << endl;
	cout << left << setw(15) << "Year of Birth:" << setw(7) << "Rank:" << endl;
	for (int i = 0; i < 14; i++)
	{
		cout << setw(15) << year[i] << setw(7) << ranks[i] << endl;
	}


	for (int i = 0; i < 14; i++)
	{
		bFile[i].close();
	}
	delete[] bFile;
	delete[] S;
}

void program4()
{
	fstream file;
	fstream bFile;
	string str;

	int year[14] = { 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 };

	for (int i = 0; i < 14; i++)
	{
		string fileNametxt = "babynamesrank";
		fileNametxt.append(to_string(year[i]));
		string fileNamedat = fileNametxt;
		fileNametxt.append(".txt");
		fileNamedat.append(".dat");
		file.open(fileNametxt, ios::in);
		bFile.open(fileNamedat, ios::out | ios::binary);
		while (getline(file, str))
		{
			str.append("\n");
			bFile.write(str.c_str(), str.size());
		}
		file.close();
		bFile.close();
	}
	
	bFile.open("babynamesrank2000.dat", ios::in | ios::binary | ios::ate);
	streampos size = bFile.tellg();
	char *arr = new char[size];
	bFile.seekg(0, ios::beg);
	bFile.read(arr, size);
	bFile.close();

	string myS(arr, size);

	cout << myS;

}

int main()
{
	bool running = true;

	while (running)
	{
		int choice = 0;
		cout << "Which program would you like to run? " << endl;
		cout << "Years searchable are 2000-2013 inclusive" << endl;
		cout << "1. Search for a single name" << endl;
		cout << "2. Find all common names between genders in a certain file" << endl;
		cout << "3. Find the ranking by year of a name" << endl;
		cout << "4. Convert files to binary files" << endl;
		cout << "5. Exit" << endl;
		cout << "You MUST convert files to binary before searching their contents" << endl;
		cin >> choice;

		switch (choice)
		{
		case 1:
			program1();
			break;
		case 2:
			program2();
			break;
		case 3:
			program3();
			break;
		case 4:
			program4();
			break;
		case 5:
			running = false;
		}
	}

	cin.ignore();
	cout << "Press ENTER to continue...";
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}