I am shuffling songs for my program but im a little confused because when I try the compiler tells me I cant compare my struct to an int. Im wondering what yall might think?
struct Songs //my struct
{
string title;
string artist;
string mem;
};
Songs *ptr;
ptr = new Songs[25]; //dynamic array
so i told u the struct and ptr well heres the function im experiencing trouble..
void shuffle (Songs song[], Songs *ptr, string title, string mem, string artist, int num)
{
for (int i=0; i<(num); i++)
{
int r = i + (rand() % (num-i)); // Random remaining position.
int temp = ptr[i]; ptr[i] = ptr[r]; ptr[r] = temp; //this isnt working
} //but its logically sound?
for (int c=0; c<n; c++)
{
cout << ptr[c] << " "; // Just print
}
}
The offending code is at
int temp = ptr[i]; ... ptr[r] = temp;, you’re assigningSongandintwhich is not possible.Additionally, I strongly suggest using
std::vector< Song >for storage. Your code is more robust and will crash less likely, plus the vector always knows the number of Songs it contains. ExamplemySongs.size()contains the number of songs, and you can access each song withmySongs[index](or bettermySongs.at(index)) as expected. Adding new songs is done bymySongs.push_back(someSong).Now to your question: How do I shuffle my vector of songs. Well …
does the trick. See here.
Writing a song to a stream can be done by defining a function like this:
Now you can happily do
std::cout << mySong << std::endl.