Hey guys, I have a project which I've been trying to do, but I don't know how to do it. Can you please help me a little?
-Album database:
Create a program that allows the user to set information of a music album, display the saved albums and search for a particular album by its title, artist name, genre, year of release and its rating.
struct Album
{
int id;
string title;
string artist;
string musicGenre;
int year;
int rating;
};
class Albums
{
public:
// class builder
Albums()
{
currentId = 1;
}
//function that allows to set information of an album
Album setAlbumInfo()
{
Album album;
string albumTitle;
string artistName;
string albumGenre;
int albumYear;
int albumRating;
cout << "Write the album name: ";
cin >> albumTitle;
cout << "Write the artist name: ";
cin >> artistName;
cout << "Write the genre: ";
cin >> albumGenre;
cout << "Write the year of release: ";
cin >> albumYear;
cout << "Write the rating: " << endl;
do
{
cout << "(rank between 0 y 5): ";
cin >> albumRating;
}
while(albumRating < 0 || albumRating > 5);
// function that assigns information of a particular album based on its index void setAlbum(int index, Album album)
{
// I don't know what to do here
}
// funcion que devuelve un album particular en base a su indice
Album getAlbum(int id)
{
int x;
cout<<"Enter the id: ";
cin>>x;
if(x+1==myAlbums[MAX_CAPACITY].id)
{
cout<<myAlbums[x+1]<<endl;
}
else
{
x++;
}
}
// prints the list of saved albums
void printAlbums()
{
// I don't know what to do here
}
void albumArtistSearch(string name)
{
string x;
cout<<"Enter the name of the artist: ";
cin>>x;
if(!artistName.compare(x))
{
cout<<myAlbums[].artistName;
}
}
void albumMusicGenreSearch(string genre)
{
string x;
cout<<"Enter the music genre: ";
cin>>x;
if(!albumGenre.compare(x))
{
cout<<
}
}
void albumYearSearch(int year)
{
int x;
cout<<"Enter the release year ofr the album: ";
cin>>x;
if(x==myAlbums[MAX_CAPACITY].year)
{
cout<<
}
else
{
cout<<"Album no encontrado."<<endl;
}
}
void albumRatingSearch(int rating)
{
int x;
cout<<"Enter the rating: ";
cin>>x;
if(x==myAlbums[MAX_CAPACITY].rating)
{
cout<<
}
}
// turns thte string into lowercase
string stringToLower(string s)
{
int length = s.length();
string lower;
for(int i = 0; i < length; i++)
{
lower += tolower(s[i]);
}
return lower;
}
private:
int currentId;
Album myAlbums[MAX_CAPACITY]; //array of albums
};
int main ()
{
// class instance
Albums albumCollection;
// tempolar album to keep information
Album temp;
int opc;
cout<< "Albums database"<<endl;
cout<<"Choose an option: "<<endl;
cout<<"1-Display all albums"<<endl;
cout<<"2-Enter a new album"<<endl;
cout<<"3-Edit an album"<<endl;
cout<<"4-Search an album"<<endl;
cout<<"5-Delete an album"endl;
cout<<"6-Exit"<<endl;
cin>>opc;
while (opc!=6)
{
switch (opc)
{
case '1':
albumCollection.printAlbums();
break;
case '2':
albumCollection.addAlbum();
break;
case '3':
albumCollection.setAlbum();
break;
case '4':
int op;
cout<<"Search by: "<<endl;
cout<<"1-Title"<<endl;
cout<<"2-Artist name"<<endl;
cout<<"3-Music genre"<<endl;
cout<<"4-Year"<<endl;
cout<<"5-Rating"<<endl;
cin>>op;
switch (op)
{
case '1':
albumCollection.albumTitleSearch();
break;
case '2':
albumCollection.albumArtistSearch();
break;
case '3':
albumCollection.albumMusicGenreSearch();
break;
case '4':
albumCollection.albumYearSearch();
break;
case '5':
albumCollection.albumRatingSearch();
break;
}
case '5':
//I dont lnow how to exit
break;
}
}
return 0;
}
This what I have done so far. Pleasea I need help, thanks guys.
first, the nested switch is probably a bad idea. Its confusing to read and I would try to find another way (or move the inner switch to a function call for clarity). As a rule of thumb, I personally make all my switches call functions if the code in the cases is more than about 3 lines, to keep it extra clean.
while (opc!=6)... you NEVER modify OPC in the code that I can see. So the outer while loop never exits.
ok, now it works, i will post the code down below, the proble is that it doesnt show the album when i am searchig for one. I triedo to do the inner switch as a function, but i couldn't
struct Album
{
int id;
string title;
string artist;
string musicGenre;
int year;
int rating;
};
class Albums
{
public:
// constructor de la clase
Albums()
{
currentId = 1;
}
~Albums()
{
}
// funcion que permite ingresar la informacion de un album
Album setAlbumInfo()
{
Album album;
string albumTitle;
string artistName;
string albumGenre;
int albumYear;
int albumRating;
cout << "Ingrese el titulo del album: ";
cin >> albumTitle;
cout << "Ingrese el nombre del artista: ";
cin >> artistName;
cout << "Ingrese el genero musical del album: ";
cin >> albumGenre;
cout << "Ingrese el periodo en el cual fue publicado el album:";
cin >> albumYear;
cout << "Ingrese el rating del album " << endl;
do
{
cout << "(escala entre 0 y 5): ";
cin >> albumRating;
}
while(albumRating < 0 || albumRating > 5);
// funcion que permite agregar un album
void addAlbum(Album album)
{
if(currentId <= MAX_CAPACITY)
{
album.id = currentId;
myAlbums[currentId-1] = album;
currentId++;
}
else
{
cerr << "Capacidad maxima alcanzada, no es posible agregar mas datos." << endl;
}
}
// funcion que asigna información de un album particular en base a su indice
void setAlbum(int index, Album album)
{
string albumTitle;
string artistName;
string albumGenre;
int albumYear;
int albumRating;
cout << "Ingrese el titulo del album: ";
cin >> albumTitle;
cout << "Ingrese el nombre del artista: ";
cin >> artistName;
cout << "Ingrese el genero musical del album: ";
cin >> albumGenre;
cout << "Ingrese el periodo en el cual fue publicado el album:";
cin >> albumYear;
cout << "Ingrese el rating del album " << endl;
do
{
cout << "(escala entre 0 y 5): ";
cin >> albumRating;
}
while(albumRating < 0 || albumRating > 5);
// funcion que devuelve un album particular en base a su indice
Album getAlbum(int id)
{
for(int i=0;i<=MAX_CAPACITY;i++)
{
if (id==myAlbums[i].id)
{
cout<<"Titulo: "<<myAlbums[i].title<<endl;
cout<<"Artista: "<<myAlbums[i].artist<<endl;
cout<<"Genero: "<<myAlbums[i].musicGenre<<endl;
cout<<"Anyo: "<<myAlbums[i].year<<endl;
cout<<"Rating: "<<myAlbums[i].rating<<endl;
}
}
}
// imprime la lista de albumes almacenados en memoria
void printAlbums()
{
for(int i=1;i<=currentId;i++)
{
cout<<i<<"."<<setw(20)<<myAlbums[i-1].title<<endl;
cout<<"Artista: "<<myAlbums[i-1].artist<<endl;
cout<<"Genero: "<<myAlbums[i-1].musicGenre;
}
}
// busca un álbum por titulo
void albumTitleSearch(string title)
{
string a;
private:
int currentId;
Album myAlbums[MAX_CAPACITY]; // arreglo de albumes
};
int main ()
{
// instancia de la clase Albums (Objeto albumCollection)
Albums albumCollection;
// album temporal para almacenar informacion
Album temp;
int opc;
int idx;
string b;
do
{
cout<<"--------------------------------------------------------------------"<<endl;
cout<<" ||Registro de albumes||"<<endl<<endl;
cout<<"Por favor elija una de las siguientes opciones: "<<endl;
cout<<"1-Ver todos los albumes"<<endl;
cout<<"2-Ingresar un nuevo album"<<endl;
cout<<"3-Editar album ingresado"<<endl;
cout<<"4-Buscar album"<<endl;
cout<<"5-Eliminar album"<<endl;
cout<<"6-Salir"<<endl<<endl;
cin>>opc;
switch (opc)
{
case 1:
albumCollection.printAlbums();
break;
case 2:
temp=albumCollection.setAlbumInfo();
albumCollection.addAlbum(temp);
break;
case 3:
cout<<"Ingrese el indice el album que desea encontrar: ";
cin>>idx;
albumCollection.setAlbum(idx, temp);
break;
case 4:
int op;
cout<<"Buscar por: "<<endl;
cout<<"1-Titulo"<<endl;
cout<<"2-Artista"<<endl;
cout<<"3-Genero"<<endl;
cout<<"4-Anyo"<<endl;
cout<<"5-Rating"<<endl;
cin>>op;
switch (op)
{
case 1:
cout<<"Ingrese el titulo del album que quiere encontrar (unido): ";
cin>>b;
albumCollection.albumTitleSearch(b);
break;
case 2:
cout<<"Ingrese artista del album que quiere encontrar (unido): ";
cin>>b;
albumCollection.albumArtistSearch(b);
break;
case 3:
cout<<"Ingrese el genero del album que quiere encontrar (unido): ";
cin>>b;
albumCollection.albumMusicGenreSearch(b);
break;
case 4:
cout<<"Ingrese el anyo publicacion del album que quiere encontrar: ";
cin>>opc;
albumCollection.albumYearSearch(opc);
break;
case 5:
cout<<"Ingrese el del album que quiere encontrar: ";
cin>>opc;
albumCollection.albumRatingSearch(opc);
break;
}
break;
case 5:
cout<<"Ingrese el id del album que desea eliminar: ";
cin>>opc;
albumCollection.deleteAlbum(opc);
break;
}
}
while (opc<6);
}
the proble is that when i try to search for an album and i type in the title or the artist name, it doesn't show back the album which i was looking for, can you help me with this?