Capacity of queue

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.

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
#include<iostream>
#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;
        }
        }  
Last edited on
line 33: Do not loop on (! stream.eof()) or (stream.good()). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> (or getline) operation as the condition in the while statement.
1
2
3
  while (stream >> var) // or while (getline(stream,var))
  {  //  Good operation
  }


Line 41: If you fail to open the file, you fall through and search the empty array.

Line 43: You're looping through the entire array, most of which are empty strings. You read only loop entries.
use <cstdlib> ... the one you used is for C programs. The difference won't matter until it does, but it can create issues in large projects due to namespaces.

consider using constexpr not #define. #defines are largely bad practice, with a few exceptions (include guards, debug macros, project settings (eg ifdef unix pull in unitstd ifdef windows pull in windows.h sorts of things) and the like.

it would be a little better to read the file apart from inserting. What if you wanted to later insert from typing it in? Its more flexible to split the two things, and easier to debug. If you do this, you can debug your file reader first, then do the insert and debug it knowing the file is correct. If you mix them together, you debug 2 things at once.
ok, so does the new fstream function pull the correct data in? Insert will never work until you have the data it needs. Post the new code will help us.
So here it is the the reading from file works and the arrays works
I'm still not sure how the queue works I added the output of contents in file to show that it works. Now I'm not sure how will the insert works and I'm guessing if it has something to do with size or the array.



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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include<iostream>
#include<string>
#include<cstdlib>
#include<fstream>
#include<queue>
using namespace std;
#define size 10 

class Playlist{
	
	private:
	int rear, front;
	string q[size];
	
	
	public:
	string choice;
	void Insert();
	void Delete();
	void Display();
	void search();
}; 
void Playlist::search()
{
	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()) //To read from the textfile and assign to array
    {
        while (getline (myfile,line)) 
        {
            array[loop] = line;
            loop++;
            cout<<line<<endl;
        }
     
        
}   
for(int i=0; i<100; i++)//Search for the song title in  array
        {
                if(array[i]==choice)
                {
                        cnt=1;
                        pos=i+1;
                        break;
                }
        }
        if(cnt==0)
        {
                cout<<"\n Song not Found..!!"<<endl;
        }
}
void Playlist::Insert()
{	
search();
	if(rear<size)
         {
        rear++;
        q[rear]=choice;
        }
        if(front==0)
        {
            front=1;
        }
    else cout<<"Queue is full"<<endl;
        }
}
void Playlist::Delete()
{

    if(front == 0) 
    {
        cout << "Queuy is Empty. Nothing to Delete!" << endl;
        return;
    }
    else
    {
        choice = q[front]; // delete the element that front is pointing to
        cout << "Element Deleted :" << choice << endl;
    }
    if(front == rear)
    {

        front = 0;
        rear = 0;

    }
    else
    {

        front = front + 1;

    }
}

void Playlist::Display() 
{

    if(front == 0)

        return;

    for(int i=front;i<=rear;i++)
    {

        cout << " " << q[i];

    }
        cout << "\n\n";
}

int main()
{
	system("COLOR 97");
    int ch; 
    Playlist Q;
    
    while (1) {
    	cout<<"Music Playlist Queue";
        cout<<"\n1.ADD SONG\n2.DELETE SONG\n3.SHOW PLAYLIST\n4.EXIT\n\nEnter the number of your choice: ";
		cin >> ch;
        switch (ch) {
        case 1:
            Q.Insert();
            break;
        case 2:
        	Q.Delete();
            break;
        case 3:
            Q.Display();
            break;
        case 4:
        	cout<<"\nThank  You!!!";
            exit(0);
        }
    }
    return 0;
}
Last edited on
Consider this:

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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class Playlist {
private:
	static constexpr size_t maxsize {3};

	size_t rear {}, front {};
	size_t num {};
	string q[maxsize];

public:
	string choice;

	bool Insert();
	bool Delete();
	void Display();
	bool search();
};

bool Playlist::search()
{
	cout << "Enter a song you want to add in queue\n";
	getline(cin, choice);

	ifstream myfile("Songs.txt");

	if (myfile.is_open())
		for (string line; getline(myfile, line);)
			if (line == choice)
				return true;

	cout << "\n Song not Found..!!\n";
	return false;
}

bool Playlist::Insert()
{
	if (num < maxsize) {
		if (search()) {
			q[rear] = choice;
			rear = (rear + 1) % maxsize;
			++num;
			return true;
		}
	} else
		cout << "Queue is full\n";

	return false;
}

bool Playlist::Delete()
{
	if (num == 0) {
		cout << "Queue is Empty. Nothing to Delete!\n";
		return false;
	}

	choice = q[front];
	cout << "Element Deleted :" << choice << '\n';

	front = (front + 1) % maxsize;
	--num;
	return true;
}

void Playlist::Display()
{
	if (num > 0)
		for (size_t i = front, n = 0; n < num; ++n, i = (i + 1) % maxsize)
			cout << " " << q[i];

	cout << "\n\n";
}

int main()
{
	//system("COLOR 97");
	Playlist Q;

	while (true) {
		int ch {};
		cout << "Music Playlist Queue";
		cout << "\n1.ADD SONG\n2.DELETE SONG\n3.SHOW PLAYLIST\n4.EXIT\n\nEnter the number of your choice: ";
		cin >> ch;
		cin.ignore();

		switch (ch) {
			case 1:
				Q.Insert();
				break;

			case 2:
				Q.Delete();
				break;

			case 3:
				Q.Display();
				break;

			case 4:
				cout << "\nThank  You!!!";
				return 0;
		}
	}
}

Original question was by https://www.cplusplus.com/user/Loookster/
Only saved information I can find is:
I'm currently making a mock up playlist using queue but I have this problem wherein It won't read up [...]
@seeplus Thank you so much and the others who helped I'll try to study the constexpr since its new to me. Btw, I fixed the Insert function again by deleting the %maxsize because if it reaches the max size it will loop infinitely.

I also created a new account since my account got reported :V
Last edited on
Try your old account again, it appears to have been restored.

Or keep using your new one. Looks like the site admin took out a lot of trash.
Topic archived. No new replies allowed.