fun with structs


/ Published in: C++
Save to your folder(s)



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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.