Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7570993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:33:29+00:00 2026-05-30T15:33:29+00:00

I am shuffling songs for my program but im a little confused because when

  • 0

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 
 }      
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:33:31+00:00Added an answer on May 30, 2026 at 3:33 pm

    The offending code is at int temp = ptr[i]; ... ptr[r] = temp;, you’re assigning Song and int which 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. Example

    #include <vector>
    ...
    struct Song { ... };
    ...
    void shuffle(std::vector< Song >& mySongs, ...)
    {
       /* shuffle mySongs somehow. */
       ...
    }
    

    mySongs.size() contains the number of songs, and you can access each song with mySongs[index] (or better mySongs.at(index)) as expected. Adding new songs is done by mySongs.push_back(someSong).

    Now to your question: How do I shuffle my vector of songs. Well …

    /* at start of program. */
    srand(unsigned(time(NULL)));
    ...
    void shuffle(std::vector< Song >& mySongs)
    {
        std::random_shuffle(mySongs.begin(), mySongs.end());
    }
    

    does the trick. See here.

    Writing a song to a stream can be done by defining a function like this:

    std::ostream& operator << (std::ostream& osr, const Song& mySong)
    {
        osr << mySong.title << ' ' << mySong.artitst << ' ' << mySong.mem;
        return osr;
    }
    

    Now you can happily do std::cout << mySong << std::endl.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this code online for shuffling array elements, it works well, but I
I have an object that i've serialised but I want to run a shuffling
While writing a card shuffling algorithm I realized that there are 52! ~= 2^225
I'm getting the following error after shuffling an array, then trying to loop through
All examples I find on shuffling arrays are for NSMutableArrays. Copying an NSArray to
Given a list of elements, does a shuffling algorithm exist that will guarantee that
Like the str_shuffle() function in PHP, is there a function similar in shuffling the
Here's a C implementation of Fisher-Yates that I want to use in a deck-shuffling
A little background on the application that I am gonna talk about in the
I have an app which supports shuffling. I am not sure if I should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.