Advertisement
Guest User

Untitled

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