Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include "bboard.h"
  4. #include "user.h"
  5. #include "message.h"
  6. #include <cstdlib>
  7.  
  8. using namespace std;
  9.  
  10. //Start of private functions
  11.  
  12. /** Diplays all messages to the user. If there are no messages, "Nothing to
  13.  *  Diplay" will be show, */
  14.  
  15. void Bboard:: message_display()
  16. {
  17.   if (message_list.size() == 0)
  18.   {
  19.     cout <<"\nNothing to Display.\n";
  20.   }
  21.   else
  22.   {
  23.     cout << "\n------------------------------------\n";
  24.     for (unsigned x = 0; x < message_list.size(); x++)
  25.     {
  26.       cout << "Message #" << x+1 << ": ";
  27.       message_list[x].display();
  28.     }
  29.   }
  30. }
  31.  
  32. /** Asks the user for the subject name and body of the message. Once the user
  33.  *  has entered the subject and body, the message gets stored in a vector. */
  34.  
  35. void Bboard:: add_message()
  36. {
  37.   char sub[256], body[256];
  38.   const string author = current_user.get_username();
  39.   cout << "\nEnter Subject: ";
  40.   cin.getline(sub, 256);
  41.   cin.getline(sub, 256);
  42.  
  43.   cout << "Enter Body: ";
  44.   cin.getline(body, 256);
  45.  
  46.   Message msg(author, sub, body);       //Creates a new message named msg
  47.  
  48.   message_list.push_back(msg);
  49.   cout << "Message Recorded!\n";
  50. }
  51.  
  52. //Start of public functions
  53.  
  54. /** default bboard constructor. */
  55.  
  56. Bboard:: Bboard()
  57. :title("The Most Epic Board of All Boards"), user_list(), current_user(User()),
  58.   message_list()
  59. {
  60. }
  61.  
  62. /** bboard constructor that takes in the name of the board as the parameter */
  63.  
  64. Bboard:: Bboard(const string& ttl)
  65. :title(ttl), user_list(), current_user(User()), message_list()
  66. {
  67. }
  68.  
  69. /** reads in a text file with usernames and passwords and stores them in
  70.  *  in a vector. The text file must have a username, space, and a password
  71.  *  on each line. When there are no more users, add "end" under the last
  72.  *  username. */
  73.  
  74. void Bboard:: setup()
  75. {
  76.   string usr;
  77.   string pw;
  78.  
  79.   cin >> usr;
  80.  
  81.   while (usr != "end")
  82.   {
  83.     cin >> pw;
  84.     User new_user(usr, pw);
  85.     user_list.push_back(new_user);
  86.     cin >> usr;
  87.   }
  88. }
  89.  
  90. /** prompts the user to enter thair username and password. If one of them is
  91.  *  invalid, the user will have to enter their username and password again. */
  92.  
  93. void Bboard:: login()
  94. {
  95.   string usr;
  96.   string pw;
  97.   bool real_user = false;  //Will become true if the user is valid.
  98.  
  99.   cout << "Enter your username (\'Q\' or \'q\' to quit): ";
  100.   cin >> usr;
  101.  
  102.   if (usr == "Q" || usr == "q")
  103.   {
  104.     cout << "Bye!" << endl;
  105.     exit(0);
  106.   }
  107.  
  108.   cout << "Enter your password: ";
  109.   cin >> pw;
  110.  
  111.   for (unsigned x = 0; x < user_list.size(); x++)
  112.   {
  113.     if (user_list[x].check(usr, pw))
  114.     {
  115.       current_user = user_list[x];
  116.       cout << "\nWelcome back " << current_user.get_username() << "!\n";
  117.       real_user = true;
  118.     }
  119.   }
  120.   if (!real_user)
  121.   {
  122.     cout << "Invalid Username or Password!\n" << endl;
  123.     login();
  124.   }
  125. }
  126.  
  127. /** prompts the user with the menu of the board. The user can quit, add a new
  128.  *  message, or display all messages. */
  129.  
  130. void Bboard:: run()
  131. {
  132.   char choice;
  133.    
  134.   cout << "\nMenu\n";
  135.   cout << "  - Display Messages (\'D\' or \'d\')\n";
  136.   cout << "  - Add New Message (\'N\' or \'n\')\n";
  137.   cout << "  - Quit (\'Q\' or \'q\')\n";
  138.   cout << "Choose an action: ";
  139.   cin >> choice;
  140.  
  141.   if (choice == 'D' || choice == 'd')
  142.   {
  143.     message_display();
  144.     run();
  145.   }
  146.   else if (choice == 'N' || choice == 'n')
  147.   {
  148.     add_message();
  149.     run();
  150.   }
  151.   else if (choice == 'Q' || choice == 'q')
  152.   {
  153.     cout << "Bye!\n";
  154.     exit(0);
  155.   }
  156.   else
  157.   {
  158.     cout << "\nInvalid entry\n";
  159.     run();
  160.   }
  161. }
  162.  
  163. /** returns the title of the bboard */
  164.  
  165. string Bboard:: get_title()
  166. {
  167.   return title;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement