fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <sstream>
  4. #include <fstream>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8. struct Person {
  9. string firstName;
  10. string lastName;
  11. int birthday;
  12. int phone;
  13. string email;
  14. };
  15. void addData(vector<Person>& v);
  16. void displayData(vector<Person> person);
  17. void displayRaw(Person person);
  18. void sort(vector<Person>& v, string input);
  19. char getMenuResponse();
  20. void save(ofstream& fout, vector<Person>&p);
  21. void read(vector<Person>& p);
  22. int find(vector<Person>& v, string & t );
  23. void remove(vector<Person>&v, string rnames);
  24. int main()
  25. {
  26.  
  27. vector<Person> v;
  28. bool run = true;
  29. string name;
  30. string ch;
  31. ofstream fout;
  32. ifstream fin;
  33. int index;
  34. do {
  35.  
  36. switch (getMenuResponse()) {
  37. case 'A':
  38. system("cls");
  39. addData(v);
  40. break;
  41. case 'F':
  42. cout << "Enter Last Name to find" << endl;
  43. getline(cin, name);
  44. cout << "The index is " ;
  45. index = find(v, name);
  46. cout << index;
  47. break;
  48. case 'D':
  49. system("cls");
  50. cout << "Display the Address Book Records: " << endl;
  51. displayData(v);
  52. break;
  53. case 'S':
  54. system("cls");
  55. cout << "Would you like to sort by Name or Last_Name ?" << endl;
  56. getline(cin, name);
  57. sort(v, name);
  58. cout << endl;
  59. displayData(v);
  60. break;
  61. case 'R':
  62. cout << "Enter the string to remove " << endl;
  63. getline(cin, name);
  64. //remove(v, name);
  65. cout << endl;
  66. displayData(v);
  67. break;
  68. case 'K':
  69. save(fout, v);
  70. break;
  71. case 'G':
  72. read(v);
  73. break;
  74. case 'Q':
  75. run = false;
  76. break;
  77. default:
  78. cout << "That is NOT a valid choice" << endl;
  79. }
  80. } while (run);
  81. cout << endl;
  82. cout << "Thank you for using our program, HAVE A NICE DAY!" << endl;
  83. }
  84. char getMenuResponse()
  85. {
  86. char response;
  87. cout << endl
  88. << "Make your selection" << endl
  89. << "(A)dd Record, (D)isplay name, (F)ind name, (S)ort names, (R)emove name , (K)Save File, (G)et File, (Q)uit" << endl
  90. << "> ";
  91. cin >> response;
  92. cin.ignore(256, '\n');
  93. return toupper(response);
  94. }
  95. void addData(vector<Person>& v)
  96. {
  97.  
  98. while (true) {
  99. Person aPerson;
  100. if (v.size() < 2) {
  101. cout << "Address Book Program - " << v.size() << " Item stored " << endl;
  102. }
  103. else {
  104. cout << "Address Book Program - " << v.size() << " Items stored " << endl;
  105. }
  106.  
  107. cout << "Enter Address Book Records" << endl;
  108. string str;
  109. stringstream ss(str);
  110. string firstName;
  111. cout << "Please enter your first name(quit to stop): " << endl;
  112. getline(cin, firstName);
  113. if (firstName == "quit") {
  114. break;
  115. }
  116. string surname;
  117. cout << "Please enter your last name: ";
  118. getline(cin, surname);
  119. string birthday;
  120. cout << "Please Enter your Date of Birth YYMMDD: ";
  121. getline(cin, birthday);
  122. ss << birthday;
  123. ss >> aPerson.birthday;
  124. string phone;
  125. cout << "Please Enter your Phone Number: ";
  126. getline(cin,phone);
  127. ss << phone;
  128. ss >> aPerson.phone;
  129. ss.clear();
  130. string email;
  131. cout << "Please enter your Email Address: ";
  132. getline(cin, email);
  133. aPerson.firstName = firstName;
  134. aPerson.lastName = surname;
  135. aPerson.email = email;
  136. v.push_back(aPerson);
  137. ss.clear();
  138. }
  139. }
  140. void displayData(vector<Person> v)
  141. {
  142.  
  143. int length = v.size();
  144. for (int x = 0; x < length; x++) {
  145. displayRaw(v[x]);
  146. }
  147.  
  148. if (length < 1) {
  149. cout << "Nothing to display" << endl;
  150. }
  151. }
  152. void displayRaw(Person person)
  153. {
  154. const char separator = ' ';
  155. const int nameWidth = 12;
  156. const int numWidth = 10;
  157. cout << left << setw(nameWidth) << setfill(separator) << "First Name:";
  158. cout << left << setw(nameWidth) << setfill(separator) << "Last Name:";
  159. cout << left << setw(numWidth) << setfill(separator) << "DOB:";
  160. cout << left << setw(numWidth) << setfill(separator) << "Phone:";
  161. cout << left << setw(numWidth) << setfill(separator) << "Email Address:" << endl;
  162. cout << left << setw(nameWidth) << setfill(separator) << person.firstName;
  163. cout << left << setw(nameWidth) << setfill(separator) << person.lastName;
  164. cout << left << setw(numWidth) << setfill(separator) << person.birthday;
  165. cout << left << setw(numWidth) << setfill(separator) << person.phone;
  166. cout << left << setw(numWidth) << setfill(separator) << person.email << endl;
  167. }
  168. void sort(vector<Person>& v, string input)
  169. {
  170. if (input == "Name")
  171. {
  172. cout << " Sorting Address Book by First Name " << endl;
  173. int n = v.size();
  174. for (int i = 1; i < n; i++)
  175. for (int j = 0; j < n - i; j++)
  176. if (v[j].lastName > v[j + 1].lastName)
  177. swap(v[j].firstName, v[j + 1].firstName);
  178. }else if (input == "Last_Name")
  179. {
  180. cout << " Sorting Address Book by Last Name " << endl;
  181. int n = v.size();
  182. for (int i = 1; i < n; i++)
  183. for (int j = 0; j < n - i; j++)
  184. if (v[j].firstName > v[j + 1].firstName)
  185. swap(v[j].lastName, v[j + 1].lastName);
  186. }
  187. }
  188. void save(ofstream& fout, vector<Person>&p)
  189. {
  190. fout.open("addressbook.txt");
  191. if (!fout.fail()) {
  192. system("cls");
  193. cout << "Saving Records to the addressbook.txt file ";
  194. int length = p.size();
  195. for (int x = 0; x < length; x++)
  196. {
  197. fout << p[x].firstName << ";";
  198. fout << p[x].lastName << ";";
  199. fout << p[x].birthday << ";";
  200. fout << p[x].phone << ";";
  201. fout << p[x].email;
  202. if (x<(length-1))
  203. {
  204. fout << endl;
  205. }
  206. }
  207. cout << endl;
  208. cout << length << " records writen to the disc." << endl;
  209. fout.close();
  210. }
  211. }
  212. void read(vector<Person>& p)
  213. {
  214. ifstream fin("addressbook.txt");
  215. if (!fin.fail())
  216. {
  217. system("cls");
  218. cout << "Reading inventory from the disc ";
  219. }
  220. while(!fin.eof())
  221. {
  222.  
  223. Person aPerson;
  224. string str;
  225. stringstream ss(str);
  226. string firstName;
  227. getline(fin, firstName, ';');
  228. string surname;
  229. getline(fin, surname, ';');
  230. string birthday;
  231. getline(fin, birthday, ';');
  232. ss << birthday;
  233. ss >> aPerson.birthday;
  234. string phone;
  235. getline(fin, phone, ';');
  236. ss << phone;
  237. ss >> aPerson.phone;
  238. ss.clear();
  239. string email;
  240. getline(fin, email);
  241. aPerson.firstName = firstName;
  242. aPerson.lastName = surname;
  243. aPerson.email = email;
  244. p.push_back(aPerson);
  245. ss.clear();
  246.  
  247. }
  248. }
  249. int find(vector<Person> &v, string & t )
  250. {
  251. for(int i=0; i< v.size(); i++)
  252. {
  253. if(v[i].lastName ==t)
  254. {
  255. return i;
  256. }
  257. }
  258. // If execution gets here, the string was not found.
  259. return -1;
  260. }
  261. void remove(vector<Person>&v, string rnames)
  262. {
  263. v.erase(remove(v.begin(), v.end(), rnames), v.end());
  264. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:38:25: error: 'system' was not declared in this scope
             system("cls");
                         ^
prog.cpp: In function 'void save(std::ofstream&, std::vector<Person>&)':
prog.cpp:192:21: error: 'system' was not declared in this scope
         system("cls");
                     ^
prog.cpp: In function 'void read(std::vector<Person>&)':
prog.cpp:217:15: error: 'system' was not declared in this scope
   system("cls");
               ^
prog.cpp: In function 'void remove(std::vector<Person>&, std::string)':
prog.cpp:263:43: error: no matching function for call to 'remove(std::vector<Person>::iterator, std::vector<Person>::iterator, std::string&)'
  v.erase(remove(v.begin(), v.end(), rnames), v.end());
                                           ^
In file included from /usr/include/c++/5/cstdio:42:0,
                 from /usr/include/c++/5/fstream:41,
                 from prog.cpp:4:
/usr/include/stdio.h:178:12: note: candidate: int remove(const char*)
 extern int remove (const char *__filename) __THROW;
            ^
/usr/include/stdio.h:178:12: note:   candidate expects 1 argument, 3 provided
prog.cpp:261:6: note: candidate: void remove(std::vector<Person>&, std::string)
 void remove(vector<Person>&v, string rnames)
      ^
prog.cpp:261:6: note:   candidate expects 2 arguments, 3 provided
stdout
Standard output is empty