Class Array Question

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


Thanks
closed account (D80DSL3A)
Sure you can. Something like this should do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void fillSongArray( song Songs[], int Nsongs );// a function to assign the Songs data values
// you must supply a definition for this function

int main
{
	const int 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? 


Thanks again.
closed account (D80DSL3A)
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.

Why?

This makes more sense:
1
2
3
4
string getSongNameFromPlaylist(int playlistSpot)
{
	return  mySong[ playList[ playlistSpot ] ].getArtist() + " - " + mySong[ playList[ playlistSpot ] ].getTitle();	
}

Then call the function for each song on the list you want to see.
1
2
cout << "First song is: " << getSongNameFromPlaylist( 0 );// array indexes start at 0
cout << "Third song is: " << getSongNameFromPlaylist( 2 );
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.
closed account (D80DSL3A)
I like helping but I think you're in over your head with this problem.

Can you see why your addSongToPlaylist() is equivalent to this?
1
2
3
4
5
6
void addSongToPlaylist(int songNum)
{
    s = songNum;
    numberOfSongs += numberInPlaylist;
    break;
}

That's all it's doing.

I tried to explain what's wrong with the getSongNameFromPlaylist() in my last post.
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!

Can you see those two issues in your code?
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. :)

I will keep chasing the rabbit down his hole.

Thanks again for your time.
Topic archived. No new replies allowed.