0

I'm currently making a mock up playlist using queue but I have this problem wherein It won't read up to 5 title from my textfile song directory rather it only reads up to 1 or 3 title depending on the length of the song title. I tried changing the size but it doesn't seems to work.

#include<string>
#include<stdlib.h>
#include<fstream>
using namespace std;
#define size 10

class Playlist{
    
    public:
    int rear, front;
    string q[size];
    string choice;
    
    void Insert();
    void Delete();
    void Display();
}; 

void Playlist::Insert()
{    
    cout<<"Enter a song you want to add in queue"<<endl;
    cin.ignore();
    getline(cin,choice);
    int cnt=0;
    int pos;
    string array[100]; 
    short loop=0; 
    string line; 
    ifstream myfile ("Songs.txt"); 
    if (myfile.is_open()) 
    {
        while (! myfile.eof() ) 
        {
            getline (myfile,line); 
            array[loop] = line;
            loop++;
        }
        myfile.close(); 
    }
    else cout << "can't open the file"; 
    
    for(int i=0; i<100; i++)
        {
                if(array[i]==choice)
                {
                        cnt=1;
                        pos=i+1;
                        break;
                }
        }
        if(cnt==0)
        {
                cout<<"\n Song not Found..!!"<<endl;
        }
        else
        {
             if(rear<size)
         {
        rear++;
        q[rear]=choice;
        
        }
        if(front==0)
        {
            front=1;
        }
    else cout<<"Queue is full"<<endl;
        }
        }```
  • To begin with, your usage of `while (! myfile.eof() ) ` is [wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) and you have to check if reading is successful *before* using what is "read". – MikeCAT Jan 06 '21 at 13:53
  • It will be helping if you can format your code with good indentation, it will help the audience to read & understand the problem quickly, hence you will get your answer more quickly. – Hassan Akram Jan 06 '21 at 14:16
  • 1
    What are the results of your debugging session? Which statement is causing the issue? What are the actual values in the variables? What are the expected values in the variables? – Thomas Matthews Jan 06 '21 at 16:35

0 Answers0