Write a C++ class called Song to represent a song in a music collection

Hello! This is my first post.

Can any one help me with this problem.
Write a C++ class called Song to represent a song in a music collection. The data members of the Song class are strings representing the song title, artist's name, and the album where the song can be found. These instance variables for the class are to have private access modifiers so you will need to write the appropriate functions to allow a client to access and modify the data values. Include a function that returns a string that identifies the Song object for display purposes.
A second file is to be written called SongList that contains a main function and separate function to display a menu of options for the user. The menu will contain options that allow the user to,
Add a song.
Remove a song.
Display all the songs in the list.
Quit.
The menu must list these options in the exact same order, using these numbers (1 - 4). Failure to follow this specification will reduce your project grade by 10%.
The SongList program is to read in a list of songs from a file; when option #4 (Quit) is selected; the program is to save the revised song list in a file with the same name. Use songs.txt as the input file name. Recall that the file is composed of the three data components (strings) that are separated by varying amount of whitespace and a colon. Such as,
Artist : SongTitle : Album

Compile your program with a version of C++ that uses the instruction set from your text. Your program should be appropriately commented.

song.h file

#include <iostream>
#include <string>

using namespace std;

class Song
{

public:

Song(); //default constructor
Song(string aTitle, string anArtist, string anAlbum); //parametrized constructor

string getTitle()const; // return value title
string getArtist()const; // return value artist
string getAlbum()const; // return value album
void setTitle(string newTitle);
void setArtist(string newArtist);
void setAlbum(string newAlbum);

string toDisplayString() const;
string toSaveString() const;

void Addsong(int counter);
void Remove(int counter);
void Display();
void Exit();

private:

string ttl; //title
string art; //artist
string alb; // album

};


song.cpp file
#include <iostream>
#include <string>
#include "Song.h"

using namespace std;

Song:: Song()
{
ttl= " ";
art = " ";
alb = " ";
}

Song::Song(string aTitle, string anArtist, string anAlbum)
{

ttl = aTitle; //Assing parameters
art = anArtist;
alb = anAlbum;

}
string Song::getTitle() const
{
return ttl;
}

void Song:: setTitle(string newTitle)
{
ttl= newTitle;
}

string Song::getArtist() const
{
return art;
}

void Song:: setArtist(string newArtist)
{
art=newArtist;
}

string Song::getAlbum() const
{
return alb;
}
void Song::setAlbum(string newAlbum)
{
alb= newAlbum;
}

string Song::toDisplayString() const
{
string result = "\nTitle: " + ttl;
result += "\nAuthor: " + art;
result += "\nAlbum: " + alb + "\n";
return result;
}

string Song:: toSaveString() const
{
return ttl+ " : " + art + " : " + alb;
}

songList.cpp file:

#include <iostream>
#include <fstream>
#include <string>
#include "Song.h"

using namespace std;

int option;
int menu
int counter = 0;
Song collection[200];




int menu()
{

//Displaying the menu //
cout << "What do you want to do" << endl;
cout << "1. Add a song" << endl;
cout << "2. Remove a song" << endl;
cout << "3. Display all the songs in the list" << endl;
cout << "4. Quit" << endl;




/// Getting input
cin >> option;

//Selecting case and calling functions associated
switch (option) {
case 1:
cout << "\nAdd a song" << endl;
void AddSong (int counter);
counter++;
break;
case 2:
cout << "\nRemove a song" << endl;
void Remove();
break;
case 3:
cout << "\nDisplay a song" << endl;
void Display();
break;
case 4:
cout<< "\nQuit" << endl;
void Exit();
break;
default:
cout << "\nInput a value between 1-4" << endl;
return menu();
}
void Addsong(int counter)
{

cout << "\nEnter song title:" << endl;
getline(cin, ttl);
collection[counter].setTitle(ttl);


cout << "\nEnter song artist:" << endl;
getline(cin, art);
collection[counter].setArtist(art);

cout << "\nEnter song album:" << endl;
getline(cin, alb);
collection[counter].setAlbum(alb);

return menu();
}

void Remove(int counter);
{
cout << "\nEnter song title:" << endl;
getline(cin, ttl);
collection[counter].setTitle(ttl);


cout << "\nEnter song artist:" << endl;
getline(cin, art);
collection[counter].setArtist(art);

cout << "\nEnter song album:" << endl;
getline(cin, alb);
collection[counter].setAlbum(alb);

return menu();
}

void Display(int counter);
{
int loco;
Song list;

cout << "\nHere are the songs in your collection:\n\n" << endl;

for(loco = 0; loco < 10; loco++);
{
list = collection[loco];

if(loco + 1 < 10)
{
cout << " " << loco + 1 << " | ";
}
else
cout << loco + 1 << " | ";
}


cout << "\nHere are the songs in your collection:" << endl;
cout << "Title:" << list.getTitle() << endl;
cout << "Artist:" << list.getArtist()<< endl;
cout << "Album:" << cout << list.getAlbum() << endl;

return menu();
}
void Exit()
{

}
}

