Advertisement
matthewcole805

Code2

Apr 23rd, 2017
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. /**/
  2. #include <iostream>
  3. #include <cstring>
  4. #include <iomanip>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. const int NO_OF_SONGS = 21;
  10.  
  11. struct songInfo
  12. {
  13. char title[30];
  14. char artist[30];
  15. int durationMin = 0;
  16. int durationSec = 0;
  17. char album[30];
  18. };
  19.  
  20. void addSong (songInfo list[], int listSize);
  21. void removeSong (songInfo list[], int listSize);
  22. void displayList (songInfo list[], int listSize);
  23. void searchList (songInfo list[], int listSize);
  24. void readList (ifstream& infile, songInfo list[], int listSize);
  25. void printList (ofstream& outfile, songInfo list[], int listSize);
  26.  
  27. int main()
  28. {
  29. songInfo trackListing[NO_OF_SONGS];
  30.  
  31. char selection;
  32.  
  33. cout << endl;
  34. cout << "*Music Track Database*" << endl;
  35.  
  36. do
  37. {
  38. ifstream infile("songs.txt");
  39. ofstream outfile("songs.txt");
  40.  
  41. if (!infile)
  42. {
  43. cout << "Cannot open file: songs.txt";
  44. }
  45.  
  46. cout << "Main Menu:" << endl;
  47. cout << endl;
  48. cout << "(a)dd a song" << endl;
  49. cout << "(r)emove a song" << endl;
  50. cout << "(d)isplay track listings" << endl;
  51. cout << "(s)earch for a track" << endl;
  52. cout << "(q)uit" << endl;
  53. cout << endl;
  54. cout << "Select (a, r, d, s, q): ";
  55. cin >> selection;
  56. cout << endl;
  57.  
  58. if (selection != 'a' && selection != 'r' && selection != 'd' && selection != 's' && selection != 'q')
  59. {
  60. cout << "Select (a, r, d, s, q): ";
  61. cin >> selection;
  62. cout << endl;
  63. }
  64.  
  65. switch (selection)
  66. {
  67. case 'a':
  68. readList(infile, trackListing, NO_OF_SONGS);
  69. addSong(trackListing, NO_OF_SONGS);
  70. printList(outfile, trackListing, NO_OF_SONGS);
  71. break;
  72. case 'd':
  73. readList(infile, trackListing, NO_OF_SONGS);
  74. displayList(trackListing, NO_OF_SONGS);
  75. break;
  76. case 'r':
  77. removeSong(trackListing, NO_OF_SONGS);
  78. printList(outfile, trackListing, NO_OF_SONGS);
  79. break;
  80. case 's':
  81. readList(infile, trackListing, NO_OF_SONGS);
  82. searchList(trackListing, NO_OF_SONGS);
  83. break;
  84. }
  85. }while (selection != 'q');
  86.  
  87. cout << "Happy Trails To You; Until We Meet Again!" << endl;
  88. cout << endl;
  89.  
  90. return 0;
  91. }
  92.  
  93. void addSong (songInfo list[], int listSize)
  94. {
  95. int i;
  96. cout << "Add a Song" << endl;
  97. cout << endl;
  98. cout << "Enter Song Number in list: ";
  99. cin >> i;
  100. cout << endl;
  101.  
  102. cout << "Enter Song Title: ";
  103. cin.ignore();
  104. cin.get(list[i].title, 30);
  105. cout << endl;
  106.  
  107. cout << "Enter Artist Name: ";
  108. cin.ignore();
  109. cin.get(list[i].artist, 30);
  110. cout << endl;
  111.  
  112. cout << "Enter Track Duration: ";
  113. cout << endl;
  114. cout << "(Seperate mins & secs with a space.";
  115. cout << endl;
  116. cout << "ex: if 3:40, enter 3 40): ";
  117. cin >> list[i].durationMin >> list[i].durationSec;
  118. cout << endl;
  119.  
  120. cout << "Enter Album Title: ";
  121. cin.ignore();
  122. cin.get(list[i].album, 30);
  123. cout << endl;
  124. cout << "Song Added to Database!";
  125. cout << endl;
  126. cout << endl;
  127. }
  128.  
  129. void readList (ifstream& infile, songInfo list[], int listSize)
  130. {
  131. int i;
  132. infile.open("songs.txt");
  133. while(infile)
  134. {
  135. for(i=1;i<listSize;i++)
  136. {
  137. infile.get(list[i].title, 30);
  138. infile.get(list[i].artist, 30);
  139. infile >> list[i].durationMin >> list[i].durationSec;
  140. infile.get(list[i].album, 30);
  141. }
  142. }
  143. infile.close();
  144. }
  145. void printList (ofstream& outfile, songInfo list[], int listSize)
  146. {
  147. outfile.open("songs.txt");
  148. int i;
  149. for (i=1; i<listSize; i++)
  150. {
  151. outfile << left << setw(2) << i << left << setw(32) << list[i].title << ';' << left << setw(32) << list[i].artist << ';' << right << setw(4) << list[i].durationMin << ';' << left << setw(5) << list[i].durationSec << ';' << left << setw(32) << list[i].album << ';' << endl;
  152. }
  153. outfile.close();
  154. }
  155. void removeSong (songInfo list[], int listSize)
  156. {
  157. int i=1;
  158. int e;
  159. cout << "Remove a Song" << endl;
  160. cout << "Enter Track Number to Confirm Deletion: ";
  161. cin >> e;
  162. cout << endl;
  163.  
  164. for (i=1; i<listSize; i++)
  165. {
  166. if (e==i)
  167. {
  168. for(i=e; i<listSize; i++)
  169. {
  170. list[i] = list[i+1];
  171. }
  172. }
  173. }
  174. }
  175. void displayList (songInfo list[], int listSize)
  176. {
  177. int i;
  178. cout << left << setw(14) << '#' << left << setw(21) << "Song Name" << left << setw(21) << "Artist Name" << left << setw(20) << "Duration" << left << setw(22) << "Album Title" << endl;
  179. cout << left << setw(110) << setfill ('-') << '-' << endl;
  180. cout << setfill (' ');
  181. for (i=1; i<listSize; i++)
  182. {
  183. cout << left << setw(2) << i << left << setw(32) << list[i].title << left << setw(32) << list[i].artist << right << setw(4) << list[i].durationMin << ':' << left << setw(5) << list[i].durationSec << left << setw(32) << list[i].album << endl;
  184. }
  185. cout << left << setw(110) << setfill ('-') << '-' << endl;
  186. cout << setfill (' ');
  187. }
  188. void searchList (songInfo list[], int listSize)
  189. {
  190. char aOrB;
  191. int i = 1;
  192. char art[30];
  193. char alb[30];
  194.  
  195. cout << "Search by (a)rtist or al(b)um? (a or b): ";
  196. cin >> aOrB;
  197. cout << endl;
  198.  
  199. if (aOrB != 'a' && aOrB != 'b')
  200. {
  201. cout << "(a or b): ";
  202. cin >> aOrB;
  203. cout << endl;
  204. }
  205. if (aOrB == 'a')
  206. {
  207. cout << "Artist Name: ";
  208. cin.ignore();
  209. cin.get(art, 30);
  210.  
  211. for(i=1; i<listSize; i++)
  212. {
  213. if (!strcmp(art, list[i].artist))
  214. {
  215. cout << left << setw(2) << i << left << setw(32) << list[i].title << left << setw(32) << list[i].artist << right << setw(4) << list[i].durationMin << ':' << left << setw(5) << list[i].durationSec << left << setw(32) << list[i].album << endl;
  216. }
  217. }
  218. }
  219. else
  220. {
  221. cout << "Album Name: ";
  222. cin.ignore();
  223. cin.get(alb, 30);
  224.  
  225. for(i=1; i<listSize; i++)
  226. {
  227. if (!strcmp(alb, list[i].album))
  228. {
  229. cout << left << setw(2) << i << left << setw(32) << list[i].title << left << setw(32) << list[i].artist << right << setw(4) << list[i].durationMin << ':' << left << setw(5) << list[i].durationSec << left << setw(32) << list[i].album << endl;
  230. }
  231. else
  232. cout << "Album not found" << endl;
  233. }
  234. }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement