Advertisement
ryancnap

Main.cpp

Apr 21st, 2024
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.06 KB | None | 0 0
  1. // Project 4 - Ryan Camaratta
  2.  
  3. // C - city then lname then fname
  4. // Z - zip then lname then fname
  5.  
  6. using namespace std;
  7.  
  8. #include <iostream>
  9. #include <iomanip>
  10. #include <fstream>
  11. #include <string>
  12.  
  13. #include "List.h"
  14. #include "Person.h"
  15.  
  16.  
  17. // Parses .csv file using comma as delimeter, as long as we can read from fileIn.
  18. bool readCSV(fstream &fileIn, string &record)
  19. {
  20.     char c;
  21.     record = "";
  22.     if (!(fileIn >> c))
  23.         return false;
  24.  
  25.     while (c != ',')
  26.     {
  27.         record = record + c;
  28.         if (!(fileIn >> c))
  29.             return false;
  30.     }
  31.     return true;
  32. }
  33.  
  34. // Display menu.
  35. void menu()
  36. {
  37.     cout << "\n\nF.....Search by first name\n";
  38.     cout << "L.....Search by last name\n";
  39.     cout << "A.....Search by street address\n";
  40.     cout << "C.....Search by city\n";
  41.     cout << "S.....Search by state\n";
  42.     cout << "Z.....Search by zip code\n";
  43.     cout << "Q.....Quit\n\n";
  44.     cout << ">> ";
  45. }
  46.  
  47. // Processes list depending on user input to search by different criteria.
  48. void process (List<Person> &names, Person person, char selection)
  49. {
  50.     string searchName, searchName2, checkName;
  51.     List<Person> namesSorted;
  52.  
  53.     // First name.
  54.      if (selection == 'F' || selection == 'f')
  55.         {
  56.             names.sortState = "F";
  57.             cout << "Enter first name to search: ";
  58.             cin >> searchName;
  59.             searchName2 = searchName + "zzz";
  60.             names.gotoFirst();
  61.             while (names.getCurrent(person))
  62.             {
  63.                 checkName = person.fName;
  64.                 if (searchName <= checkName && checkName <= searchName2)
  65.                     namesSorted.add(person);
  66.                 names.gotoNext();
  67.             }
  68.  
  69.             if (!(namesSorted.gotoFirst()))
  70.                 cout << "\nNo records found." << endl;
  71.             else
  72.             {
  73.                 cout << "\nFound the following record(s): " << endl << endl;
  74.                 namesSorted.gotoFirst();
  75.                 cout << left << setw(20) << "NAME";
  76.                 cout << left << setw(15) << "STREET";
  77.                 cout << left << setw(15) << "CITY";
  78.                 cout << left << setw(15) << "STATE";
  79.                 cout << left << setw(15) << "ZIP" << endl;
  80.                 cout << "----------------------------------------------------------------------" << endl << endl;
  81.                 while (namesSorted.getCurrent(person))
  82.                 {
  83.                     cout << left << setw(10) << person.fName;
  84.                     cout << left << setw(10) << person.lName;
  85.                     cout << left << setw(15) << person.street;
  86.                     cout << left << setw(15) << person.city;
  87.                     cout << left << setw(15) << person.state;
  88.                     cout << left << setw(15) << person.zip << endl;
  89.                     namesSorted.gotoNext();
  90.                 }
  91.             }
  92.         }
  93.  
  94.         // Last name.
  95.         if (selection == 'L' || selection == 'l')
  96.         {
  97.             names.sortState = "L";
  98.             cout << "Enter last name to search: ";
  99.             cin >> searchName;
  100.             searchName2 = searchName + "zzz";
  101.             names.gotoFirst();
  102.             while (names.getCurrent(person))
  103.             {
  104.                 checkName = person.lName;
  105.                 if (searchName <= checkName && checkName <= searchName2)
  106.                     namesSorted.add(person);
  107.                 names.gotoNext();
  108.             }
  109.             if (!(namesSorted.gotoFirst()))
  110.                 cout << "No records found." << endl;
  111.             else
  112.             {
  113.                 cout << "Found the following record(s): " << endl << endl;
  114.                 namesSorted.gotoFirst();
  115.                 cout << left << setw(20) << "NAME";
  116.                 cout << left << setw(15) << "STREET";
  117.                 cout << left << setw(15) << "CITY";
  118.                 cout << left << setw(15) << "STATE";
  119.                 cout << left << setw(15) << "ZIP" << endl;
  120.                 while (namesSorted.getCurrent(person))
  121.                 {
  122.                     cout << left << setw(10) << person.fName;
  123.                     cout << left << setw(10) << person.lName;
  124.                     cout << left << setw(15) << person.street;
  125.                     cout << left << setw(15) << person.city;
  126.                     cout << left << setw(15) << person.state;
  127.                     cout << left << setw(15) << person.zip << endl;
  128.                     namesSorted.gotoNext();
  129.                 }
  130.             }
  131.         }
  132.  
  133.         // Address.
  134.         if (selection == 'A' || selection == 'a')
  135.         {
  136.             names.sortState = "A";
  137.             cout << "Enter address to search: ";
  138.             cin >> searchName;
  139.             searchName2 = searchName + "zzz";
  140.             names.gotoFirst();
  141.             while (names.getCurrent(person))
  142.             {
  143.                 checkName = person.street;
  144.                 if (searchName <= checkName && checkName <= searchName2)
  145.                     namesSorted.add(person);
  146.                 names.gotoNext();
  147.             }
  148.             if (!(namesSorted.gotoFirst()))
  149.                 cout << "No records found." << endl;
  150.             else
  151.             {
  152.                 cout << "Found the following record(s): " << endl << endl;
  153.                 namesSorted.gotoFirst();
  154.                 cout << left << setw(20) << "NAME";
  155.                 cout << left << setw(15) << "STREET";
  156.                 cout << left << setw(15) << "CITY";
  157.                 cout << left << setw(15) << "STATE";
  158.                 cout << left << setw(15) << "ZIP" << endl;
  159.                 while (namesSorted.getCurrent(person))
  160.                 {
  161.                     cout << left << setw(10) << person.fName;
  162.                     cout << left << setw(10) << person.lName;
  163.                     cout << left << setw(15) << person.street;
  164.                     cout << left << setw(15) << person.city;
  165.                     cout << left << setw(15) << person.state;
  166.                     cout << left << setw(15) << person.zip << endl;
  167.                     namesSorted.gotoNext();
  168.                 }
  169.             }
  170.         }
  171.  
  172.         // City
  173.  
  174.         // State
  175.  
  176.         // Zip
  177. }
  178.  
  179.  
  180.  
  181. int main()
  182. {
  183.     // Instantiate a List of Person objects.
  184.     List<Person> names;
  185.     List<Person> namesSorted;
  186.     Person person;
  187.     fstream fileIn;
  188.     fileIn.open("csvfile.csv");
  189.     string searchName;
  190.     string searchName2;
  191.     string checkName;
  192.  
  193.  
  194.     // Populate fields of each instance of Person, add each instance to List.
  195.     while(readCSV(fileIn, person.personID))
  196.     {
  197.         readCSV(fileIn, person.fName);
  198.         readCSV(fileIn, person.lName);
  199.         readCSV(fileIn, person.street);
  200.         readCSV(fileIn, person.city);
  201.         readCSV(fileIn, person.state);
  202.         fileIn >> person.zip;
  203.         names.add(person);
  204.     }
  205.  
  206.     // Display menu and prompt for input.
  207.     char selection;
  208.     do
  209.     {
  210.         menu();
  211.         cin >> selection;
  212.         process(names, person, selection);
  213.     }
  214.     while (toupper(selection) != 'Q');
  215.  
  216.  
  217.     // Search by different criteria.
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.     return 0;
  226.  
  227. }
  228.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement