/* * Author: Kyle Power * Course: CIS237 * Assignment: #4 * Due Date: Fri, Dec 06 * Filename: p4.cpp * Purpose: main .cpp file that implements the State class. Provides functionality * by allowing the user to sort and print out the States.data.txt file. */ #include #include #include #include #include #include #include "Date.h" #include "State.h" using namespace std; /* * * Function: MenuFunc * * Description: Provides the User with a Menu with which to navigate the program * * Parameters: map > &StMap - The map sorted by letter in state names * map > &CapMap - the map sorted by letter in state capitals * vector &StVec - The vector of State objects that have been read in * * Return value: none * */ void MenuFunc(map > &StMap, map > &CapMap, vector &StVec); /* * * Function: GetInfo * * Description: Reads the States.data.txt file data into the vector StVec * * Parameters: vector &StVec - The vector of State objects that have been read in * * Return value: none * */ void GetInfo(vector &StVec); /* * * Function: getChoice * * Description: function to help when reading in menu choices. Converts the choice * into caps, checks to see if it's valid, then returns it. * * Parameters: string check - the string to check for validity against. * * Return value: char - returns the character the user entered in caps * */ char getChoice(string check); /* * * Function: PrintStates * * Description: Outputs the states vector in it's current ordering * * Parameters: vector &StVec - The vector of State objects that have been read in * * Return value: none * */ void PrintStates(vector StVec); /* * * Function: NameSearach * * Description: Asks user to enter a state name, then uses STL find() function * to retrieve the state from the vector * * Parameters: vector &StVec - The vector of State objects that have been read in * * Return value: none * */ void NameSearch(vector StVec); /* * * Function: LetterSearch * * Description: Provides user with a sub-menu that asks whether they want to * list the state names by letter or the state capitals by letter * and then calls the appropriate function. * * Parameters: map > &StMap - The map sorted by letter in state names * map > &CapMap - the map sorted by letter in state capitals * * Return value: none * */ void LetterSearch(map > StMap, map > CapMap); /* * * Function: NameByLetter * * Description: Asks the user which letter they wish to have listed, and then * outputs all state names containing that letter. * * Parameters: map > &StMap - The map sorted by letter in state names * * Return value: none * */ void NameByLetter(map > StMap); /* * * Function: CapByLetter * * Description: Asks the user which letter they would like to view, and then * outputs all the state capitals that contain that letter * * Parameters: map > &CapMap - the map sorted by letter in state capitals * * Return value: none * */ void CapByLetter(map > CapMap); /* * * Function: LoadMaps * * Description: Sorts the data and loads the proper values into each key of the two * maps * * Parameters: map > &StMap - The map sorted by letter in state names * map > &CapMap - the map sorted by letter in state capitals * * Return value: none * */ void LoadMaps(map > &StMap, map > &CapMap, vector StVec); /* * * Function: Reorder * * Description: Asks the user which sorting they would like applied to the vector, * and then uses stl sort() function to sort it properly * * Parameters: vector &StVec - The vector of State objects that have been read in * char SortType - a character value that keeps track of which sort * is currently applied, and prevents a resort of * same type * * Return value: none * */ void Reorder(vector &StVec, char &SortType); int main() { vector StVec; //vector object that holds the States map > StMap, CapMap; //maps that sort state and capital by letters GetInfo(StVec); //loads the data into the State vector LoadMaps(StMap, CapMap, StVec); //loads the data into the two map objects MenuFunc(StMap, CapMap, StVec); //gives the user a menu with which to navigate the program return 0; } /**************************************************** * MenuFunc ****************************************************/ void MenuFunc(map > &StMap, map > &CapMap, vector &StVec) { char choice, SortType = 'N'; //for SortType: N = by name, C = by Capital, //P = by Pop., A = by Area, D = Date of Admission do { cout<<"Choose an Option:"<"; choice = getChoice("PLNRQ"); //get the users input in proper format switch(choice) { case 'P': cout<<"The States:"< &StVec) { ifstream inFile; State StHolder; //temporary State object to read data into inFile.open("States.data.txt"); // while(!inFile.eof()) while(inFile >> StHolder) { // inFile >> StHolder; StVec.push_back(StHolder); } inFile.close(); sort(StVec.begin(), StVec.end()); //first sort sorts the data by name } /**************************************************** * getChoice ****************************************************/ char getChoice(string check) { char ch=' '; do ch=toupper(cin.get()); while (check.find(ch)==string::npos); cin.get(); // eat CR return(toupper(ch)); } /**************************************************** * PrintStates ****************************************************/ void PrintStates(vector StVec) { vector::iterator it; //uses iterator to print vector for(it=StVec.begin(); it != StVec.end(); it++) { cout<<*it< StVec) { string StIn, StName; State temp; //temporary state object to hold data cout<<"Enter Name of State to Search For"<"; cin>>StIn; for(int i=0; i < StIn.length(); i++) //loops through string and turns all { //lowercase letters into uppercase if(StIn[i] >= 97 && StIn[i] <= 122) StIn[i]-=32; } vector::iterator it; it = find(StVec.begin(), StVec.end(), StIn); //stl find() searches vector and returns if(it == StVec.end()) //iterator to element if found { cout<<"State not found"< > StMap, map > CapMap) { char choice, Letter; do { cout<<"Select Letter In:"<"; choice = getChoice("NCG"); //get choice and format it to uppercase switch(choice) { case 'N': NameByLetter(StMap); break; case 'C': CapByLetter(CapMap); break; } }while(choice !='G'); } /**************************************************** * NameByLetter ****************************************************/ void NameByLetter(map > StMap) { State temp; char i; cout<<"Enter Letter to Search For"<"; cin>>i; i=toupper(i); //needs to be uppercase set::iterator it; cout<<"States with names containing \'"< > CapMap) { State temp; char i; cout<<"Enter Letter to Search For"<"; cin>>i; i=toupper(i); //needs to be uppercase set::iterator it; cout<<"State Capitals with names containing \'"< > &StMap,map > &CapMap, vector StVec) { string St, Cap; //temporary values to hold state name and capital as needed State temp; //temporary state object to hold value vector::iterator it; for(it=StVec.begin(); it != StVec.end(); it++) { temp = *it; //iterates through vector St = temp.getName(); //gets State name for(int i=0; i < St.length(); i++) //iterates through state name { StMap[St[i]].insert(*it); //for each letter in state name, insert } //value into StMap } for(it=StVec.begin(); it != StVec.end(); it++) { temp = *it; //iterates trhough vector St = temp.getCap(); //gets state capital for(int i=0; i < St.length(); i++) { CapMap[St[i]].insert(*it); //for each letter in capital, insert } //value into CapMap } } /***************************************************** * Reorder *****************************************************/ void Reorder(vector &StVec, char &SortType) { char choice; do { cout<<"Sort By..."<"; choice=getChoice("NCPADG"); //get and format input if(choice == SortType) //if it's already sorted in that way return; //we return switch(choice) { case 'N': sort(StVec.begin(), StVec.end()); //sort by name uses overloaded < op SortType = 'N'; //so we know how the vector is currently sorted return; case 'C': sort(StVec.begin(), StVec.end(), State::ByCap()); //other sorts use functors in State.h SortType = 'C'; return; case 'P': sort(StVec.begin(), StVec.end(), State::ByPop()); SortType = 'P'; return; case 'A': sort(StVec.begin(), StVec.end(), State::ByArea()); SortType = 'A'; return; case 'D': sort(StVec.begin(), StVec.end(), State::ByDate()); SortType = 'D'; return; } }while(choice != 'G'); return; }