fork download
  1. //*******************************************************************************************************
  2. //
  3. // File: NameReport.cpp
  4. //
  5. // Student: Anjali Shrestha
  6. //
  7. // Assignment: Program #8
  8. //
  9. // Course Name: Programming II
  10. //
  11. // Course Number: COSC 1560 02
  12. //
  13. // Due: 29th March 2024
  14. //
  15. //
  16. // This program reads from a file.
  17. //
  18. // Other files required:
  19. // 1. ??? (say none if no other files are included)
  20. //
  21. //*******************************************************************************************************
  22.  
  23. #include <iostream>
  24. #include <fstream>
  25. #include <cstring>
  26. #include <string>
  27. using namespace std;
  28.  
  29. //*******************************************************************************************************
  30.  
  31. int getChoice ();
  32. void handleMenu (ifstream & );
  33. void displayFirstFive (ifstream & );
  34. void displayLastFive (ifstream & );
  35. int count (ifstream & );
  36. void addNewNames ();
  37.  
  38. //*******************************************************************************************************
  39.  
  40. int main()
  41. {
  42. ifstream file;
  43. file.open ("names.txt");
  44.  
  45. if (file.fail())
  46. {
  47. cout << "Cannot open file" << endl;
  48. }
  49. else
  50. {
  51. handleMenu(file);
  52. file.close();
  53. }
  54.  
  55. return 0;
  56. }
  57.  
  58. //*******************************************************************************************************
  59.  
  60. int getChoice ()
  61. {
  62. int choice;
  63.  
  64. do
  65. {
  66. cout << string (40, '*') << endl;
  67. cout << "1. Display first five names" << endl
  68. << "2. Display last five names" << endl
  69. << "3. Count the number of names" << endl
  70. << "4. Add a new name" << endl
  71. << "5. Quit" << endl;
  72. cout << string (40, '*') << endl;
  73.  
  74. cout << "\nEnter your choice: ";
  75. cin >> choice;
  76. }
  77. while (choice < 1 || choice > 5);
  78.  
  79. return choice;
  80. }
  81.  
  82. //*******************************************************************************************************
  83.  
  84. void handleMenu (ifstream & fin)
  85. {
  86. const int DISPLAY_FIRST_FIVE = 1,
  87. DISPLAY_LAST_FIVE = 2,
  88. COUNT = 3,
  89. ADD_NEW_NAMES = 4,
  90. QUIT = 5;
  91.  
  92. bool quit = false;
  93.  
  94. while (!quit)
  95. {
  96. int choice = getChoice();
  97.  
  98. switch (choice)
  99. {
  100. case DISPLAY_FIRST_FIVE:
  101. displayFirstFive(fin);
  102. cout << endl;
  103. break;
  104.  
  105. case DISPLAY_LAST_FIVE:
  106. displayLastFive(fin);
  107. cout << endl;
  108. break;
  109.  
  110. case COUNT:
  111. cout << "There are " << count(fin) << " names" << endl;
  112. cout << endl;
  113. break;
  114.  
  115. case ADD_NEW_NAMES:
  116. addNewNames();
  117. cout << endl;
  118. break;
  119.  
  120. case QUIT:
  121. quit = true;
  122. cout << "Over!" << endl;
  123. break;
  124.  
  125. default:
  126. cout << "!!! Choice out of range 1 - 5" << endl;
  127. break;
  128. }
  129. }
  130. }
  131.  
  132. //*******************************************************************************************************
  133.  
  134. void displayFirstFive (ifstream & fin)
  135. {
  136. fin.clear();
  137. fin.seekg(0L, ios::beg);
  138.  
  139. char names[81];
  140.  
  141. for (int i = 0; i < 5; i++)
  142. {
  143. fin.getline(names, 81);
  144. cout << i + 1 << ". " << names << endl;
  145. }
  146. }
  147.  
  148. //*******************************************************************************************************
  149.  
  150.  
  151. int count(ifstream & fin)
  152. {
  153. fin.clear();
  154. fin.seekg(0L, ios::beg);
  155. int count = 0;
  156.  
  157. char names[81];
  158.  
  159. while (fin.getline(names, 81))
  160. {
  161. count++;
  162. }
  163.  
  164. return count;
  165. }
  166.  
  167. //*******************************************************************************************************
  168.  
  169. void displayLastFive(ifstream & fin)
  170. {
  171. fin.clear();
  172. fin.seekg(0L, ios::beg);
  173.  
  174. char names[81];
  175. int totalNumberOfLines = count(fin);
  176.  
  177. for (int i = 0; i < totalNumberOfLines; i++)
  178. {
  179. fin.getline(names, 81);
  180. if ( i >= totalNumberOfLines - 5)
  181. cout << i << ". " << names << endl;
  182. }
  183.  
  184. }
  185.  
  186. //*******************************************************************************************************
  187.  
  188.  
  189. void addNewNames()
  190. {
  191. ofstream fout;
  192. fout.open("names.txt", ios::app);
  193.  
  194. char names[81];
  195.  
  196. if (fout.fail())
  197. {
  198. cout << "Cannot open file." << endl;
  199. }
  200. else
  201. {
  202. cout << "Adding a name" << endl;
  203. cout << "Enter a name: ";
  204. cin.ignore();
  205. cin.get (names, 81);
  206. fout << names << endl;
  207. fout.close();
  208. }
  209. }
  210.  
  211. //*******************************************************************************************************
  212.  
  213. // attach your sample output here.
  214.  
Success #stdin #stdout 0.01s 5152KB
stdin
Standard input is empty
stdout
Cannot open file