Hello learning c++ here.
I have a question regarding file handling. So I want to have the data stored so that I don't have to rewrite the data every time I close and open the program again.
I believe it has to be saved some where in the .txt file?
I been doing research but I'm very confused on how to do so using #include with vector of lists
Please help! Thank you.
Music.h header file
#ifndef MUSIC_H #define MUSIC_H #include using namespace std; class Music { public: string getSongName() const; void setSongName(string songName); string getGenreName() const; void setGenreName(string genreName); int getyear() const; void setyear(int year); bool equals(const Music&); string toString() const; private: string songName; string genreName; int year = 0; }; #endif
Music.cpp file
#include "Music.h"
string Music::getSongName() const { return songName; }
void Music::setSongName(string songName) { this->songName = songName; }
string Music::getGenreName() const { return genreName; }
void Music::setGenreName(string genreName) { this->genreName = genreName; }
int Music::getyear() const { return year; }
void Music::setyear(int year) { this->year = year; }
bool Music::equals(const Music &s) { return (this->songName == s.getSongName()); }
string Music::toString() const { string str = ""; str += "Name of Song: " + songName; str += " Name of Genre: " +genreName ; str += " Year of Creation: " + to_string(year) + " "; return str; }
Main.cpp
#include"Music.h" #include #include //prototype functions int mainMenu(); void addMusic(vector& mInfo); void viewMusic(const vector& mInfo); void viewMusicInfo(const vector& mInfo); void deleteMusic(vector& mInfo); /*********************************************** * Function:main * Description: controls full flow of program this is where the user will be inputting all * option from program menu, to inputting Music info, Deleting Music info, * as well as viewing movie info. ***********************************************/ int main() { //data storage vector name vector musicCount; int choiceLI = 0; do { choiceLI = mainMenu(); //if choice is more than 3 or less than 1, reset user while (choiceLI > 3 || choiceLI < 0) { cout << " " << endl; cout << "Invalid Option, Please try again." << endl; cout << " " << endl; main(); } //options menu functions if (choiceLI == 1) { addMusic(musicCount); } else if (choiceLI == 2) { viewMusic(musicCount); viewMusicInfo(musicCount); } else if (choiceLI == 3) { viewMusic(musicCount); deleteMusic(musicCount); } //while choice isn't 0 } while (choiceLI != 0); cout << " " << endl; cout << "Thank you, Goodbye!" << endl; exit(0); } /*********************************************** * Function: mainMenu * Description: Menu for user to decide what they would like to do * Return choice gets passed to main. * Input: int choice - reads user choice from menu ***********************************************/ int mainMenu() { int choice; cout << "||==================== Prestige Worldwide DJ =====================||" << endl; //list of options cout << "||================================================================||" << endl; cout << "|| 1 - Add Song ||" << endl; cout << "|| 2 - View Playlist ||" << endl; cout << "|| 3 - Delete Songs ||" << endl; cout << "|| 0 - Exit ||" << endl; cout << "||================================================================||" << endl; cout << "Please Enter Choice: "; //reads user choice from menu cin >> choice; return choice; } /*************************************************************************** * Function: addMusic * Description: adds song and the data to storage from user input * Input: string songName - holds data for name of song * int year - holds data for year * string genreName - holds data for name of genre ***************************************************************************/ void addMusic(vector& mInfo) { //push back data of Music class Music temp; //prototype variables string songName; string genreName; int year; cout << " " << endl; //Gets data for songName do { cout << "Please enter Name of Song: "; cin.ignore(100, ' '); getline(cin, songName); temp.setSongName(songName); for (int i = 0; i < mInfo.size(); i++) { if (mInfo[i].equals(temp)) { cout << "This song is already in your playlist.. Please enter new Song." << endl << endl; continue; } } } while (sizeof(songName) <= 1); //Gets data for year do { cout << "Please enter Year of Creation: "; cin >> year; temp.setyear(year); } while (sizeof(year) >= 1900); //Gets data for genreName do { cout << "Please enter Name of Genre: "; cin.ignore(100, ' '); getline(cin, genreName); temp.setGenreName(genreName); } while (sizeof(genreName) <= 1); mInfo.push_back(temp); //informs user of song added cout << "The song: " << songName << " has been added to your playlist." << endl; cout << " " << endl; } /*************************************************************************** * Function: viewMusic * Description: displays available list of song Names * Output: songName - list of available songs in numerical order ***************************************************************************/ void viewMusic(const vector& mInfo) { //if Music is empty then return to main menu if (mInfo.size() == 0) { cout << " " << endl; cout << "No songs in list, Returning to main Menu..." << endl; cout << " " << endl; main(); } //else list all available songName in Music class else { cout << " " << endl; cout << "Available Songs in Playlist: " << endl; for (int i = 0; i < mInfo.size(); i++) { cout << i << ". " << mInfo.at(i).getSongName() << endl; } } } /*************************************************************************** * Function: viewMusicInfo * Description: adds song and the data to storage from user input * Input: int i - reads choice of minfo data to grab for user * Output: Music& minfo - grabs music information user desires and displays it. ***************************************************************************/ void viewMusicInfo(const vector& mInfo) { int i = 0; cout << "Which song Information would you like to see: "; //reads what song they've chosen cin >> i; cout << " " << endl; //lists information of chosen song cout << mInfo[i].toString(); cout << " " << endl;
} /*************************************************************************** * Function: deleteMusic * Description: adds song and the data to storage from user input * Input: int i - reads choice of minfo data to delete for user ***************************************************************************/ void deleteMusic(vector& mInfo) { int i = 0; cout << "Which song would you like to delete: "; //reads what song they've chosen cin >> i; //erase position which is the beginning of index + chosen number mInfo.erase(mInfo.begin() + i); cout << " " << endl; cout << "Song deleted, Returning to Main Menu..." << endl; cout << " " << endl; }