Advertisement
Guest User

Main.cpp

a guest
Mar 21st, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include "User.h"
  7. #include "HashFriend.h"
  8. #include "HashInterest.h"
  9. //#include "Graph.h"
  10. //#include "HashInterest.h"
  11. //#include "HashSomethingIDK.h"
  12.  
  13. using namespace std;
  14.  
  15. bool logIn();
  16. int mainMenu();
  17. void viewFriends(int id);
  18. void searchNewFriends();
  19. void removeFriends(int id);
  20. void friendRecs();
  21.  
  22. vector<User> userID;
  23.  
  24. void readFile()
  25. {
  26.         HashFriend hf;
  27.         HashInterest hi;
  28.         string firstName, lastName, userName, passWord, city, state, friendName, interest;
  29.         string filename;
  30.         unsigned id = 1;
  31.  
  32.         User empty;
  33.         userID.push_back(empty); // index 0 is a empty user
  34.  
  35.         filename = "data.txt";
  36.         ifstream inputfile;
  37.         inputfile.open(filename);
  38.  
  39.         assert(inputfile);
  40.  
  41.         while(inputfile)
  42.         {
  43.         inputfile >> firstName;
  44.         inputfile >> lastName;
  45.         inputfile >> userName;
  46.         inputfile >> passWord;
  47.         inputfile.get(); // go to next line
  48.         getline(inputfile, city, ',');
  49.         getline(inputfile, state);
  50.  
  51.         User user(firstName, lastName, userName, passWord, city, state, id);
  52.  
  53.         getline(inputfile, friendName);
  54.         istringstream tempF(friendName);
  55.         while(tempF)
  56.         {
  57.             getline(tempF, friendName, ',');
  58.             if(tempF.peek() == ' ')
  59.                 tempF.get();
  60.             user.setFriends(friendName);
  61.         }
  62.  
  63.         getline(inputfile, interest);
  64.         istringstream tempI(interest);
  65.         while(tempI)
  66.         {
  67.             getline(tempI, interest, ',');
  68.             if(tempI.peek() == ' ')
  69.                 tempI.get();
  70.             user.setInterests(interest);
  71.         }
  72.  
  73.         while(inputfile.peek() == '\n')
  74.             inputfile.get(); // go to next line
  75.         while(inputfile.peek() == ' ')
  76.             inputfile.get(); // go to next line
  77.  
  78.         userID.push_back(user); // insert to the id vector
  79.         hf.insert(user);// insert to hashFriend
  80.         hi.insert(user);// insert to hashInterest
  81.         id ++;
  82.         }
  83.     inputfile.close();
  84. }
  85.  
  86. int main()
  87. {
  88.     string name;
  89.     int choice;
  90.     int id = 0;
  91.     readFile();
  92.  
  93.     // Log in validation
  94.     while (id <= 0)
  95.             id = logIn();
  96.  
  97.     name = "<Name>";
  98.     //name = user.getFirstName();
  99.     cout << "\n\nWelcome, " << name << "!\n";
  100.  
  101.  
  102.  
  103.     do{
  104.         choice = mainMenu();
  105.  
  106.         int temp = 1; //DELETE LATER, USED JUST FOR TESTING
  107.  
  108.         switch(choice)
  109.         {
  110.             case 1:     // view friends
  111.                 viewFriends(temp);
  112.                 break;
  113.  
  114.             case 2:     // search for new friends
  115.                 searchNewFriends();
  116.                 break;
  117.  
  118.             case 3:     // friends rec
  119.                 friendRecs();
  120.                 break;
  121.  
  122.  
  123.             case 4:      //quit
  124.             {
  125.                 /*
  126.                 ofstream outfile;
  127.                 outfile.open("userinfo.txt");
  128.                 //print to outfile
  129.                 outfile.close();
  130.                 */
  131.  
  132.  
  133.  
  134.                 cout << "     You are signed out"
  135.                      << "\n\t- GOODBYE! -" << endl;
  136.             }
  137.         }
  138.  
  139.     }while(choice != 4);
  140.  
  141.     return 0;
  142. }
  143.  
  144.  
  145. /// Returns bool for whether or not username & password combo are matched
  146. bool logIn()
  147. {
  148.     string un, pw;
  149.  
  150.     cout << "\t- LOG IN -" << endl
  151.          << "Username: ";
  152.     cin >> un;
  153.     cout << "Password: ";
  154.     cin >> pw;
  155.  
  156.     if(1)   // if un & pw combo are found
  157.         return true;
  158.     else
  159.         return false;
  160.  
  161. }
  162.  
  163.  
  164. /// Main Menu :)
  165. int mainMenu()
  166. {
  167.     string input = "0";
  168.     do{
  169.         cout << "\n\t - MENU -" << endl
  170.              << "1. View Friends" << endl
  171.              << "2. Search for New Friends" << endl
  172.              << "3. Friends Recommendations" << endl
  173.              << "4. Quit\n\n"
  174.              << "Enter choice: ";
  175.         cin >> input;
  176.  
  177.         if(!isdigit(input[0]))
  178.         {
  179.             cout << "Please enter numbers only.\n";
  180.             input = "0";
  181.         }
  182.         else if(!(input == "1" || input == "2" || input == "3" || input == "4"))
  183.         {
  184.             cout << "Invalid input.\n";
  185.             input = "0";
  186.         }
  187.  
  188.     }while(input == "0");
  189.  
  190.     cout << endl;
  191.     int choice = atoi(input.c_str());
  192.  
  193.     return choice;
  194. }
  195.  
  196.  
  197. /// Menu option to view friends
  198. void viewFriends(int id)
  199. {
  200.     id = 1; //temporarily assigned to spongebob
  201.  
  202.     string input = "0";
  203.     User u = userID[1];
  204.  
  205.  
  206.     cout << "     - View Friends -" << endl;
  207.  
  208.         cout << "1. View all friends\n"
  209.              << "2. View a friend's profile\n"
  210.              << "3. Remove a friend\n\n"
  211.              << "Enter choice or 'm' for menu: ";
  212.         cin >> input;
  213.  
  214.      do{   if(input[0] == 'm')
  215.                 return;
  216.         else if(!isdigit(input[0]))
  217.         {
  218.             cout << "Please enter numbers or 'm' only.\n\n";
  219.             input = "0";
  220.         }
  221.         else if(!(input == "1" || input == "2" || input == "3"))
  222.         {
  223.             cout << "Invalid input.\n\n";
  224.             input = "0";
  225.         }
  226.         else if (input == "3")
  227.         {
  228.                 removeFriends(id);
  229.         }
  230.         else        // input == 1 2 or 3
  231.         {
  232.             u.printFriends();
  233.             cout << "1. View a friend's profile\n"
  234.                  << "2. Remove a friend\n\n"
  235.                  << "Enter choice or 'm' for menu: ";
  236.             cin >> input;
  237.             if (input == "1")
  238.                 {
  239.                     //friend profile function
  240.                 }
  241.             else if (input == "2")
  242.             {
  243.                     removeFriends(id);
  244.             }
  245.  
  246.         }
  247.  
  248.     }while(input == "0");
  249.     cout << endl;
  250. }
  251.  
  252. /// Menu option to search for new friends
  253. void searchNewFriends()
  254. {
  255.     string input = "0";
  256.     cout << " - Search for New Friends -" << endl;
  257.     do{
  258.         cout << "1. Search by Name\n"
  259.              << "2. Search by Interest\n\n"
  260.              << "Enter choice or 'm' for menu: ";
  261.         cin >> input;
  262.  
  263.         if(input[0] == 'm')
  264.                 return;
  265.         else if(!isdigit(input[0]))
  266.         {
  267.             cout << "Please enter numbers or 'm' only.\n\n";
  268.             input = "0";
  269.         }
  270.         else if(!(input == "1" || input == "2"))
  271.         {
  272.             cout << "Invalid input.\n\n";
  273.             input = "0";
  274.         }
  275.         else        // input == 1 or 2
  276.         {
  277.             //int choice = atoi(input.c_str());
  278.  
  279.         }
  280.  
  281.     }while(input == "0");
  282.     cout << endl;
  283. }
  284.  
  285.  
  286. /// Menu option to get friend recommendations
  287. void friendRecs()
  288. {
  289.     cout << " - Friend Recommendations -" << endl
  290.          << "People you may know:\n\n"
  291.          // print all the ppl using graph (?)
  292.          << "Enter choice or 'm' for menu: ";
  293.     cout << endl;
  294. }
  295.  
  296. void removeFriends(int id)
  297. {
  298.     string name, input;
  299.     bool results;
  300.     bool option = true;
  301.  
  302.     User u = userID[1]; // temporarily assigned to spongebob
  303.  
  304.     while (option)
  305.     {
  306.         bool find = true;
  307.         while (find)
  308.         {
  309.             cout << "Enter the name of the friend you'd like to remove: " << endl;
  310.             cin.ignore();
  311.             getline(cin, name);
  312.             results = u.searchFriend(name);
  313.         if (results == 0)
  314.         {
  315.             cout << "Friend not found. Please try again. \n" << endl;
  316.         }
  317.         else
  318.         {
  319.             u.removeFriend(name);
  320.             u.printFriends();
  321.             find = false;
  322.         }
  323.         }
  324.         bool more = true;
  325.  
  326.         while (more)
  327.         {
  328.             cout << "Would you like to remove more friends? ";;
  329.             cin >> input;
  330.             transform(input.begin(), input.end(), input.begin(), ::toupper);
  331.  
  332.             if (input == "NO")
  333.             {
  334.                 cout << "Back to the main menu. " << endl;
  335.                 mainMenu();
  336.                 more = false;
  337.                 option = false;
  338.             }
  339.             else if (input == "YES")
  340.             {
  341.                 more = false;
  342.             }
  343.             else
  344.             {
  345.                 cout << "Please enter only yes or no " << endl;
  346.             }
  347.         }
  348.     }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement