Posted By

NerdGnome on 03/14/11


Tagged


Versions (?)

final


 / Published in: C++
 

oh gawd. the extra two functions were just there for reference. very incomplete. Also, there are inconsistencies in the variables simply because I'm pulling these straight from my notes. Still trying to figure out how to apply it to the assignment.

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. struct temp
  8. {
  9. string temp;
  10. };
  11.  
  12. struct Book
  13. {
  14. string title;
  15. string author;
  16. };
  17.  
  18. const int ARRAY_SIZE = 1000;
  19. Book books [ARRAY_SIZE];
  20.  
  21.  
  22. // Function prototypes
  23. int loadData(string pathName);
  24. void showAll(int count);
  25. void showBooksByAuthor (int count);
  26. void showBooksByTitle (int count);
  27. void sortByTitle (int count);
  28. void sortByAuthor (int count);
  29. void swap(int , int );
  30.  
  31.  
  32. int main()
  33. {
  34. char choice = ' ';
  35. ifstream inFile;
  36. int count = 0;
  37.  
  38. string fileName;
  39.  
  40.  
  41. cout << "Welcome to Patrick’s Library Database.'/n' Please enter the name of the backup file:" << endl;
  42. getline(cin, fileName);
  43. if ( loadData(fileName) == -1 )
  44. {
  45. cout << "failed to load from file" << endl;
  46. return 0;
  47. }
  48. count = loadData(fileName);
  49. if(count < 1)
  50. loadData(fileName);
  51.  
  52. while(choice != 'q' || choice != 'Q')
  53. {
  54. cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All:" << endl;
  55. cin >> choice;
  56. if(choice == 'q' || choice == 'Q')
  57. return 0;
  58. else if(choice == 'a' || choice == 'A')
  59. {
  60. sortByAuthor (count);
  61. showBooksByAuthor (count);
  62. }
  63. else if(choice == 't' || choice == 'T')
  64. {
  65. sortByTitle (count);
  66. showBooksByTitle (count);
  67. }
  68. else if(choice == 's' || choice == 'S')
  69. showAll(count);
  70. else
  71. {
  72. cout << "Invalid request. '/n' Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: " << endl;
  73. cin >> choice;
  74. }
  75. }
  76.  
  77. inFile.close();
  78. //closes both files
  79.  
  80. return 0;
  81. }
  82.  
  83. int loadData(string pathName)
  84. {
  85. ifstream inFile;
  86. int i;
  87.  
  88.  
  89.  
  90. inFile.open(pathName);
  91. if(!inFile)
  92. {
  93.  
  94. return -1;
  95. }
  96. else if(inFile)
  97. {
  98. for(i = 0; !inFile.eof() && i < ARRAY_SIZE ; i++)
  99. {
  100. getline(inFile, books[i].title);
  101. getline(inFile, books[i].author);
  102. }
  103. return i;
  104. }
  105. return 0;
  106.  
  107. }
  108.  
  109. void showAll(int count)
  110. {
  111. for (int i = 0; i < count; i++)
  112. {
  113. cout << books[i].title << " by " << books[i].author << endl;
  114. }
  115.  
  116. }
  117.  
  118. void showBooksByAuthor (int count)
  119. {
  120. string author;
  121. cout << "Enter name or partial name of author: " << endl;
  122. cin >> author;
  123.  
  124. for(int i = 0; i < count; i++)
  125. {
  126. if ( books[i].author.find(author) != -1 )
  127. {
  128. cout << books[i].author << " " << books[i].title << endl;
  129. }
  130.  
  131. }
  132.  
  133. }
  134. void showBooksByTitle (int count)
  135. {
  136. string title;
  137. cout << "Enter name or partial name of title: " << endl;
  138. cin >> title;
  139. for(int i = 0; i < count; i++)
  140. {
  141. if ( books[i].title.find(title) != -1 )
  142. {
  143. cout << books[i].title << " " << books[i].author << endl;
  144. }
  145.  
  146. }
  147. }
  148.  
  149.  
  150. void sortByTitle (int count)
  151. {
  152. int i, j, minIndex ;
  153.  
  154. for(i = 0; i < count - 1; i++)
  155. {
  156. minIndex = i;
  157. for(j = i + 1; j < count; j++)
  158. {
  159. if( books[j].title.compare( books[minIndex].title) < 0)
  160. minIndex = j;
  161. }
  162. swap(i, minIndex);
  163.  
  164. }
  165. }
  166.  
  167. void sortByAuthor (int count)
  168. {
  169. int i, j, minIndex ;
  170.  
  171. for(i = 0; i < count - 1; i++)
  172. {
  173. minIndex = i;
  174. for(j = i + 1; j < count; j++)
  175. {
  176. if( books[j].author.compare( books[minIndex].author) < 0)
  177. minIndex = j;
  178. }
  179. swap(i, minIndex);
  180.  
  181. }
  182. }
  183.  
  184. void swap(int i, int minIndex)
  185. {
  186. Book temp;
  187.  
  188. temp.title = books[i].title;
  189. temp.author = books[i].author;
  190. books[i].title = books[minIndex].title;
  191. books[i].author = books[minIndex].author;
  192. books[minIndex].title = temp.title;
  193. books[minIndex].author = temp.author;
  194. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: the_coder on March 20, 2011

if( books[j].title.compare( books[minIndex].title) < 0) should be written as if( books[j].title < books[minIndex].title)

You need to login to post a comment.