Why Queue can't take(push) a string??
Feb 25, 2016 at 12:22pm UTC
So it seems that this queue can't take an
array of
strings ?
I am suffering in making the "
" to be strings....and I am even suffering
in making the strings in an array and then the program just picks the names (strings)
randomly from the array.
this is the code:
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
#include<iostream>
using namespace std;
class queue {
public :
void queue_init(int size);
void push(char x);
void pop();
int size();
char front();
char back();
int Empty();
int full();
private :
char * queue_ptr;
int max_len;
int rear_cnt;
int front_cnt;
};
int queue::size() {
return (rear_cnt - front_cnt);
}
void queue::queue_init(int arr_size) {
queue_ptr = new char [arr_size];
max_len = arr_size - 1;
front_cnt = 0;
rear_cnt = 0;
}
int queue::Empty() {
return (rear_cnt == front_cnt);
}
int queue::full() {
return (rear_cnt == max_len + 1);
}
char queue::front() {
return (queue_ptr[front_cnt]);
}
char queue::back() {
return (queue_ptr[rear_cnt - 1]);
}
void queue::push(char X) {
if (rear_cnt == max_len + 1)
cout << "Error queue is full \n" ;
else
{
queue_ptr[rear_cnt] = X;
rear_cnt++;
}
}
void queue::pop() {
if (Empty())
cout << "Error queue is empty\n" ;
else
{
queue_ptr[front_cnt] = 0;
front_cnt++;
}
}
int main(){
queue Q;
int time = 810;
Q.queue_init(10);
char a = 'a' , b = 'b' , c = 'c' , d = 'd' , e = 'e' , f = 'f' , g = 'g' , h = 'h' , j = 'j' , k = 'k' , l = 'l' ;
cout << "This program will calculate the customer service throughout the next 30 minutes.\n"
"Since a customer enters Rajhi Bank every 3 minutes, therefore the size of the queue should be 10 or a possible 11." << endl;
cout << "The customer service opens at 8:10 am" << endl;
for (int i = 0; i <= 30; i++)
{
if (i == 0) {
cout << "it is 8:10 am. The first customer just arrived." << endl;
Q.push(a);
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
cout << " ************************************************************************* " << endl;
}
if (i == 3) {
cout << "it is 8:13 am. The second customer just arrived." << endl;
Q.push(b);
cout << "There are: " ; Q.size(); cout << "waiting on line currently." << endl;
cout << "currently serving: " ; Q.front(); cout << endl;
cout << " ************************************************************************* " << endl;
}
if (i == 5){
cout << "it is 8: 15 am. The first customer is served." << endl;
Q.pop();
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
cout << "currently serving: " ; Q.front(); cout << endl;
cout << " ************************************************************************* " << endl;
}
if (i == 6){
cout << "it is 8: 16 am. The third customer just arrived." << endl;
Q.push(c);
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
cout << "currently serving: " ; Q.front(); cout << endl;
cout << " ************************************************************************* " << endl;
}
if (i == 9){
cout << "it is 8: 19 am. The fourth customer just arrived." << endl;
Q.push(d);
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
cout << "currently serving: " ; Q.front(); cout << endl;
cout << " ************************************************************************* " << endl;
}
if (i == 10){
cout << "it is 8: 20 am. The second customer is served." << endl;
Q.pop();
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
cout << "currently serving: " ; Q.front(); cout << endl;
cout << " ************************************************************************* " << endl;
}
if (i == 12){
cout << "it is 8: 22 am. The fifth customer just arrived." << endl;
Q.push(e);
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
cout << "currently serving: " ; Q.front(); cout << endl;
cout << " ************************************************************************* " << endl;
}
if (i == 15){
cout << "it is 8: 25 am. The sixth customer just arrived." << endl;
Q.push(f);
cout << "and the third customer just got served." ; Q.pop(); cout << endl;
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
cout << "currently serving: " ; Q.front(); cout << endl;
cout << " ************************************************************************* " << endl;
}
if (i == 18){
cout << "it is 8: 28 am. The seventh customer just arrived." << endl;
Q.push(g);
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
cout << "currently serving: " ; Q.front(); cout << endl;
cout << " ************************************************************************* " << endl;
}
if (i == 20){
cout << "it is 8: 30 am. The fourth customer just got served." << endl;
Q.pop();
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
cout << "currently serving: " ; Q.front(); cout << endl;
cout << " ************************************************************************* " << endl;
}
if (i == 21){
cout << "it is 8: 31 am. The eighth customer just arrived." << endl;
Q.push(h);
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
cout << "currently serving: " ; Q.front(); cout << endl;
cout << " ************************************************************************* " << endl;
}
if (i == 24){
cout << "it is 8: 34 am. The ninth customer just arrived." << endl;
Q.push(j);
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
cout << "currently serving: " ; Q.front(); cout << endl;
cout << " ************************************************************************* " << endl;
}
if (i == 25){
cout << "it is 8: 35 am. The fifth customer just got served." << endl;
Q.pop();
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
cout << "currently serving: " ; Q.front(); cout << endl;
cout << " ************************************************************************* " << endl;
}
if (i == 27){
cout << "it is 8: 37 am. The tenth customer just arrived." << endl;
Q.push(j);
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
cout << "currently serving: " ; Q.front(); cout << endl;
cout << " ************************************************************************* " << endl;
}
if (i == 30){
cout << "it is 8: 40 am. The eleventh customer just arrived." << endl;
Q.push(l);
cout << "and the sixth customer just got served." ; Q.pop(); cout << endl;
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
cout << "currently serving: " ; Q.front(); cout << endl;
cout << " ************************************************************************* " << endl;
}
}
return 0;
}
Last edited on Feb 25, 2016 at 12:23pm UTC
Feb 25, 2016 at 3:28pm UTC
So it seems that this queue can't take an array of strings?
That is correct. It uses, as you should know since you wrote the code, an array of char.
Feb 25, 2016 at 5:38pm UTC
Consider using std::vector<std::string>
Change all these:
1 2 3
cout << "it is 8:10 am. The first customer just arrived." << endl;
Q.push(a);
cout << "There are: " ; Q.size(); cout << "waiting on line currently" << endl;
to
1 2 3
cout << "it is 8:10 am. The first customer just arrived." << endl;
Q.push(a);
cout << "There are: " << Q.size() << " waiting on line currently\n" ;
For some reason, the size function doesn't work. The counters seem ok though.
Now for the elephant in the room: You really should think up a better way of doing things with lines 81 to 191. How much code would you write if you had 1 customer per minute for the whole day? Would you have 3360 or more lines of code?
Can you figure out how to use a function to do that?
Feb 25, 2016 at 8:57pm UTC
Yes
and
, sorry about that.
So yeah, there you go, this is the code that I was trying to show you.
the
in the push function is reflecting an error that I don't understand.
and in the nested for loop in the int main, you can see that I am trying to tell the program
that when
is incremented three times, it should push a new
(string)
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
#include<iostream>
#include<string>
using namespace std;
class queue {
public :
void queue_init(int size);
void push(string x);
void pop();
int size();
char front();
char back();
int Empty();
int full();
private :
char * queue_ptr;
int max_len;
int rear_cnt;
int front_cnt;
};
int queue::size() {
return (rear_cnt - front_cnt);
}
void queue::queue_init(int arr_size) {
queue_ptr = new char [arr_size];
max_len = arr_size - 1;
front_cnt = 0;
rear_cnt = 0;
}
int queue::Empty() {
return (rear_cnt == front_cnt);
}
int queue::full() {
return (rear_cnt == max_len + 1);
}
char queue::front() {
return (queue_ptr[front_cnt]);
}
char queue::back() {
return (queue_ptr[rear_cnt - 1]);
}
void queue::push(string X) {
if (rear_cnt == max_len + 1)
std::cout << "Error queue is full \n" ;
else
{
queue_ptr[rear_cnt] = X;
rear_cnt++;
}
}
void queue::pop() {
if (Empty())
std::cout << "Error queue is empty\n" ;
else
{
queue_ptr[front_cnt] = 0;
front_cnt++;
}
}
int main(){
queue Q;
Q.queue_init(10);
const int array_size = 11;
string customer[array_size] = {"George" , "Ahmed" , "Abdullah" , "Jonah" , "Lamar" , "Ronan" , "Ali" , "Peanuts" , "James" , "Omar" , "Yassir" };
std::cout << "This program will calculate the customer service throughout the next 30 minutes.\n"
"Since a customer enters Rajhi Bank every 3 minutes, therefore the size of the queue should be 10 or a possible 11." << endl;
std::cout << "The customer service opens at 8:10 am" << endl;
for (int i = 0; i <= 30; i++) {
for (int j = 0; j <= (i + 3); j++){
Q.push(customer[array_size]);
std::cout << "There are: " ; Q.size(); std::cout << "waiting on line currently" << endl;
}
}
return 0;
}
Topic archived. No new replies allowed.