Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. /******************************
  2. Cody Hesselgrave
  3. 2/16/2019
  4. PA2_Cody_Hesselgrave.cpp
  5. Description
  6. ********************************/
  7.  
  8. // Headers
  9. #include <iostream>
  10. #include <cstdlib>
  11. #include <string>
  12. #include <vector>
  13. #include <fstream>
  14. #include <algorithm>
  15. using namespace std;
  16.  
  17. // Global variables
  18. const int CREATURE_NAME_SIZE = 30; // Max length of a creature name
  19. const int CREATURE_TYPE_SIZE = 26; // Max length of a creature type
  20. const string FILENAME = "creatures.dat"; // Name of file that contains the data
  21.  
  22. // struct used to create records in file
  23. struct Creature
  24. {
  25. char name[CREATURE_NAME_SIZE]; // Name of the creature
  26. char creatureType[CREATURE_TYPE_SIZE]; // Type of creature
  27. int hp; // Hit points for creature
  28. int armor; // Armor class for creature
  29. int speed; // Speed of creature
  30. };
  31.  
  32. // This function returns true if the name of the left creature is less than the name of the right creature
  33. // Use this function when running the sort function
  34. bool sortByName(const Creature &lhs, const Creature &rhs)
  35. {
  36. string name1(lhs.name), name2(rhs.name);
  37. return name1 < name2;
  38. }
  39.  
  40. // Function declarations
  41. // You will need to code the definitions for each of these functions
  42. int getCreatureNumber(int numCreatures);
  43. void displayCreature(fstream& file, int num);
  44. void displaySorted(fstream& file);
  45.  
  46. int main()
  47. {
  48. char choice; // choice made by user for menu
  49. fstream creatureFile; // file stream for input file
  50. int numCreatures; // the number of creatures saved to the file
  51.  
  52. // Open the creatureFile for input and binary (one statement of code)
  53.  
  54.  
  55. // Get the number of creatures in the file (two statements of code)
  56. // The number of creatures should be assigned to numCreatures
  57.  
  58. do
  59. {
  60. cout << "Menu" << endl;
  61. cout << "1. Display a specific creature\n";
  62. cout << "2. Display all creatures sorted by name\n";
  63. cout << "3. Quit\n";
  64. cout << "Enter your choice (1, 2, 3): ";
  65. cin >> choice;
  66.  
  67. while (cin.get() != '\n');
  68.  
  69. switch (choice)
  70. {
  71. case '1': // Display a specific creature
  72. displayCreature(creatureFile, getCreatureNumber(numCreatures));
  73. break;
  74.  
  75. case '2': // Display all the creatures in order
  76. displaySorted(creatureFile);
  77. break;
  78.  
  79. case '3': // Quit
  80. break;
  81.  
  82. default:
  83. cout << "Invalid option.\n";
  84. break;
  85. }
  86.  
  87. if (choice != '3')
  88. {
  89. system("PAUSE");
  90. system("CLS");
  91. }
  92.  
  93. } while (choice != '3');
  94.  
  95. creatureFile.close();
  96.  
  97.  
  98. // Make sure we place the end message on a new line
  99. cout << endl;
  100.  
  101. // The following is system dependent. It will only work on Windows
  102. system("PAUSE");
  103.  
  104. /*
  105. // A non-system dependent method is below
  106. cout << "Press any key to continue";
  107. cin.get();
  108. */
  109. return 0;
  110. }
  111.  
  112. /*******************************************************************
  113. getCreatureNumber gets and returns the record number from the file that the user would like to be displayed
  114. PARAM: numCreatures should be the value of the total number of records (creatures) in the file
  115. PRE: numCreatures contains a value that is equal to the number of records in the file
  116. POST: A value between 1 and numCreatures is returned as selected by the user
  117. NOTE: Do not allow a value less than 1 or greater than numCreatures to be returned
  118. ********************************************************************/
  119. int getCreatureNumber(int numCreatures)
  120. {
  121.  
  122. }
  123.  
  124. /*******************************************************************
  125. displayCreature displays record number num from file
  126. PARAM: file is a fstream that should be open for input and binary
  127. num contains the record number that is to be read in file
  128. PRE: file is a fstream that is open for input and binary
  129. num is a value between 1 and the number of records in file
  130. POST: The record number num is displayed to the monitor
  131. ********************************************************************/
  132. void displayCreature(fstream& file, int num)
  133. {
  134.  
  135. }
  136.  
  137. /*******************************************************************
  138. displaySorted should read file into a vector. It should then sort the vector by
  139. the name of the creature. Last it should display the vector
  140. PARAM: file is a fstream that should be open for input and binary
  141. PRE: file is open for input and binary
  142. POST: Each record is displayed sorted by the name of the creature
  143. ********************************************************************/
  144. void displaySorted(fstream& file)
  145. {
  146. vector<string> vect;
  147.  
  148. file.open(FILENAME);
  149. if (file.fail)
  150. {
  151. cout << "File not found.";
  152. }
  153. /*
  154. for (int i = 0; i < file.end; i++)
  155. {
  156. vect[i] = file.getline(file, line1[a], '\n');
  157. }
  158. */
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement