Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Project 4 - Ryan Camaratta
- // C - city then lname then fname
- // Z - zip then lname then fname
- using namespace std;
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include "List.h"
- #include "Person.h"
- // Parses .csv file using comma as delimeter, as long as we can read from fileIn.
- bool readCSV(fstream &fileIn, string &record)
- {
- char c;
- record = "";
- if (!(fileIn >> c))
- return false;
- while (c != ',')
- {
- record = record + c;
- if (!(fileIn >> c))
- return false;
- }
- return true;
- }
- // Display menu.
- void menu()
- {
- cout << "\n\nF.....Search by first name\n";
- cout << "L.....Search by last name\n";
- cout << "A.....Search by street address\n";
- cout << "C.....Search by city\n";
- cout << "S.....Search by state\n";
- cout << "Z.....Search by zip code\n";
- cout << "Q.....Quit\n\n";
- cout << ">> ";
- }
- // Processes list depending on user input to search by different criteria.
- void process (List<Person> &names, Person person, char selection)
- {
- string searchName, searchName2, checkName;
- List<Person> namesSorted;
- // First name.
- if (selection == 'F' || selection == 'f')
- {
- names.sortState = "F";
- cout << "Enter first name to search: ";
- cin >> searchName;
- searchName2 = searchName + "zzz";
- names.gotoFirst();
- while (names.getCurrent(person))
- {
- checkName = person.fName;
- if (searchName <= checkName && checkName <= searchName2)
- namesSorted.add(person);
- names.gotoNext();
- }
- if (!(namesSorted.gotoFirst()))
- cout << "\nNo records found." << endl;
- else
- {
- cout << "\nFound the following record(s): " << endl << endl;
- namesSorted.gotoFirst();
- cout << left << setw(20) << "NAME";
- cout << left << setw(15) << "STREET";
- cout << left << setw(15) << "CITY";
- cout << left << setw(15) << "STATE";
- cout << left << setw(15) << "ZIP" << endl;
- cout << "----------------------------------------------------------------------" << endl << endl;
- while (namesSorted.getCurrent(person))
- {
- cout << left << setw(10) << person.fName;
- cout << left << setw(10) << person.lName;
- cout << left << setw(15) << person.street;
- cout << left << setw(15) << person.city;
- cout << left << setw(15) << person.state;
- cout << left << setw(15) << person.zip << endl;
- namesSorted.gotoNext();
- }
- }
- }
- // Last name.
- if (selection == 'L' || selection == 'l')
- {
- names.sortState = "L";
- cout << "Enter last name to search: ";
- cin >> searchName;
- searchName2 = searchName + "zzz";
- names.gotoFirst();
- while (names.getCurrent(person))
- {
- checkName = person.lName;
- if (searchName <= checkName && checkName <= searchName2)
- namesSorted.add(person);
- names.gotoNext();
- }
- if (!(namesSorted.gotoFirst()))
- cout << "No records found." << endl;
- else
- {
- cout << "Found the following record(s): " << endl << endl;
- namesSorted.gotoFirst();
- cout << left << setw(20) << "NAME";
- cout << left << setw(15) << "STREET";
- cout << left << setw(15) << "CITY";
- cout << left << setw(15) << "STATE";
- cout << left << setw(15) << "ZIP" << endl;
- while (namesSorted.getCurrent(person))
- {
- cout << left << setw(10) << person.fName;
- cout << left << setw(10) << person.lName;
- cout << left << setw(15) << person.street;
- cout << left << setw(15) << person.city;
- cout << left << setw(15) << person.state;
- cout << left << setw(15) << person.zip << endl;
- namesSorted.gotoNext();
- }
- }
- }
- // Address.
- if (selection == 'A' || selection == 'a')
- {
- names.sortState = "A";
- cout << "Enter address to search: ";
- cin >> searchName;
- searchName2 = searchName + "zzz";
- names.gotoFirst();
- while (names.getCurrent(person))
- {
- checkName = person.street;
- if (searchName <= checkName && checkName <= searchName2)
- namesSorted.add(person);
- names.gotoNext();
- }
- if (!(namesSorted.gotoFirst()))
- cout << "No records found." << endl;
- else
- {
- cout << "Found the following record(s): " << endl << endl;
- namesSorted.gotoFirst();
- cout << left << setw(20) << "NAME";
- cout << left << setw(15) << "STREET";
- cout << left << setw(15) << "CITY";
- cout << left << setw(15) << "STATE";
- cout << left << setw(15) << "ZIP" << endl;
- while (namesSorted.getCurrent(person))
- {
- cout << left << setw(10) << person.fName;
- cout << left << setw(10) << person.lName;
- cout << left << setw(15) << person.street;
- cout << left << setw(15) << person.city;
- cout << left << setw(15) << person.state;
- cout << left << setw(15) << person.zip << endl;
- namesSorted.gotoNext();
- }
- }
- }
- // City
- // State
- // Zip
- }
- int main()
- {
- // Instantiate a List of Person objects.
- List<Person> names;
- List<Person> namesSorted;
- Person person;
- fstream fileIn;
- fileIn.open("csvfile.csv");
- string searchName;
- string searchName2;
- string checkName;
- // Populate fields of each instance of Person, add each instance to List.
- while(readCSV(fileIn, person.personID))
- {
- readCSV(fileIn, person.fName);
- readCSV(fileIn, person.lName);
- readCSV(fileIn, person.street);
- readCSV(fileIn, person.city);
- readCSV(fileIn, person.state);
- fileIn >> person.zip;
- names.add(person);
- }
- // Display menu and prompt for input.
- char selection;
- do
- {
- menu();
- cin >> selection;
- process(names, person, selection);
- }
- while (toupper(selection) != 'Q');
- // Search by different criteria.
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement