Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.38 KB | None | 0 0
  1. //David SoucieGarza. Lab 4
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <string>
  6. using namespace std;
  7.  
  8. //Defines CD Class
  9. class CD {
  10.  
  11. private:
  12. //Private Class Attributes
  13. string Artist;
  14. string Title;
  15. int yearReleased;
  16. int numTracks;
  17. string *Tracks
  18.  
  19. public:
  20. //Public Accessor function prototypes
  21. string getArtist();
  22. string getTitle();
  23. int getYearReleased();
  24. int getNumTracks();
  25. string getTrack(int);
  26.  
  27. //Public Mutator function prototypes
  28. void setArtist (string);
  29. void setTitle (string);
  30. void setYearReleased (int);
  31. void setNumTracks(int);
  32. void setTrack(int, string);
  33.  
  34. //Constructor function protype
  35. CD();
  36.  
  37. };
  38.  
  39. /*********************************************/
  40. /* Function: getArtist
  41. /* Inputs: None
  42. /* Outputs: The Artist string
  43. /* Purpose: This function is the accesor for the
  44. /* Artist string of the CD object
  45. /*********************************************/
  46. string CD::getArtist() {
  47. return Artist;
  48. }
  49.  
  50. /*********************************************/
  51. /* Function: getTitle
  52. /* Inputs: None
  53. /* Outputs: The Title string
  54. /* Purpose: This function is the accesor for the
  55. /* Title string of the CD object
  56. /*********************************************/
  57. string CD::getTitle() {
  58. return Title;
  59. }
  60.  
  61. /*********************************************/
  62. /* Function: getYearReleased
  63. /* Inputs: None
  64. /* Outputs: The yearReleased string
  65. /* Purpose: This function is the accesor for the
  66. /* Year_Released string of the CD object
  67. /*********************************************/
  68. int CD::getYearReleased() {
  69. return yearReleased;
  70. }
  71.  
  72. /*********************************************/
  73. /* Function: getNumTracks
  74. /* Inputs: None
  75. /* Outputs: The numTracks string
  76. /* Purpose: This function is the accesor for the
  77. /* Year_Released string of the CD object
  78. /*********************************************/
  79. int CD::getNumTracks() {
  80. return numTracks;
  81. }
  82.  
  83. /*********************************************/
  84. /* Function: getTracks
  85. /* Inputs: int T
  86. /* Outputs: the T track in the Tracks Array
  87. /* Purpose: This function is the accesor for the
  88. /* Tracks string of the CD object
  89. /*********************************************/
  90. string CD::getTrack(int T) {
  91. return Tracks[T];
  92. }
  93.  
  94. /*********************************************/
  95. /* Function: setArtist
  96. /* Inputs: String A
  97. /* Outputs: None
  98. /* Purpose: This function is the mutator for the
  99. /* Artist string of the CD object
  100. /*********************************************/
  101. void CD::setArtist (string A) {
  102. Artist = A;
  103. }
  104.  
  105. /*********************************************/
  106. /* Function: setTitle
  107. /* Inputs: String T
  108. /* Outputs: None
  109. /* Purpose: This function is the mutator for the
  110. /* Title string of the CD object
  111. /*********************************************/
  112. void CD::setTitle (string T) {
  113. Title = T;
  114. }
  115.  
  116. /*********************************************/
  117. /* Function: setYearReleased
  118. /* Inputs: String Y
  119. /* Outputs: None
  120. /* Purpose: This function is the mutator for the
  121. /* Year_Released string of the CD object
  122. /*********************************************/
  123. void CD::setYearReleased (int Y) {
  124. yearReleased = Y;
  125. }
  126.  
  127. /*********************************************/
  128. /* Function: setNumTracks
  129. /* Inputs: String numOfTracks
  130. /* Outputs: None
  131. /* Purpose: This function is the mutator for the
  132. /* setNumTracks string of the CD object
  133. /*********************************************/
  134. void CD::setNumTracks (int numOfTracks) {
  135. numTracks = numOfTracks;
  136. Tracks[numTracks];
  137. }
  138.  
  139. /*********************************************/
  140. /* Function: setTrack
  141. /* Inputs: int trackIndex, string trackName
  142. /* Outputs: None
  143. /* Purpose: This function is the mutator for the
  144. /* Tracks string array of the CD object
  145. /*********************************************/
  146. void CD::setTrack(int trackIndex, string track) {
  147. cout << "Track slot: " << trackIndex << endl;
  148. Tracks[trackIndex] = track;
  149. }
  150.  
  151. /*********************************************/
  152. /* Function: CD
  153. /* Inputs: None
  154. /* Outputs: N/A, returns 0
  155. /* Purpose: This is the default Constructor for
  156. /* the CD object
  157. /*********************************************/
  158. CD::CD() {
  159. Artist = "None";
  160. Title = "None";
  161. yearReleased = 0;
  162. numTracks = 0;
  163. Tracks[0];
  164. }
  165.  
  166. //Function Prototypes
  167. void testCD();
  168.  
  169. void readCollection(CD [], int);
  170. void displayCollection(CD [], int);
  171. void sortCollection(CD [], int);
  172. void findCD(CD [], int);
  173. void editCD(CD [], int);
  174. void editFoundCD(CD *cd);
  175. void addCDTracks(CD [], int);
  176. void deleteCDTracks(CD [], int);
  177. void mathBreak();
  178. int evenOdd();
  179. int primeNumber();
  180. bool isEven(int);
  181. bool isPrime(int);
  182.  
  183. /*********************************************/
  184. /* Function: main
  185. /* Inputs: Main menu choice
  186. /* Outputs: N/A, returns 0
  187. /* Purpose: This function displays the main menu
  188. /*********************************************/
  189. int main() {
  190.  
  191. //define vars for while loops and user choice
  192. bool mainQuit = false;
  193. CD cdCollection[17];
  194. bool collectionRead = false;
  195. int mainChoice;
  196.  
  197. //Opening statement to enter the program and initially prompt user
  198. cout << "Welcome to the CD Reader 2000, please select one of the options below.\n";
  199. while (mainQuit == false) {
  200.  
  201. //Options for the CD Reader
  202.  
  203. cout << "Main Menu" << endl;
  204. cout << "___________________" << endl;
  205. cout << "1. Read Collection." << endl;
  206. cout << "2. Display Collection." << endl;
  207. cout << "3. Sort Collection." << endl;
  208. cout << "4. Find a CD." << endl;
  209. cout << "5. Edit a CD." << endl;
  210. cout << "6. Find a Track." << endl;
  211. cout << "7. Delete Tracks from a CD." << endl;
  212. cout << "8. Math Break." << endl;
  213. cout << "9. Quit." << endl;
  214.  
  215. //User input
  216. cin >> mainChoice;
  217.  
  218. //If statements redirects the user to each function or ends the program
  219. if (mainChoice == 1) {
  220. readCollection(cdCollection, 17);
  221. collectionRead = true;
  222. }
  223. else if (mainChoice == 2) {
  224. if (collectionRead == false)
  225. cout << "Make sure to read a collection first." << endl;
  226. else
  227. displayCollection(cdCollection, 17);
  228. }
  229. else if (mainChoice == 3) {
  230. if (collectionRead == false)
  231. cout << "Make sure to read a collection first." << endl;
  232. else
  233. sortCollection(cdCollection, 17);
  234. }
  235. else if (mainChoice == 4) {
  236. if (collectionRead == false)
  237. cout << "Make sure to read a collection first." << endl;
  238. else
  239. findCD(cdCollection, 17);
  240. }
  241. else if (mainChoice == 5) {
  242. if (collectionRead == false)
  243. cout << "Make sure to read a collection first." << endl;
  244. else
  245. editCD(cdCollection, 17);
  246. }
  247. else if (mainChoice == 6) {
  248. if (collectionRead == false)
  249. cout << "Make sure to read a collection first." << endl;
  250. else
  251. addCDTracks(cdCollection, 17);
  252. }
  253. else if (mainChoice == 7) {
  254. if (collectionRead == false)
  255. cout << "Make sure to read a collection first." << endl;
  256. else
  257. deleteCDTracks(cdCollection, 17);
  258. }
  259. else if (mainChoice == 8) {
  260. mathBreak();
  261. }
  262. else if (mainChoice == 9) {
  263. mainQuit = true;
  264. cout << "Exiting program.\n";
  265. }
  266. //Invalid input redirects user to the original menu
  267. else {
  268. cout << "Invalid input, please try again.\n";
  269. }
  270. }
  271. return 0;
  272. }
  273.  
  274. /*********************************************/
  275. /* Function: testCD
  276. /* Inputs: None
  277. /* Outputs: None
  278. /* Purpose: This is a test function which creates a
  279. /* default CD and sets and displays test values
  280. /*********************************************/
  281. void testCD() {
  282.  
  283. //Creates CD Object
  284. CD testCD;
  285.  
  286. //Indicates function call
  287. cout << "Test CD created." << endl;
  288.  
  289. //Sets values on Test CD
  290. testCD.setArtist("Nothing");
  291. testCD.setTitle("Nothing");
  292. testCD.setYearReleased(0);
  293.  
  294. //Displays values to the console
  295.  
  296. cout << "Artist: " << testCD.getArtist() << endl;
  297. cout << "Title: " << testCD.getTitle() << endl;
  298. cout << "Artist: " << testCD.getYearReleased() << endl;
  299. cout << "Returning to the main menu." << endl;
  300.  
  301. }
  302.  
  303. /*********************************************/
  304. /* Function: ReadCollection
  305. /* Inputs: cdCollection CD Array, size integer
  306. /* Outputs: None
  307. /* Purpose: This function reads the data from a file
  308. /* to the cdCollection CD array
  309. /*********************************************/
  310. void readCollection (CD cdCollection[], int size) {
  311.  
  312. string fileChoice;
  313. ifstream fileReader;
  314. string line;
  315. int year;
  316. int count = 0;
  317.  
  318.  
  319. fileReader.open("CD.txt");
  320.  
  321. if (fileReader) {
  322.  
  323. cout << "Reading..." << endl;
  324.  
  325. for (int i = 0; i < size; i++) {
  326.  
  327. getline(fileReader, line);
  328. cdCollection[i].setArtist(line);
  329.  
  330. getline(fileReader, line);
  331. cdCollection[i].setTitle(line);
  332.  
  333. fileReader >> year;
  334. cdCollection[i].setYearReleased(year);
  335. fileReader.ignore();
  336.  
  337. }
  338.  
  339. cout << "Collection read." << endl << endl;
  340. fileReader.close();
  341. }
  342. else {
  343. cout << "No file found." << endl << endl;
  344. }
  345.  
  346.  
  347. }
  348.  
  349. /*********************************************/
  350. /* Function: displayCollection
  351. /* Inputs: cdCollection CD Array, size integer
  352. /* Outputs: None
  353. /* Purpose: This functions displays the
  354. /* cdCollection CD array
  355. /*********************************************/
  356. void displayCollection(CD cdCollection[], int size) {
  357.  
  358. //Indicates program action
  359. cout << "Displaying collection..." << endl;
  360.  
  361. //Displays column headings
  362. cout << fixed << left << setw(30) << "Artist" << setw(30) << "Title" << setw(30) << "Year" << endl;
  363.  
  364. for (int count=0; count < size; count++) {
  365.  
  366. //Displays each object in array
  367. cout << fixed << left << setw(30) << cdCollection[count].getArtist() <<
  368. setw(30) << cdCollection[count].getTitle() <<
  369. setw(30) << cdCollection[count].getYearReleased() << endl;
  370.  
  371. //Displays any tracks a CD has
  372. if (cdCollection[count].getNumTracks() > 0) {
  373. for (int i=0; i < cdCollection[count].getNumTracks(); i++)
  374. cout << " " << i + 1 << ". " << cdCollection[count].getTrack(i) << endl;
  375. }
  376.  
  377. }
  378.  
  379. //Indicates program action
  380. cout << "Collection displayed." << endl << endl;
  381.  
  382. }
  383.  
  384. /*********************************************/
  385. /* Function: sortCollection
  386. /* Inputs: cdCollection CD Array, size integer
  387. /* Outputs: None
  388. /* Purpose: This functions sorts the cdCollection
  389. /* array via artist
  390. /*********************************************/
  391. void sortCollection(CD cdCollection[], int length) {
  392.  
  393. //intialize saving object
  394. CD oldCD;
  395.  
  396. //Indicate program action
  397. cout << "Sorting collection..." << endl;
  398.  
  399. //For loops parse every object through the array
  400. for (int i = 0; i < length; i++) {
  401.  
  402. for (int j = 0; j < length; j++) {
  403.  
  404. if (cdCollection[i].getArtist() < cdCollection[j].getArtist()) {
  405.  
  406. //If the parsing ojbect is smaller than the parsed object, they swap array location
  407. oldCD = cdCollection[i];
  408. cdCollection[i] = cdCollection[j];
  409. cdCollection[j] = oldCD;
  410.  
  411. }
  412. }
  413. }
  414.  
  415. //Indicate program action
  416. cout << "Collection sorted." << endl << endl;
  417. }
  418.  
  419. /*********************************************/
  420. /* Function: findCD
  421. /* Inputs: cdCollection CD Array, size integer
  422. /* Outputs: None
  423. /* Purpose: This functions prompts for user input
  424. /* and searches for matching object
  425. /*********************************************/
  426. void findCD(CD cdCollection[], int size) {
  427.  
  428. //Intialize variables
  429. string artist;
  430. string title;
  431. bool inCollection = false;
  432. bool search = true;
  433. string searchQuery;
  434.  
  435. //While loop runs until user exits
  436. while (search) {
  437.  
  438. //Redefines variable for album search.
  439. //NOTE: I believe this fixes the issue with lab 4, as previously if a single record was found the boolean would made true and never redefined
  440. inCollection = false;
  441.  
  442. //Prompts user for input, obtains only user-entered line
  443. cout << "Select an artist to search for." << endl;
  444. getline(cin, artist);
  445. while (artist.length()==0)
  446. getline(cin, artist);
  447.  
  448.  
  449. cout << "Select an album title to search for." << endl;
  450. getline(cin, title);
  451. while (title.length()==0 )
  452. getline(cin, title);
  453.  
  454. //For Loop parses user inputs into array, checks for matching object
  455. for (int i = 0; i < size; i++) {
  456.  
  457. if (cdCollection[i].getArtist() == artist && cdCollection[i].getTitle() == title) {
  458. inCollection = true;
  459. }
  460.  
  461. }
  462.  
  463. //decision statement dispalys whether or not matching object was found to user inputs
  464. if (inCollection) { cout << "CD found in collection." << endl;}
  465. else { cout << "CD not found in collection." << endl;}
  466.  
  467. //Prompts user to exit function
  468. cout << "Continue searching for albums? (y/n)" << endl;
  469. cin >> searchQuery;
  470. if (searchQuery == "n") {
  471. cout << "Returning to main menu." << endl << endl;
  472. search = false;
  473. }
  474.  
  475. }
  476. }
  477.  
  478. /*********************************************/
  479. /* Function: editCD
  480. /* Inputs: cdCollection CD Array, size integer
  481. /* Outputs: None
  482. /* Purpose: This functions allows the user to edit
  483. /* a cd object of their choice in the collection
  484. /*********************************************/
  485. void editCD(CD cdCollection[], int size) {
  486.  
  487. //Initialize variables
  488. string artist;
  489. string title;
  490. bool inCollection = false;
  491. bool search = true;
  492. bool edit = true;
  493. int searchedCD;
  494. CD *foundCD;
  495.  
  496.  
  497. //Redefines variables for album search
  498. artist = "";
  499. title = "";
  500. inCollection = false;
  501.  
  502. //User prompt
  503. cout << "Select a CD to edit (Enter artist first, then album, 'quit' to quit): " << endl;
  504.  
  505. //Gets first user input
  506. getline(cin, artist);
  507. while (artist.length()==0)
  508. getline(cin, artist);
  509.  
  510. //Exits function if user quits
  511. if (artist == "quit") {
  512.  
  513. cout << "Returning to main menu." << endl;
  514. search = false;
  515.  
  516. }
  517.  
  518. else {
  519.  
  520. //Gets second user input
  521. getline(cin, title);
  522. while (title.length()==0)
  523. getline(cin, title);
  524.  
  525. //Searches for album in collection
  526. cout << "Searching..." << endl;
  527. for (int i = 0; i < size; i++) {
  528.  
  529. if (cdCollection[i].getArtist() == artist && cdCollection[i].getTitle() == title) {
  530. inCollection = true;
  531. foundCD = &cdCollection[i];
  532. }
  533.  
  534. }
  535.  
  536. //If album is found, it can be edited
  537. if (inCollection) {
  538.  
  539. cout << "editing CD..." << endl;
  540. editFoundCD(foundCD);
  541. }
  542. //Redirects user if CD is not found
  543. else
  544. cout << "CD not found, please try again.";
  545.  
  546.  
  547. }
  548.  
  549.  
  550.  
  551.  
  552. }
  553.  
  554.  
  555.  
  556.  
  557. void editFoundCD(CD *FoundCD) {
  558.  
  559. int editChoice;
  560. string newArtist;
  561. string newTitle;
  562. int newYearReleased;
  563. string continueQuery;
  564. bool edit = true;
  565.  
  566. //Redefines variables for CD edit
  567. edit = true;
  568.  
  569. cout << "Entering CD Edit" << endl;
  570. //Outputs CD title
  571. cout << "CD found: " << FoundCD->getArtist() << " - " << FoundCD->getTitle() << "(" << FoundCD->getYearReleased() << ")" << endl;
  572.  
  573. while (edit) {
  574.  
  575. //Menu for editing selection
  576. cout << "Select an option to edit (0 to quit):" << endl;
  577. cout << "1. Edit artist." << endl;
  578. cout << "2. Edit album title." << endl;
  579. cout << "3. Edit year of release." << endl;
  580. cin >> editChoice;
  581.  
  582. //0 results in quit
  583. if (editChoice == 0) {
  584.  
  585. edit = false;
  586. cout << "Returning to main menu." << endl;
  587.  
  588. }
  589. else {
  590.  
  591. //Switch for user input
  592. switch (editChoice) {
  593. case 1:
  594.  
  595. //Obtains user input for new artist
  596. cout << "Select a new artist:" << endl;
  597. getline(cin, newArtist);
  598. while (newArtist.length()==0)
  599. getline(cin, newArtist);
  600.  
  601. //Sets the current record to the new artist
  602. FoundCD->setArtist(newArtist);
  603. cout << "CD has been edited." << endl;
  604.  
  605. break;
  606. case 2:
  607.  
  608. //Obtains user input for new album title
  609. cout << "Select a new album title:" << endl;
  610. getline(cin, newTitle);
  611. while (newTitle.length()==0)
  612. getline(cin, newTitle);
  613.  
  614. //Sets the current record to the new album title
  615. FoundCD->setTitle(newTitle);
  616. cout << "CD has been edited" << endl;
  617.  
  618.  
  619. break;
  620. case 3:
  621.  
  622. //Obtains user input for new year of release
  623. cout << "Select a new year of release:" << endl;
  624. cin >> newYearReleased;
  625.  
  626. //Sets the current record to the new year of release
  627. FoundCD->setYearReleased(newYearReleased);
  628. cout << "CD has been edited" << endl;
  629.  
  630. break;
  631. }
  632.  
  633.  
  634. }
  635. }
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642. }
  643.  
  644. /*********************************************/
  645. /* Function: addCDTracks
  646. /* Inputs: CD cdCollection array, int size
  647. /* Outputs: N/A
  648. /* Purpose: This function allows the user to add
  649. /* tracks to a CD
  650. /*********************************************/
  651. void addCDTracks(CD cdCollection[], int size) {
  652.  
  653. //Initialize variables
  654. string artist;
  655. string title;
  656. bool inCollection = false;
  657. bool search = true;
  658. bool edit = true;
  659. int foundCD;
  660. int addedTracks;
  661. string trackName;
  662.  
  663.  
  664. //Redefines variables for album search
  665. artist = "";
  666. title = "";
  667. inCollection = false;
  668.  
  669. //User prompt
  670. cout << "Select a CD to edit (Enter artist first, then album, 'quit' to quit): " << endl;
  671.  
  672. //Gets first user input
  673. getline(cin, artist);
  674. while (artist.length()==0)
  675. getline(cin, artist);
  676.  
  677. //Exits function if user quits
  678. if (artist == "quit") {
  679.  
  680. cout << "Returning to main menu." << endl;
  681. search = false;
  682.  
  683. }
  684.  
  685. else {
  686.  
  687. //Gets second user input
  688. getline(cin, title);
  689. while (title.length()==0)
  690. getline(cin, title);
  691.  
  692. //Searches for album in collection
  693. cout << "Searching..." << endl;
  694. for (int i = 0; i < size; i++) {
  695.  
  696. if (cdCollection[i].getArtist() == artist && cdCollection[i].getTitle() == title) {
  697. inCollection = true;
  698. foundCD = i;
  699. }
  700.  
  701. }
  702.  
  703. //If album is found, tracks can be added
  704. if (inCollection) {
  705.  
  706. cout << "CD found: " << cdCollection[foundCD].getArtist() << " - " << cdCollection[foundCD].getTitle() << "(" << cdCollection[foundCD].getYearReleased() << ")" << endl;
  707.  
  708. cout << "How many tracks would you like to add?" << endl;
  709. cin >> addedTracks;
  710. cdCollection[foundCD].setNumTracks(addedTracks);
  711.  
  712.  
  713. for (int j = 0; j < cdCollection[foundCD].getNumTracks(); j++) {
  714. trackName = "";
  715. cout << "Name of Track " << j << ":" << endl;
  716. while (trackName.length() == 0)
  717. getline(cin, trackName);
  718. cdCollection[foundCD].setTrack(j, trackName);
  719. }
  720. cout << "All Tracks:" << endl;
  721. for (int k = 0; k < cdCollection[foundCD].getNumTracks(); k++) {
  722. cout << " " << k + 1 << ". " << cdCollection[foundCD].getTrack(k) << endl;
  723. }
  724.  
  725.  
  726. }
  727. //Redirects user if CD is not found
  728. else
  729. cout << "CD not found, please try again.";
  730.  
  731.  
  732. }
  733. }
  734.  
  735.  
  736. void deleteCDTracks(CD cdCollection[], int size) {
  737.  
  738. //Initialize variables
  739. string artist;
  740. string title;
  741. bool inCollection = false;
  742. bool search = true;
  743. bool edit = true;
  744. int searchedCD;
  745. int foundCD;
  746.  
  747.  
  748. //Redefines variables for album search
  749. artist = "";
  750. title = "";
  751. inCollection = false;
  752.  
  753. //User prompt
  754. cout << "Select a CD to edit (Enter artist first, then album, 'quit' to quit): " << endl;
  755.  
  756. //Gets first user input
  757. getline(cin, artist);
  758. while (artist.length()==0)
  759. getline(cin, artist);
  760.  
  761. //Exits function if user quits
  762. if (artist == "quit") {
  763.  
  764. cout << "Returning to main menu." << endl;
  765. search = false;
  766.  
  767. }
  768.  
  769. else {
  770.  
  771. //Gets second user input
  772. getline(cin, title);
  773. while (title.length()==0)
  774. getline(cin, title);
  775.  
  776. //Searches for album in collection
  777. cout << "Searching..." << endl;
  778. for (int i = 0; i < size; i++) {
  779.  
  780. if (cdCollection[i].getArtist() == artist && cdCollection[i].getTitle() == title) {
  781. inCollection = true;
  782. foundCD = i;
  783. }
  784.  
  785. }
  786.  
  787. //If album is found, tracks are checked
  788. if (inCollection && cdCollection[foundCD].getNumTracks() > 0) {
  789.  
  790. cdCollection[foundCD].setNumTracks(0);
  791.  
  792. }
  793. //Redirects user if CD is not found or does not contain tracks
  794. else
  795. cout << "CD not found, or does not contain tracks, please try again.";
  796.  
  797.  
  798. }
  799.  
  800.  
  801.  
  802. }
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810. /*********************************************/
  811. /* Function: mathBreak
  812. /* Inputs: Math break menu choices
  813. /* Outputs: N/A, returns 0
  814. /* Purpose: This function displays the Math Break menu
  815. /* and redirects to the math functions
  816. /*********************************************/
  817. void mathBreak() {
  818.  
  819. //define vars for while loops and user choice
  820. bool mathBreakQuit = false;
  821. int mathBreakChoice;
  822.  
  823. //Prompt user
  824. cout << "Welcome to your math break, please select one of the options below.\n";
  825.  
  826. //While loop for math break menu
  827. while (mathBreakQuit == false) {
  828.  
  829. //Options for the math break
  830. cout << "1. Even or Odd.\n";
  831. cout << "2. Prime Number.\n";
  832. cout << "3. Quit.\n";
  833.  
  834. //User input
  835. cin >> mathBreakChoice;
  836.  
  837. //If statements redirect user to math functions or end the program
  838. if (mathBreakChoice == 1) {
  839. evenOdd();
  840. }
  841. else if (mathBreakChoice == 2) {
  842. primeNumber();
  843. }
  844. else if (mathBreakChoice == 3) {
  845. cout << "Returning to main menu.\n";
  846. mathBreakQuit = true;
  847. }
  848.  
  849. //Invalid input redirects user to original menu
  850. else if (mathBreakChoice < 1 || mathBreakChoice > 3) {
  851. cout << "Invalid input, please try again.\n";
  852. }
  853.  
  854. }
  855.  
  856.  
  857. }
  858.  
  859. /*********************************************/
  860. /* Function: evenOdd
  861. /* Inputs: integer to test
  862. /* Outputs: true if even, false if not even
  863. /* Purpose: This function takes one integer input
  864. /* and determines if it is prime or not.
  865. /*********************************************/
  866. int evenOdd() {
  867.  
  868. //define vars for while loops and user choice
  869. int evenOddInput;
  870. bool evenOddQuit = false;
  871.  
  872. //While loop for integer prompt
  873. while (evenOddQuit == false) {
  874.  
  875. //Prompt user for integer
  876. cout << "Enter a positive integer. (0 to quit)\n";
  877.  
  878. //User input
  879. cin >> evenOddInput;
  880.  
  881. //if statement insures input is valid
  882. if (evenOddInput < 0) {
  883. cout << "Invalid input, please try again.\n";
  884. }
  885.  
  886. //else if statement runs input through isEven function or returns user to math break menu
  887. else if (evenOddInput == 0) {
  888. evenOddQuit = true;
  889. cout << "Returning to math break menu.\n";
  890. }
  891. else if (isEven(evenOddInput) == true) {cout << "The number you entered is even.\n";}
  892. else {cout << "The number you entered is odd.\n";}
  893. }
  894.  
  895. return 0;
  896. }
  897.  
  898.  
  899. /*********************************************/
  900. /* Function: isEven
  901. /* Inputs: integer from evenOdd
  902. /* Outputs: true if even, false if not even
  903. /* Purpose: This function takes one integer from evenOdd
  904. /* and determines if it is even or not.
  905. /*********************************************/
  906. bool isEven(int EvenOddNum) {
  907.  
  908. bool Even;
  909.  
  910. //if statement tests if user input is even, outputs the result
  911. if (EvenOddNum % 2 == 0) {Even = true;}
  912. else {Even = false;}
  913.  
  914.  
  915. return Even;
  916. }
  917.  
  918.  
  919. /*********************************************/
  920. /* Function: primeNumber
  921. /* Inputs: integer to test
  922. /* Outputs: 1 if prime, 0 if not prime
  923. /* Purpose: This function takes one integer input
  924. /* and determines if it is prime or not.
  925. /*********************************************/
  926. int primeNumber() {
  927.  
  928. //Define vars for while loop and user choice
  929. int primeNumberInput;
  930. bool primeNumberQuit = false;
  931.  
  932. //While loop for integer prompt
  933. while (primeNumberQuit == false) {
  934.  
  935. //Prompt user for integer
  936. cout << "Enter a positive integer. (0 to quit)\n";
  937.  
  938. //User input
  939. cin >> primeNumberInput;
  940.  
  941. //if statement insures input is valid
  942. if (primeNumberInput < 0) {cout << "Invalid input, please try agaain.\n";}
  943. else if (primeNumberInput == 0) {
  944. primeNumberQuit = true;
  945. cout << "Returning to math break menu.\n";
  946. }
  947.  
  948. //else if statements passes input through isPrime and displays the result
  949. else if (isPrime(primeNumberInput) == true) {cout << "The number you entered is prime.\n";}
  950. else {cout << "The number you entered is not prime.\n";}
  951.  
  952. }
  953.  
  954. return 0;
  955. }
  956.  
  957. /*********************************************/
  958. /* Function: isPrime
  959. /* Inputs: integer from primeNumber
  960. /* Outputs: true if prime, false if not prime
  961. /* Purpose: This function takes one integer from primeNumber
  962. /* and determines if it is prime or not.
  963. /*********************************************/
  964. bool isPrime(int primeNumberNum) {
  965.  
  966. //Define var to be returned to main function
  967. bool Prime = true;
  968.  
  969. //for loop divides every possible factor input,
  970. for (int i = 2; i <= primeNumberNum/2; i++) {
  971. if (primeNumberNum % i == 0) {Prime = false;}
  972. }
  973.  
  974. //Return result to main function
  975. return Prime;
  976. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement