How to open random files?
-
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
-
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
-
(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.
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
-
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
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.