this is what I got so far if some on could please help me that would be great


It looks like you're almost there!

As it's an assignment, I don't think specific details are appropriate. But here are some general comments:

- is it always clear what's happening? could the naming be clearer? (without getting carried away) if you come across a variable, away from it's comment, is it clear what it holds? does th name of a variable/function say what it really is/does?
- are mistakes handled gracefully and reported?
- have you come across references yet?
- and the while statement?

(Naming is very important in complicated systems!)

Andy

P.S. I find your first few comments a bit pointless. Esp. "return value title" for getTitle(). BUT you need to do what your tutor requires! But do seek clarification at the point at which commenting becomes pointless.
The part I am having trouble with is the songList.cpp file. I am not sure how to the display all of the value in song.txt file. Also how to remove the song the user don't want from the song.txt file. Also once the user quits I have to save all of the changes the user did. If you could give me some suggestion that would be great. Thanks
You've already done a good chunk of the work.

To display the song, I'd use the accessors to get the values and then write them using cout. But you've coded getDisplayString which could be used instead.

And if you can write them to the screen, you've done most of the work to rewrite the file.

For this kind of app you just need to resave the whole file. You're not trying to update just a bit of an existing file, like a SQL server has to do (that involves a signifcantly different type of file structure).

If you're unsure of particular bit, code a little mini-app to test that particular bit. Once it works, add it to the main program. (I use this approach all the time when developig algorithms; it's quicker to compile and test a little test).
oh ok, so your saying is that to read the song.txt file using the getdisplayString and just store it in an array and cout them.
I made changes to the songList.ccp file:


#include <iostream>
#include <fstream>
#include <string>
#include "Song.h"

using namespace std;

int option;
int menu();
int counter = 0;
Song collection[200];




int menu()
{

//Displaying the menu //
cout << "What do you want to do" << endl;
cout << "1. Add a song" << endl;
cout << "2. Remove a song" << endl;
cout << "3. Display all the songs in the list" << endl;
cout << "4. Quit" << endl;




/// Getting input
cin >> option;

//Selecting case and calling functions associated
switch (option) {
case 1:
cout << "\nAdd a song" << endl;
void AddSong (int counter);
counter++;
break;
case 2:
cout << "\nRemove a song" << endl;
void Remove();
break;
case 3:
cout << "\nDisplay a song" << endl;
void Display();
break;
case 4:
cout<< "\nQuit" << endl;
void Exit();
break;
default:
cout << "\nInput a value between 1-4" << endl;
return menu();
}

void Addsong(int counter)
{

cout << "\nEnter song title:" << endl;
getline(cin, ttl);
collection[counter].setTitle(ttl);


cout << "\nEnter song artist:" << endl;
getline(cin, art);
collection[counter].setArtist(art);

cout << "\nEnter song album:" << endl;
getline(cin, alb);
collection[counter].setAlbum(alb);

return menu();
}

void Remove(int counter);
{

}

void Display(int counter);
{

}
void Exit()
{

}
[code] "Please use code tags" [/code]
1
2
3
4
5
class Song
{
public:
  void Addsong(int counter);
  void Remove(int counter);
Those methods don't make sense there. A song should not be aware that others exist.

1
2
3
4
5
6
7
8
//Selecting case and calling functions associated
switch (option) {
case 1:
  cout << "\nAdd a song" << endl;
  //void AddSong (int counter); //this is a definition
  AddSong( /* */); //this is a call
  counter++; 
  break;


¿Why don't you make title, artist and album public?
Sorry, I was mainly looking at the function definitions so it didn't sink in that Addsong(), Remove(), Display(), and Exit() were declared as methods of Song. I thought these were all global functions (as suggested by the spec).

toDisplayString (or getDisplayString) is to display a song.

You need to use a bit of istream based code to read the entries. I assume you've covered this already given the exercise.

Have you covered vectors yet?

I would try and get you program to run one bit at a time. Once each bit is running, then you can put it all together. e.g. to test the display code, create some songs in code.

Andy

P.S. ne555 - it would be a bad idea to make title, artist and album public from an OO point of view. It's also expressly forbidden by the problem spec.
Topic archived. No new replies allowed.