//libraries
#include <cstdlib> //random number usage
#include <ctime> //random number usage
#include <deque> //for deque
#include <iostream> //console IO
#include <fstream> //file IO
usingnamespace std;
//Programmer defined data types
struct shuffle
{
string songNames; //song names from file
int songPlayed; // = 0; // 0 = not played, 1 = played
};
//Special compiler dependent definitions
//NONE
//global constants/variables
//NONE
//Programmer defined functions
void introduction(string obj, string ins); //user introduction
bool playedLst(int rndNmb, shuffle *mySongs); //checking and adding song to the play list
//main program
int main()
{
//Data
srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
string objective = "shuffle music."; //program objective
string instructions = "Start shuffle"; //user instructions
constint MAX_MYSONGS = 200; //list size
int nmySongs = 0; //list capcacity
shuffle mySongs[MAX_MYSONGS]; //song array
int rndNmb; //random song
int i = 0; //index loop
char letsGo; //continue?
//user introduction
introduction(objective, instructions);
// open first input file
ifstream fin; //reserve "fin" for use
fin.open("songs.txt"); //open specifed file
if (!fin.good()) throw"I/O error"; //if file isnt open output error to console
//create a deque objective
deque<int> iDeque(5);
while (true)
{
if (!fin.good()) break; //if there are no more lines to input break
getline(fin, mySongs[nmySongs].songNames); // get song name from file and store
nmySongs++; //indicated that we added a song
}
//close first input file
fin.close();
do
{
rndNmb = 0 + rand() % nmySongs; //picking a random song
//check song
if (playedLst(rndNmb, mySongs)) //checking if its been played in the last 5
{
iDeque.push_back(rndNmb); //add to empty slot
mySongs[i].songPlayed = rndNmb; //note song
i++; //indicated a song is read to be played
if (i = 5) //if history is full reset index
{
iDeque.pop_front(); //if history is full drop oldest entry
i = 0;//index reset
}
cout <<"Play a song? [Y/N]? " << endl; //prompt user
cin >> letsGo; //store input
cin.ignore(1000, 10);
//output
if (letsGo == 'Y' || letsGo == 'y') //check input
{
cout <<"NOW PLAYING: " << mySongs[rndNmb].songNames << endl; //output song
cout << endl;
}
}
}while (toupper(letsGo) == 'Y' && toupper(letsGo) != 'N'); //stop if user said so
}//main
// user introduction
void introduction(string obj, string ins)
{
//data
//obj is the program objective
//ins is the user instructions
//output user introduction
cout << "Objective: This program will " << obj << endl;
cout << "Programmer: Prosper\n";
cout << "Editor(s) used: Notepad\n";
cout << "Compiler(s) used: TDM MinGW\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl;
cout << ins << endl << endl;
}//introduction
//list of played songs
bool playedLst(int rndNmb, shuffle *mySongs)
{
//data
//rndNmb is song being tested
bool result = false; //false is not a duplicate, true is a duplicate guess
int i; //loop index
for (int i = 0; i < 5; i++)
{
if (rndNmb == mySongs[i].songPlayed) //check song to list
{
result = true; //is a dupe
}//if
}//for i
return result;
}//playedLst
$ g++ -Wall -Wextra -O2 foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:70:16: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if (i = 5) //if history is full reset index
^
foo.cpp: In function ‘bool playedLst(int, shuffle*)’:
foo.cpp:112:7: warning: unused variable ‘i’ [-Wunused-variable]
int i; //loop index
^