Question

Fix my code, if I the song or the artist name is not on the vector,...

Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

class musicList{
private:
vector<string> songName;
vector<string> artistName;
public:
void addSong(string sName, string aName){
songName.push_back(sName);
artistName.push_back(aName);
}

void deleteSongName(string sName){
vector<string>::iterator result = find(songName.begin(), songName.end(), sName);
if (result == songName.end()){
cout << "The song name is not in there!" << endl;
}
else{
songName.erase(result);
}
}

void deleteArtistName(string aName){
vector<string>::iterator result = find(artistName.begin(), artistName.end(), aName);
if (result == artistName.end())
cout << "The artist name is not in there!" << endl;
else
artistName.erase(result);
}

void printList(){
cout << "Here is the list of your music:" << endl;
for(int i=0; i < songName.size() && i < artistName.size(); i++){
cout << i << ". " << songName.at(i) << " by " << artistName.at(i) << endl;
}
}
};

void displayMenu(){

cout << "1. Add Song" << endl;

cout << "2. Delete Song" << endl;

cout << "3. Print music List" << endl;

cout << "4. Exit" << endl;

cout << endl;

}

int main(){

musicList mList;

while(true){

displayMenu();
cout << "Enter your choice" << endl;
int option;
cin >> option;
cin.ignore(26, '\n');
cout << endl;

if(option == 1){
string sName;
string aName;
cout << "Enter the song name:" << endl;
getline(cin, sName);
cout << "Enter artist name:" << endl;
getline(cin, aName);
mList.addSong(sName, aName);

cout << endl;

}else if(option == 2){
string sName;
string aName;
cout << "Enter the song name you want to delete:" << endl;
getline(cin, sName);
mList.deleteSongName(sName);
cout << "Enter artist name you want to delete:" << endl;
getline(cin, aName);
mList.deleteArtistName(aName);

cout << endl;
}
else if(option == 3){
mList.printList();
cout << endl;
}else if(option == 4){
cout << "Exiting" << endl;
break;
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

class musicList{
private:
vector<string> songName;
vector<string> artistName;
public:
void addSong(string sName, string aName){
songName.push_back(sName);
artistName.push_back(aName);
}

int deleteSongName(string sName){
vector<string>::iterator result = find(songName.begin(), songName.end(), sName);
if (result == songName.end()){
cout << "The song name is not in there!" << endl;
return -1;
}
else{
songName.erase(result);
}

return 0;
}

int deleteArtistName(string aName){
vector<string>::iterator result = find(artistName.begin(), artistName.end(), aName);
if (result == artistName.end())
{
cout << "The artist name is not in there!" << endl;
return -1;
}
else
artistName.erase(result);
return 0;
}

void printList(){
cout << "Here is the list of your music:" << endl;
for(int i=0; i < songName.size() && i < artistName.size(); i++){
cout << i << ". " << songName.at(i) << " by " << artistName.at(i) << endl;
}
}
};

void displayMenu(){

cout << "1. Add Song" << endl;

cout << "2. Delete Song" << endl;

cout << "3. Print music List" << endl;

cout << "4. Exit" << endl;

cout << endl;

}

int main(){

musicList mList;

while(true){

displayMenu();
cout << "Enter your choice" << endl;
int option;
cin >> option;
cin.ignore(26, '\n');
cout << endl;

if(option == 1){
string sName;
string aName;
cout << "Enter the song name:" << endl;
getline(cin, sName);
cout << "Enter artist name:" << endl;
getline(cin, aName);
mList.addSong(sName, aName);

cout << endl;

}else if(option == 2){
string sName;
string aName;
while(1)
{
cout << "Enter the song name you want to delete:" << endl;
getline(cin, sName);
if(mList.deleteSongName(sName)!=-1)
{
break;
}
else
{
cout<<"Please re-enter\n";
}
}

while(1)
{
cout << "Enter artist name you want to delete:" << endl;
getline(cin, aName);
if(mList.deleteArtistName(aName)!=-1)
{
break;
}
else
{
cout<<"Please re-enter\n";
}

}

cout << endl;
}
else if(option == 3){
mList.printList();
cout << endl;
}else if(option == 4){
cout << "Exiting" << endl;
break;
}
}
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Fix my code, if I the song or the artist name is not on the vector,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • Can someone please tell me why the calculation for earnings is not coming through at the...

    Can someone please tell me why the calculation for earnings is not coming through at the end of the program? Thank you. //This program should tell us about streaming services #include <iostream> #include <iomanip> #include <string> using namespace std; int main() {    int choice, streams;    double earnings;    string song;    string artist;    string streamingService;       const int Tidal = 0.01250;    const int Amazon = 0.00402;    const int Apple_Music = 0.00735;    const int...

  • below i added the code, but while running it, makefile.win error showing. how i fix this?...

    below i added the code, but while running it, makefile.win error showing. how i fix this? 1.)waitlist.h #include<iostream> using namespace std; class guest{    public:    string name;    guest*next;    guest*prev;    guest(string);    }; class waitList{    int numberOfGuests;    guest* first;    guest* last;    public:        waitList();        void addPerson(string);        void deletePerson(string);        guest* getFirst();        guest* getLast();    }; 2.)waitlist.cpp #include<iostream> #include"waitlist.h" using namespace std; guest::guest(string name){    this->name...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • I wrote this code but there’s an issue with it : #include <iostream> #include <vector&...

    I wrote this code but there’s an issue with it : #include <iostream> #include <vector> #include <string> #include <fstream> using namespace std; class Borrower { private: string ID, name; public: Borrower() :ID("0"), name("no name yet") {} void setID(string nID); void setName(string nID); string getID(); string getName(); }; void Borrower::setID(string nID) { ID = nID; } void Borrower::setName(string nname) { name = nname; } string Borrower::getID() { return ID; } string Borrower::getName() { return name; } class Book { private: string...

  • (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the...

    (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the date of birth.            **fall.sort(compare_DOB); //Here lies your error** #include <fstream> #include <iostream> #include <string> #include <list> #include <cctype> using namespace std; const char THIS_TERM[] = "fall.txt"; const string HEADER = "\nWhat do you want to do ?\n\n" "\t. Add a new friend name and DOB ? \n" "\t. Edit/Erase a friend record ? \n" "\t. Sort a Friend's record ? \n"...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

  • Coding in c++

    I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() {    char option;    vector<movie> movies;     while (true)     {         printMenu();         cin >> option;         cin.ignore();         switch (option)         {            case 'A':            {                string nm;                int year;                string genre;                cout << "Movie Name: ";                getline(cin, nm);                cout << "Year: ";                cin >> year;                cout << "Genre: ";                cin >> genre;                               //call you addMovie() here                addMovie(nm, year, genre, &movies);                cout << "Added " << nm << " to the catalog" << endl;                break;            }            case 'R':            {                   string mn;                cout << "Movie Name:";                getline(cin, mn);                bool found;                //call you removeMovie() here                found = removeMovie(mn, &movies);                if (found == false)                    cout << "Cannot find " << mn << endl;                else                    cout << "Removed " << mn << " from catalog" << endl;                break;            }            case 'O':            {                string mn;                cout << "Movie Name: ";                getline(cin, mn);                cout << endl;                //call you movieInfo function here                movieInfo(mn, movies);                break;            }            case 'C':            {                cout << "There are " << movies.size() << " movies in the catalog" << endl;                 // Call the printCatalog function here                 printCatalog(movies);                break;            }            case 'F':            {                string inputFile;                bool isOpen;                cin >> inputFile;                cout << "Reading catalog info from " << inputFile << endl;                //call you readFromFile() in here                isOpen = readFile(inputFile, &movies);                if (isOpen == false)                    cout << "File not found" << endl;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT