I have this code here and I was wondering if I could use the int playList array to index the class song mySong array without using pointers? Can I get to the info in the class by using the index from the int array and if so how would I assign that?
The song class is filled with all strings, artist, song title, etc.
1 2 3 4
song mySong[ MAX_SONGS ]; //declare an array of class song
int playList[ MAX_PLAYLIST ]; // declare an array of ints
void fillSongArray( song Songs[], int Nsongs );// a function to assign the Songs data values
// you must supply a definition for this function
int main
{
constint MAX_SONGS = 50, MAX_PLAYLIST = 10;// for example
song mySong[ MAX_SONGS ]; //declare an array of class song
int playList[ MAX_PLAYLIST ]; // declare an array of ints
fillSongArray( mySong, MAX_SONGS );// fill the array with valid data
// put songs 2, 5, 6, 15 on the playList
int listLength = 4;
playList[0] = 2;
playList[1] = 5;
playList[2] = 6;
playList[3] = 15;
// show the song titles on the playListlist
for(int i=0; i<listLength; ++i)
cout << song[ playList[i] ].title << endl;
return 0;
}
Note: The mySong array is full of junk values until they are assigned. That's what the fillSongArray() is for.
Thanks again for the help fun2code. Here is what I got
1 2 3 4 5 6 7 8 9 10 11 12 13 14
string getSongNameFromPlaylist(int playlistSpot)
{
for( int i = 0; i < MAX_PLAYLIST; i++ )
{
return mySong[ playList[ s ] ].getArtist() + " - " + mySong[ playList[ s ] ].getTitle();
}
}
// the int playListSpot is always going to be zero when this function is called. When I
// select 1 song from my song list it works fine but when I select another song I get
// the second song twice, it replaces the first song. If i add a 3rd song I get the 3rd song
// 3 times. The function string getSongNameFromPlaylist(int playlistSpot) was written by someone
// else that cannot be modified. I can see why it is doing what its doing but I don't know how
// to fix it. Every piece of data in song mySong class is a string. Any thoughts?
The function as written doesn't make sense. If playlistSpot is being passed then it is probably supposed to be used. Where does the variable s come from? The loop index i isn't used in the loop so its value can't affect anything. The loop is pointless as the function returns on the very 1st iteration!
Are you sure this function isn't supposed to return the single playlistSpot song from the list?
the int playListSpot is always going to be zero when this function is called.
Thanks for getting back to me fun2code. I know what I got going on here is a mess.
Here is where I got the variable s from.
The string getSongNameFromPlaylist(int playlistSpot) function should return a string that corresponds to the specified song in my playlist( variable s ). This function is returning the song data that corresponds to the passed in spot in my playlist. Since the playlist is an array I need to follow the index in the designated spot to the actual song requested.
Which was what I was trying to do with this code
1 2 3 4 5 6 7 8
string getSongNameFromPlaylist(int playlistSpot)
{
for( int i = 0; i < MAX_PLAYLIST; i++ )
{
return mySong[ playList[ s ] ].getArtist() + " - " + mySong[ playList[ s ] ].getTitle();
}
}
This is where I got my s from.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void addSongToPlaylist(int songNum)
{
for( int i = 0; i < numberInPlaylist; i++ ) // numberInPlaylist = 33
{
mySong[ playList[ songNum ] ].getArtist();
mySong[ playList[ songNum ] ].getTitle();
s = songNum;
// s is a global variable that stores songNum. songNum is the song number in the
// playlist that I want to add to the string getSongNameFromPlaylist(int playlistSpot)
// function
numberOfSongs++;
break;
}
}
I can't call the getSongNameFromPlaylist function. If you are over it I understand.
Thanks for getting back to me and all your help. I can see my 2 problems. I am in way over my head but the powers that be have determined it so. I'll have more question about stuff that is even further above my head than this as time passes, well for the next month at least. :)