outputs the same song over and over

outputs the same song over and over and they outputs blank

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

//libraries
#include <cstdlib> //random number usage
#include <ctime> //random number usage
#include <deque> //for deque
#include <iostream> //console IO
#include <fstream> //file IO
using namespace 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
  const int 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
Last edited on
1
2
3
4
5
6
7
8
9
$ 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
       ^

Assignment of a non-zero value is always true.

Use == instead.

thank you!
Topic archived. No new replies allowed.