Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //David SoucieGarza. Lab 4
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- using namespace std;
- //Defines CD Class
- class CD {
- private:
- //Private Class Attributes
- string Artist;
- string Title;
- int yearReleased;
- int numTracks;
- string *Tracks
- public:
- //Public Accessor function prototypes
- string getArtist();
- string getTitle();
- int getYearReleased();
- int getNumTracks();
- string getTrack(int);
- //Public Mutator function prototypes
- void setArtist (string);
- void setTitle (string);
- void setYearReleased (int);
- void setNumTracks(int);
- void setTrack(int, string);
- //Constructor function protype
- CD();
- };
- /*********************************************/
- /* Function: getArtist
- /* Inputs: None
- /* Outputs: The Artist string
- /* Purpose: This function is the accesor for the
- /* Artist string of the CD object
- /*********************************************/
- string CD::getArtist() {
- return Artist;
- }
- /*********************************************/
- /* Function: getTitle
- /* Inputs: None
- /* Outputs: The Title string
- /* Purpose: This function is the accesor for the
- /* Title string of the CD object
- /*********************************************/
- string CD::getTitle() {
- return Title;
- }
- /*********************************************/
- /* Function: getYearReleased
- /* Inputs: None
- /* Outputs: The yearReleased string
- /* Purpose: This function is the accesor for the
- /* Year_Released string of the CD object
- /*********************************************/
- int CD::getYearReleased() {
- return yearReleased;
- }
- /*********************************************/
- /* Function: getNumTracks
- /* Inputs: None
- /* Outputs: The numTracks string
- /* Purpose: This function is the accesor for the
- /* Year_Released string of the CD object
- /*********************************************/
- int CD::getNumTracks() {
- return numTracks;
- }
- /*********************************************/
- /* Function: getTracks
- /* Inputs: int T
- /* Outputs: the T track in the Tracks Array
- /* Purpose: This function is the accesor for the
- /* Tracks string of the CD object
- /*********************************************/
- string CD::getTrack(int T) {
- return Tracks[T];
- }
- /*********************************************/
- /* Function: setArtist
- /* Inputs: String A
- /* Outputs: None
- /* Purpose: This function is the mutator for the
- /* Artist string of the CD object
- /*********************************************/
- void CD::setArtist (string A) {
- Artist = A;
- }
- /*********************************************/
- /* Function: setTitle
- /* Inputs: String T
- /* Outputs: None
- /* Purpose: This function is the mutator for the
- /* Title string of the CD object
- /*********************************************/
- void CD::setTitle (string T) {
- Title = T;
- }
- /*********************************************/
- /* Function: setYearReleased
- /* Inputs: String Y
- /* Outputs: None
- /* Purpose: This function is the mutator for the
- /* Year_Released string of the CD object
- /*********************************************/
- void CD::setYearReleased (int Y) {
- yearReleased = Y;
- }
- /*********************************************/
- /* Function: setNumTracks
- /* Inputs: String numOfTracks
- /* Outputs: None
- /* Purpose: This function is the mutator for the
- /* setNumTracks string of the CD object
- /*********************************************/
- void CD::setNumTracks (int numOfTracks) {
- numTracks = numOfTracks;
- Tracks[numTracks];
- }
- /*********************************************/
- /* Function: setTrack
- /* Inputs: int trackIndex, string trackName
- /* Outputs: None
- /* Purpose: This function is the mutator for the
- /* Tracks string array of the CD object
- /*********************************************/
- void CD::setTrack(int trackIndex, string track) {
- cout << "Track slot: " << trackIndex << endl;
- Tracks[trackIndex] = track;
- }
- /*********************************************/
- /* Function: CD
- /* Inputs: None
- /* Outputs: N/A, returns 0
- /* Purpose: This is the default Constructor for
- /* the CD object
- /*********************************************/
- CD::CD() {
- Artist = "None";
- Title = "None";
- yearReleased = 0;
- numTracks = 0;
- Tracks[0];
- }
- //Function Prototypes
- void testCD();
- void readCollection(CD [], int);
- void displayCollection(CD [], int);
- void sortCollection(CD [], int);
- void findCD(CD [], int);
- void editCD(CD [], int);
- void editFoundCD(CD *cd);
- void addCDTracks(CD [], int);
- void deleteCDTracks(CD [], int);
- void mathBreak();
- int evenOdd();
- int primeNumber();
- bool isEven(int);
- bool isPrime(int);
- /*********************************************/
- /* Function: main
- /* Inputs: Main menu choice
- /* Outputs: N/A, returns 0
- /* Purpose: This function displays the main menu
- /*********************************************/
- int main() {
- //define vars for while loops and user choice
- bool mainQuit = false;
- CD cdCollection[17];
- bool collectionRead = false;
- int mainChoice;
- //Opening statement to enter the program and initially prompt user
- cout << "Welcome to the CD Reader 2000, please select one of the options below.\n";
- while (mainQuit == false) {
- //Options for the CD Reader
- cout << "Main Menu" << endl;
- cout << "___________________" << endl;
- cout << "1. Read Collection." << endl;
- cout << "2. Display Collection." << endl;
- cout << "3. Sort Collection." << endl;
- cout << "4. Find a CD." << endl;
- cout << "5. Edit a CD." << endl;
- cout << "6. Find a Track." << endl;
- cout << "7. Delete Tracks from a CD." << endl;
- cout << "8. Math Break." << endl;
- cout << "9. Quit." << endl;
- //User input
- cin >> mainChoice;
- //If statements redirects the user to each function or ends the program
- if (mainChoice == 1) {
- readCollection(cdCollection, 17);
- collectionRead = true;
- }
- else if (mainChoice == 2) {
- if (collectionRead == false)
- cout << "Make sure to read a collection first." << endl;
- else
- displayCollection(cdCollection, 17);
- }
- else if (mainChoice == 3) {
- if (collectionRead == false)
- cout << "Make sure to read a collection first." << endl;
- else
- sortCollection(cdCollection, 17);
- }
- else if (mainChoice == 4) {
- if (collectionRead == false)
- cout << "Make sure to read a collection first." << endl;
- else
- findCD(cdCollection, 17);
- }
- else if (mainChoice == 5) {
- if (collectionRead == false)
- cout << "Make sure to read a collection first." << endl;
- else
- editCD(cdCollection, 17);
- }
- else if (mainChoice == 6) {
- if (collectionRead == false)
- cout << "Make sure to read a collection first." << endl;
- else
- addCDTracks(cdCollection, 17);
- }
- else if (mainChoice == 7) {
- if (collectionRead == false)
- cout << "Make sure to read a collection first." << endl;
- else
- deleteCDTracks(cdCollection, 17);
- }
- else if (mainChoice == 8) {
- mathBreak();
- }
- else if (mainChoice == 9) {
- mainQuit = true;
- cout << "Exiting program.\n";
- }
- //Invalid input redirects user to the original menu
- else {
- cout << "Invalid input, please try again.\n";
- }
- }
- return 0;
- }
- /*********************************************/
- /* Function: testCD
- /* Inputs: None
- /* Outputs: None
- /* Purpose: This is a test function which creates a
- /* default CD and sets and displays test values
- /*********************************************/
- void testCD() {
- //Creates CD Object
- CD testCD;
- //Indicates function call
- cout << "Test CD created." << endl;
- //Sets values on Test CD
- testCD.setArtist("Nothing");
- testCD.setTitle("Nothing");
- testCD.setYearReleased(0);
- //Displays values to the console
- cout << "Artist: " << testCD.getArtist() << endl;
- cout << "Title: " << testCD.getTitle() << endl;
- cout << "Artist: " << testCD.getYearReleased() << endl;
- cout << "Returning to the main menu." << endl;
- }
- /*********************************************/
- /* Function: ReadCollection
- /* Inputs: cdCollection CD Array, size integer
- /* Outputs: None
- /* Purpose: This function reads the data from a file
- /* to the cdCollection CD array
- /*********************************************/
- void readCollection (CD cdCollection[], int size) {
- string fileChoice;
- ifstream fileReader;
- string line;
- int year;
- int count = 0;
- fileReader.open("CD.txt");
- if (fileReader) {
- cout << "Reading..." << endl;
- for (int i = 0; i < size; i++) {
- getline(fileReader, line);
- cdCollection[i].setArtist(line);
- getline(fileReader, line);
- cdCollection[i].setTitle(line);
- fileReader >> year;
- cdCollection[i].setYearReleased(year);
- fileReader.ignore();
- }
- cout << "Collection read." << endl << endl;
- fileReader.close();
- }
- else {
- cout << "No file found." << endl << endl;
- }
- }
- /*********************************************/
- /* Function: displayCollection
- /* Inputs: cdCollection CD Array, size integer
- /* Outputs: None
- /* Purpose: This functions displays the
- /* cdCollection CD array
- /*********************************************/
- void displayCollection(CD cdCollection[], int size) {
- //Indicates program action
- cout << "Displaying collection..." << endl;
- //Displays column headings
- cout << fixed << left << setw(30) << "Artist" << setw(30) << "Title" << setw(30) << "Year" << endl;
- for (int count=0; count < size; count++) {
- //Displays each object in array
- cout << fixed << left << setw(30) << cdCollection[count].getArtist() <<
- setw(30) << cdCollection[count].getTitle() <<
- setw(30) << cdCollection[count].getYearReleased() << endl;
- //Displays any tracks a CD has
- if (cdCollection[count].getNumTracks() > 0) {
- for (int i=0; i < cdCollection[count].getNumTracks(); i++)
- cout << " " << i + 1 << ". " << cdCollection[count].getTrack(i) << endl;
- }
- }
- //Indicates program action
- cout << "Collection displayed." << endl << endl;
- }
- /*********************************************/
- /* Function: sortCollection
- /* Inputs: cdCollection CD Array, size integer
- /* Outputs: None
- /* Purpose: This functions sorts the cdCollection
- /* array via artist
- /*********************************************/
- void sortCollection(CD cdCollection[], int length) {
- //intialize saving object
- CD oldCD;
- //Indicate program action
- cout << "Sorting collection..." << endl;
- //For loops parse every object through the array
- for (int i = 0; i < length; i++) {
- for (int j = 0; j < length; j++) {
- if (cdCollection[i].getArtist() < cdCollection[j].getArtist()) {
- //If the parsing ojbect is smaller than the parsed object, they swap array location
- oldCD = cdCollection[i];
- cdCollection[i] = cdCollection[j];
- cdCollection[j] = oldCD;
- }
- }
- }
- //Indicate program action
- cout << "Collection sorted." << endl << endl;
- }
- /*********************************************/
- /* Function: findCD
- /* Inputs: cdCollection CD Array, size integer
- /* Outputs: None
- /* Purpose: This functions prompts for user input
- /* and searches for matching object
- /*********************************************/
- void findCD(CD cdCollection[], int size) {
- //Intialize variables
- string artist;
- string title;
- bool inCollection = false;
- bool search = true;
- string searchQuery;
- //While loop runs until user exits
- while (search) {
- //Redefines variable for album search.
- //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
- inCollection = false;
- //Prompts user for input, obtains only user-entered line
- cout << "Select an artist to search for." << endl;
- getline(cin, artist);
- while (artist.length()==0)
- getline(cin, artist);
- cout << "Select an album title to search for." << endl;
- getline(cin, title);
- while (title.length()==0 )
- getline(cin, title);
- //For Loop parses user inputs into array, checks for matching object
- for (int i = 0; i < size; i++) {
- if (cdCollection[i].getArtist() == artist && cdCollection[i].getTitle() == title) {
- inCollection = true;
- }
- }
- //decision statement dispalys whether or not matching object was found to user inputs
- if (inCollection) { cout << "CD found in collection." << endl;}
- else { cout << "CD not found in collection." << endl;}
- //Prompts user to exit function
- cout << "Continue searching for albums? (y/n)" << endl;
- cin >> searchQuery;
- if (searchQuery == "n") {
- cout << "Returning to main menu." << endl << endl;
- search = false;
- }
- }
- }
- /*********************************************/
- /* Function: editCD
- /* Inputs: cdCollection CD Array, size integer
- /* Outputs: None
- /* Purpose: This functions allows the user to edit
- /* a cd object of their choice in the collection
- /*********************************************/
- void editCD(CD cdCollection[], int size) {
- //Initialize variables
- string artist;
- string title;
- bool inCollection = false;
- bool search = true;
- bool edit = true;
- int searchedCD;
- CD *foundCD;
- //Redefines variables for album search
- artist = "";
- title = "";
- inCollection = false;
- //User prompt
- cout << "Select a CD to edit (Enter artist first, then album, 'quit' to quit): " << endl;
- //Gets first user input
- getline(cin, artist);
- while (artist.length()==0)
- getline(cin, artist);
- //Exits function if user quits
- if (artist == "quit") {
- cout << "Returning to main menu." << endl;
- search = false;
- }
- else {
- //Gets second user input
- getline(cin, title);
- while (title.length()==0)
- getline(cin, title);
- //Searches for album in collection
- cout << "Searching..." << endl;
- for (int i = 0; i < size; i++) {
- if (cdCollection[i].getArtist() == artist && cdCollection[i].getTitle() == title) {
- inCollection = true;
- foundCD = &cdCollection[i];
- }
- }
- //If album is found, it can be edited
- if (inCollection) {
- cout << "editing CD..." << endl;
- editFoundCD(foundCD);
- }
- //Redirects user if CD is not found
- else
- cout << "CD not found, please try again.";
- }
- }
- void editFoundCD(CD *FoundCD) {
- int editChoice;
- string newArtist;
- string newTitle;
- int newYearReleased;
- string continueQuery;
- bool edit = true;
- //Redefines variables for CD edit
- edit = true;
- cout << "Entering CD Edit" << endl;
- //Outputs CD title
- cout << "CD found: " << FoundCD->getArtist() << " - " << FoundCD->getTitle() << "(" << FoundCD->getYearReleased() << ")" << endl;
- while (edit) {
- //Menu for editing selection
- cout << "Select an option to edit (0 to quit):" << endl;
- cout << "1. Edit artist." << endl;
- cout << "2. Edit album title." << endl;
- cout << "3. Edit year of release." << endl;
- cin >> editChoice;
- //0 results in quit
- if (editChoice == 0) {
- edit = false;
- cout << "Returning to main menu." << endl;
- }
- else {
- //Switch for user input
- switch (editChoice) {
- case 1:
- //Obtains user input for new artist
- cout << "Select a new artist:" << endl;
- getline(cin, newArtist);
- while (newArtist.length()==0)
- getline(cin, newArtist);
- //Sets the current record to the new artist
- FoundCD->setArtist(newArtist);
- cout << "CD has been edited." << endl;
- break;
- case 2:
- //Obtains user input for new album title
- cout << "Select a new album title:" << endl;
- getline(cin, newTitle);
- while (newTitle.length()==0)
- getline(cin, newTitle);
- //Sets the current record to the new album title
- FoundCD->setTitle(newTitle);
- cout << "CD has been edited" << endl;
- break;
- case 3:
- //Obtains user input for new year of release
- cout << "Select a new year of release:" << endl;
- cin >> newYearReleased;
- //Sets the current record to the new year of release
- FoundCD->setYearReleased(newYearReleased);
- cout << "CD has been edited" << endl;
- break;
- }
- }
- }
- }
- /*********************************************/
- /* Function: addCDTracks
- /* Inputs: CD cdCollection array, int size
- /* Outputs: N/A
- /* Purpose: This function allows the user to add
- /* tracks to a CD
- /*********************************************/
- void addCDTracks(CD cdCollection[], int size) {
- //Initialize variables
- string artist;
- string title;
- bool inCollection = false;
- bool search = true;
- bool edit = true;
- int foundCD;
- int addedTracks;
- string trackName;
- //Redefines variables for album search
- artist = "";
- title = "";
- inCollection = false;
- //User prompt
- cout << "Select a CD to edit (Enter artist first, then album, 'quit' to quit): " << endl;
- //Gets first user input
- getline(cin, artist);
- while (artist.length()==0)
- getline(cin, artist);
- //Exits function if user quits
- if (artist == "quit") {
- cout << "Returning to main menu." << endl;
- search = false;
- }
- else {
- //Gets second user input
- getline(cin, title);
- while (title.length()==0)
- getline(cin, title);
- //Searches for album in collection
- cout << "Searching..." << endl;
- for (int i = 0; i < size; i++) {
- if (cdCollection[i].getArtist() == artist && cdCollection[i].getTitle() == title) {
- inCollection = true;
- foundCD = i;
- }
- }
- //If album is found, tracks can be added
- if (inCollection) {
- cout << "CD found: " << cdCollection[foundCD].getArtist() << " - " << cdCollection[foundCD].getTitle() << "(" << cdCollection[foundCD].getYearReleased() << ")" << endl;
- cout << "How many tracks would you like to add?" << endl;
- cin >> addedTracks;
- cdCollection[foundCD].setNumTracks(addedTracks);
- for (int j = 0; j < cdCollection[foundCD].getNumTracks(); j++) {
- trackName = "";
- cout << "Name of Track " << j << ":" << endl;
- while (trackName.length() == 0)
- getline(cin, trackName);
- cdCollection[foundCD].setTrack(j, trackName);
- }
- cout << "All Tracks:" << endl;
- for (int k = 0; k < cdCollection[foundCD].getNumTracks(); k++) {
- cout << " " << k + 1 << ". " << cdCollection[foundCD].getTrack(k) << endl;
- }
- }
- //Redirects user if CD is not found
- else
- cout << "CD not found, please try again.";
- }
- }
- void deleteCDTracks(CD cdCollection[], int size) {
- //Initialize variables
- string artist;
- string title;
- bool inCollection = false;
- bool search = true;
- bool edit = true;
- int searchedCD;
- int foundCD;
- //Redefines variables for album search
- artist = "";
- title = "";
- inCollection = false;
- //User prompt
- cout << "Select a CD to edit (Enter artist first, then album, 'quit' to quit): " << endl;
- //Gets first user input
- getline(cin, artist);
- while (artist.length()==0)
- getline(cin, artist);
- //Exits function if user quits
- if (artist == "quit") {
- cout << "Returning to main menu." << endl;
- search = false;
- }
- else {
- //Gets second user input
- getline(cin, title);
- while (title.length()==0)
- getline(cin, title);
- //Searches for album in collection
- cout << "Searching..." << endl;
- for (int i = 0; i < size; i++) {
- if (cdCollection[i].getArtist() == artist && cdCollection[i].getTitle() == title) {
- inCollection = true;
- foundCD = i;
- }
- }
- //If album is found, tracks are checked
- if (inCollection && cdCollection[foundCD].getNumTracks() > 0) {
- cdCollection[foundCD].setNumTracks(0);
- }
- //Redirects user if CD is not found or does not contain tracks
- else
- cout << "CD not found, or does not contain tracks, please try again.";
- }
- }
- /*********************************************/
- /* Function: mathBreak
- /* Inputs: Math break menu choices
- /* Outputs: N/A, returns 0
- /* Purpose: This function displays the Math Break menu
- /* and redirects to the math functions
- /*********************************************/
- void mathBreak() {
- //define vars for while loops and user choice
- bool mathBreakQuit = false;
- int mathBreakChoice;
- //Prompt user
- cout << "Welcome to your math break, please select one of the options below.\n";
- //While loop for math break menu
- while (mathBreakQuit == false) {
- //Options for the math break
- cout << "1. Even or Odd.\n";
- cout << "2. Prime Number.\n";
- cout << "3. Quit.\n";
- //User input
- cin >> mathBreakChoice;
- //If statements redirect user to math functions or end the program
- if (mathBreakChoice == 1) {
- evenOdd();
- }
- else if (mathBreakChoice == 2) {
- primeNumber();
- }
- else if (mathBreakChoice == 3) {
- cout << "Returning to main menu.\n";
- mathBreakQuit = true;
- }
- //Invalid input redirects user to original menu
- else if (mathBreakChoice < 1 || mathBreakChoice > 3) {
- cout << "Invalid input, please try again.\n";
- }
- }
- }
- /*********************************************/
- /* Function: evenOdd
- /* Inputs: integer to test
- /* Outputs: true if even, false if not even
- /* Purpose: This function takes one integer input
- /* and determines if it is prime or not.
- /*********************************************/
- int evenOdd() {
- //define vars for while loops and user choice
- int evenOddInput;
- bool evenOddQuit = false;
- //While loop for integer prompt
- while (evenOddQuit == false) {
- //Prompt user for integer
- cout << "Enter a positive integer. (0 to quit)\n";
- //User input
- cin >> evenOddInput;
- //if statement insures input is valid
- if (evenOddInput < 0) {
- cout << "Invalid input, please try again.\n";
- }
- //else if statement runs input through isEven function or returns user to math break menu
- else if (evenOddInput == 0) {
- evenOddQuit = true;
- cout << "Returning to math break menu.\n";
- }
- else if (isEven(evenOddInput) == true) {cout << "The number you entered is even.\n";}
- else {cout << "The number you entered is odd.\n";}
- }
- return 0;
- }
- /*********************************************/
- /* Function: isEven
- /* Inputs: integer from evenOdd
- /* Outputs: true if even, false if not even
- /* Purpose: This function takes one integer from evenOdd
- /* and determines if it is even or not.
- /*********************************************/
- bool isEven(int EvenOddNum) {
- bool Even;
- //if statement tests if user input is even, outputs the result
- if (EvenOddNum % 2 == 0) {Even = true;}
- else {Even = false;}
- return Even;
- }
- /*********************************************/
- /* Function: primeNumber
- /* Inputs: integer to test
- /* Outputs: 1 if prime, 0 if not prime
- /* Purpose: This function takes one integer input
- /* and determines if it is prime or not.
- /*********************************************/
- int primeNumber() {
- //Define vars for while loop and user choice
- int primeNumberInput;
- bool primeNumberQuit = false;
- //While loop for integer prompt
- while (primeNumberQuit == false) {
- //Prompt user for integer
- cout << "Enter a positive integer. (0 to quit)\n";
- //User input
- cin >> primeNumberInput;
- //if statement insures input is valid
- if (primeNumberInput < 0) {cout << "Invalid input, please try agaain.\n";}
- else if (primeNumberInput == 0) {
- primeNumberQuit = true;
- cout << "Returning to math break menu.\n";
- }
- //else if statements passes input through isPrime and displays the result
- else if (isPrime(primeNumberInput) == true) {cout << "The number you entered is prime.\n";}
- else {cout << "The number you entered is not prime.\n";}
- }
- return 0;
- }
- /*********************************************/
- /* Function: isPrime
- /* Inputs: integer from primeNumber
- /* Outputs: true if prime, false if not prime
- /* Purpose: This function takes one integer from primeNumber
- /* and determines if it is prime or not.
- /*********************************************/
- bool isPrime(int primeNumberNum) {
- //Define var to be returned to main function
- bool Prime = true;
- //for loop divides every possible factor input,
- for (int i = 2; i <= primeNumberNum/2; i++) {
- if (primeNumberNum % i == 0) {Prime = false;}
- }
- //Return result to main function
- return Prime;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement