Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to open random files?

How to open random files?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++tutoriallounge
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DaveE9th
    wrote on last edited by
    #1

    I'm working with the code below and want to make a jukebox. How can I play one of several songs randomly? #include stdlib.h int main() { system( "start c:\\cpp\\pickmeup.mid" ); /* or just system( "c:\\cpp\\pickmeup" ); */ return 0; } Thanks, Dave :-D

    "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

    K 1 Reply Last reply
    0
    • D DaveE9th

      I'm working with the code below and want to make a jukebox. How can I play one of several songs randomly? #include stdlib.h int main() { system( "start c:\\cpp\\pickmeup.mid" ); /* or just system( "c:\\cpp\\pickmeup" ); */ return 0; } Thanks, Dave :-D

      "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

      K Offline
      K Offline
      kochhar
      wrote on last edited by
      #2

      (1) Read all song (file) names into an array (say N items) (2) Pick a random number between 1 and N and use that entry from your array.

      D 1 Reply Last reply
      0
      • K kochhar

        (1) Read all song (file) names into an array (say N items) (2) Pick a random number between 1 and N and use that entry from your array.

        D Offline
        D Offline
        DaveE9th
        wrote on last edited by
        #3

        I was hoping to find some code to generate a random number. I can use a switch statement (or an array as you mentioned), but I don't know how to generate a random number. Any ideas? Thanks in advance, Dave :-D

        "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

        K 1 Reply Last reply
        0
        • D DaveE9th

          I was hoping to find some code to generate a random number. I can use a switch statement (or an array as you mentioned), but I don't know how to generate a random number. Any ideas? Thanks in advance, Dave :-D

          "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

          K Offline
          K Offline
          kochhar
          wrote on last edited by
          #4

          OK, here's what you need to do: (1) Somewhere near the start of your program, seed the random number generator using time of day. This ensures that you don't get the same numbers every time: srand((unsigned)time(NULL)); (2) Then, every time you need a random number (betrween 0 and iN) ifile=(rand() % (unsigned)(iN)); That should do it.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups