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
  • Hi I've a problem with this code. When I add more than 1 song to the...

    Hi I've a problem with this code. When I add more than 1 song to the list, it doesn't show the first one, only shows the latest one, when I called the function displaysong, let's say when you add 2 songs ID 1 then ID 2, it shows only ID 1. Can you fix it.  Everything else is fine.. #include <iostream> #include<string> using namespace std; //class Song class Song{ private: int songID; string title; string artist; string album; int year; public:...

  • 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);...

  • This is just a partial C++ code. I am having a problem with the getline(cin, variable)...

    This is just a partial C++ code. I am having a problem with the getline(cin, variable) statement of my code. Please run the code for yourself and see that it doesn't let you enter the full name. It skips the prompt to enter the name and goes straight to the next cout statement. I can't use cin alone because I need to capture and store both the first name followed by space followed by last name. I was trying to...

  • The project I have is a link list that asks for a name and phone number...

    The project I have is a link list that asks for a name and phone number and it allows the user to insert ,delete ,modify ,and find an element in the structure. The program has a menu in which the user can choose which function they want to use I need to use  switch case to do this but it has not worked I need help with this. Thank you! #include <iostream> #include <string> #include <iomanip> using namespace std; const int...

  • 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 have problem where the address is not save to my file after i input a...

    i have problem where the address is not save to my file after i input a new address and exit it #include<string> #include<iostream> #include<fstream> #include<sstream> using namespace std; typedef struct date {    int day;    int month;    int year; }Date; typedef struct add {    string building;    string street;    string city;    string state;    string zip; }Address; typedef struct entry {    string firstName;    string lastName;    Address address;    string phNo;    Date...

  • 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"...

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