### FIFO queue demonstration Hi all,
Im having some trouble figuring out how to complete a assignment in C++ involving queue, array and FIFO. Hoping someone can point me in the right direction.

Just to give some clarity, the assignment is to demonstrate how a FIFO works.

The queue elements have point coordinates i.e. each element has 2 numbers e.g. 3,4 . (with the comma from what I understand).

And then you have to create a method that allows the user to enter (queue) two new numbers and adds it to the array queue.
Another (dequeue) method that removes the first element from the queue
And a final (show) method that displays the remaining elements in a {x, y}; {x, y}; ... fashion.

Im having trouble understanding how to allow the user to save two numbers with a comma in a signle array element

Right now I have somewhat of a start going. This is a queue system based on humans (single integer format)
Main:
 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include #include "queue.h" using namespace std; #define HUMAN 1 int main(){ int size; cout << "Please enter the queue size: " <> size; queue q(size); q.enqueue(HUMAN); q.enqueue(HUMAN); q.enqueue(HUMAN); cout << "Maximum size of the queue is: " << size<

Then queue.h:
 ``123456789101112131415161718192021222324`` ``````#include #ifndef QUEUE_H #define QUEUE_H using namespace std; class queue{ int *arr; // array int capacity; // max size int front; // first element in the queue int rear; // last element in queue int count; // current size of the queue public: queue(int size); //constructor void dequeue(); void enqueue(int x); int size(); bool isEmpty(); bool isFull(); }; #endif // QUEUE_H ``````

and queue.cpp:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````#include #include "queue.h" // Konstruktors queue::queue(int size){ arr = new int[size]; capacity = size; front = 0; rear = -1; count = 0; } // Method to remove from the queue void queue::dequeue(){ // Checks if queue is empty, if is, then terminates if (isEmpty()) { cout << "Queue is empty"; exit(EXIT_FAILURE); } cout << "Dequeing " << arr[front] << '\n'; front = (front + 1) % capacity; count--; } // Method that adds to the queue void queue::enqueue(int item){ // Checks if queue is full if (isFull()) { cout << "Queue is full"; exit(EXIT_FAILURE); } cout << "Entering" << item << '\n'; rear = (rear + 1) % capacity; arr[rear] = item; count++; } // returns queue size int queue::size(){ return count; } bool queue::isEmpty(){ return (size() == 0); } // checks if queue is full bool queue::isFull(){ return (size() == capacity); } ``````

When using the program, it should look something like this:

 ``123456789101112131415161718192021222324`` ``````Please enter the size of the queue: 15 What would you like to do? + (positive being they have to enter two new numbers) Please enter the coordinates to be saved: 5,4 What would you like to do? + (again) Please enter the coordinates to be saved: 3,5 What would you like to do? * (star being show method) (Show method) The current Queue is: {5,4}, {3,5}; What would you like to do? - (negative being dequeue) (Show method) The current Queue is: {3,5}; What would you like to do? + (positive being they have to enter two new numbers) Please enter the coordinates to be saved: 7,8 (Show method) The current Queue is: {3,5}; {7,8}; And so forth. I hope this explains the end results.``````

This is far from complete, and I will make the changes that will ask to enter the coordinate numbers at a later stage, once I understand how to save multiple integers with commas in a single array element.

Thank you for any help
Last edited on Actually, I realised I was being silly, I can simply use strings rather than integers...

So I've remade it to this:

main.cpp:
 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include #include "queue.h" using namespace std; int main() { string choice; int length; cout << "What is the queues max length: " <> length; queue q(length); do{ cout <<"\n"; cout << "Please choose an option "; cout <<"\n"; cin >> choice; if (choice== "+"){ q.enqueue(); } if (choice== "-"){ q.dequeue(); } /*if (choice== "*"){ q.show(); }*/ if (choice== "\$"){ exit(EXIT_FAILURE); } }while (choice!= "\$"); return 0; }``````

queue.h
 ``1234567891011121314151617181920212223242526`` ``````#include #ifndef QUEUE_H #define QUEUE_H using namespace std; class queue{ int *arr; // array int capacity; // max int front; // first in queue int rear; // last in queue int count; //current size public: queue(int length); //constructor void dequeue(); enqueue(); int size(); bool isEmpty(); bool isFull(); string show(); }; #endif // QUEUE_H ``````

and queue.cpp:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768`` ``````#include #include "queue.h" // Constructor queue::queue(int length){ arr = new int[length]; capacity = length; count = 0; } // Remove from first void queue::dequeue(){ // Checks if theres something to remove if (isEmpty()) { cout << "Queue is empty"; exit(EXIT_FAILURE); } cout << "Removing " << arr << '\n'; // -------- This part doesnt work, it returns a random number rather than the value that was removed?? front = (front + 1) % capacity; count--; } // Add to queue queue::enqueue(){ // Checks if full if (isFull()) { cout << "Queue is full"; exit(EXIT_FAILURE); } string coordinates; cout << "Please enter the coordinates (e.g. 4,5): "; cin >> coordinates; cout << "Entering " << coordinates<< '\n'; rear = (rear + 1) % capacity; count++; } // Retunrs queue zise int queue::size(){ return count; } bool queue::isEmpty(){ return (size() == 0); } // Checks if full bool queue::isFull(){ return (size() == capacity); } //Show method that shows the current aray string queue::show(){ }``````

Can someone help with how i would use the 'show' method to print out the current queue array? The queue elements have point coordinates i.e. each element has 2 numbers e.g. 3,4 . (with the comma from what I understand). […] Im having trouble understanding how to allow the user to save two numbers with a comma in a signle array element

I’m afraid you can’t. Apart from the comma or the semicolon, inside the queue you can’t use a simple C-style int array to store those data.
Storing a C-style array of std::pair<int, int> could be a possible solution.

Also if you store the elements inside a C-style array, you don’t need such information as the ‘front’ ad the ‘rear’, since you can use int indexes to identify them.

 …I realised … I can simply use strings rather than integers...

You can definitely read them from the user as std::strings, but, if the assignment says you need to store them as ‘numbers’, you’d better not store std::strings or you could get a bad mark.

 `front = (front + 1) % capacity;`

Here it seems you want to create a circular queue… Do you?

 Just to give some clarity, the assignment is to demonstrate how a FIFO works.

I think it would be better if you posted the entire assignment. The 'assignment':

Explore FIFO queue operation. Develop a FIFO queue action demonstration program.
Realize the queue as an array or as a related list. Queue elements - x and y coordinates of a point (positive integers with a sign). `// I suppose this part means that we have to use integers? But I think I can get away with using strings tbh `

Create a queue object class.
• Create a constructor with the ability to specify the maximum length of a queue.

Write methods for working with queue:
• Push - puts items in a row;
• Pop - removes an item from the queue;
• Show - displays the queue as a series separated by a semicolon: {x, y}; {x, y}; ...

Demo program user interface (commands):
• + x, y [Enter] - insert the coordinates of a point in a queue;
• - [Enter] - remove an item from the queue and display it on the screen;
• * [Enter] - display the queue on the screen;
• \$ [Enter] - exit the program;
• The first character you enter, other than [+ - * \$], is an error message. If the text you enter is not a number, then an error message is displayed.

`front = (front + 1) % capacity;` This was made for a different assignment so it might have some remnants of the old code. This is useless now I believe.

Any suggestions on how anyone else would tackle this assignment? i.e. what kind of methods, or structures, or arrays you would use?
I simply thought I could adapt a basic queue code I found online to my needs...
Last edited on I could adapt a basic queue code I found online

That’s a confession! :-D

Well, it seems your teacher proved cannier than you: s/he made things a bit more complicated :-)

 Queue elements - x and y coordinates of a point (positive integers with a sign)

You need to store something like:
 ``123456`` ``````std::pair // or struct Coord { int x; int y; };``````

 what kind of methods, or structures, or arrays you would use?

 ``123456`` ``````class Queue { // or whatever name you like Queue(int length); /* type? */ push ( /* parameters? */); /* type? */ pop ( /* parameters? */); /* type? */ show ( /* parameters? */) const; };``````

(Anyway, if you can use std::array instead of C-style arrays, I think it would be better) @Enoizat Im not that experiences with strucures, but I think I get the whole jist.
And this is what I have so far:

Main.cpp:
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include #include #include "queue.h" using namespace std; int main() { string choice; queue q; do{ cout <<"\n"; cout << "Please choose an option: "; cout <<"\n"; cin >> choice; cout <<"\n"; if (choice== "+"){ q.push(); } if (choice== "-"){ q.pop(); } if (izvele == "*"){ q.show(); } if (choice== "\$"){ exit(EXIT_FAILURE); } }while (choice!= "\$"); return 0; }``````

queue.h:
 ``123456789101112131415161718`` ``````#ifndef QUEUE_H #define QUEUE_H #include class queue { public: queue(); virtual ~queue(); void push(); void pop(); void show() const; private: int capacity; }; #endif // QUEUE_H ``````

and queue.cpp:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````#include #include "queue.h" #include using namespace std; queue::queue(){ int length; cout <<"Queue max length: "; cin >> length; cout <<"\n"; int array[length]; capacity = length; } queue::~queue() { //dtor } void queue::push(){ struct Coordinates{ //And this whole part wont work either cause I need to create a structure before I can enter data into it. //I assume I need to use a for loop in order to createa strcutre everytype the 'push' method is called in order to create a new structure every time? int x; int y; }arr[capacity]; for (i = 0; i> Coordinates.x >> Coordinates.y; cout <<"\n" << "You entered: " <

If i understand this correctly, the whole idea with the struct is to have two values for a single structure. And the amount of structures will be defined by an array that is created once the user enters a value. Please correct me if Im way off?

And please point out any other mistakes Im making, or if theres easier ways to do something.
Last edited on ``1234`` ``````struct Coordinates{ int x; int y; }arr[capacity];``````

That’s a start, but I’d declare that before, so that the array can become a property of the class.
For example:
 ``123456789101112`` ``````struct Coordinates { int x {}; int y {}; }; class queue { public: queue(); private: Coordinates * arr; };``````

or:
 ``123456789101112`` ``````class queue { public: struct Coordinates { int x {}; int y {}; }; queue(); private: Coordinates * arr; };`````` I feel like im taking 1 step forward, 2 steps back at this stage..

Soo I figured I could do something like this:
 ``12345678910111213141516171819202122`` ``````void queue::push(){ int x, y; if (isFull()){ cout << "Queue is full."; return; } struct Coordinates coord[capacity]; int i; for (i = 0; i> coord[i].x; cout <<"Enter coordinate y: "; cin >> coord[i].y; //return; ---- If i dont use return, it also continues asking the coordinates until the full capacity is reached - this is not the goal, we only want to add to it if the user selects push method. } for (i=0; i

This would in essence ask to enter the two values for each structure based on the size of the queue that was entered.
However, this is contained within this method and isnt global, and therefore it doesnt make the check 'isFull' and doesnt save the entered data outside of this method.

How would I go about making sure the entered data is saved in the structure, so it can be accessed outside of this method? (I hope that makes sense)

I assume I would have to save the created coordinates to a seperate property of the class, but I have no idea how that would look
Last edited on ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115`` ``````#include #include struct queue { struct coordinates { int x ; int y ; }; // Create a constructor with the ability to specify the maximum length of a queue. explicit queue( std::size_t max_size ) : max_sz(max_size) {} // Push - puts items in to the queue void push( coordinates coord ) { if( q.size() < max_sz ) q.push_back(coord) ; else throw "queue is full" ; } // Pop - removes an item from the queue and return what was removed coordinates pop() { if( !q.empty() ) { const coordinates f = q.front() ; q.pop_front() ; return f ; } else throw "queue is empty" ; } // Show - displays the queue as a series separated by a semicolon: {x, y}; {x, y} void show() const { for( coordinates c : q ) std::cout << '{' << c.x << ',' << c.y << "}; " ; std::cout << '\n' ; } private: std::size_t max_sz ; std::deque q ; }; int main() { std::size_t max_sz ; std::cout << "max size? " ; std::cin >> max_sz ; queue fifo(max_sz) ; char command ; do { std::cout << "\ncommands:\n----------\n" << "+ x, y [Enter] - insert the coordinates of a point in a queue\n" << "- [Enter] - remove an item from the queue and display it on the screen\n" << "* [Enter] - display the queue on the screen\n" << "\$ [Enter] - exit the program\n" << "\nenter command: " ; std::cin >> command ; switch(command) { case '+' : { queue::coordinates c ; char comma ; if( std::cin >> c.x >> comma >> c.y && comma == ',' ) { try { fifo.push(c) ; std::cout << "pushed {" << c.x << ',' << c.y << "}\n" ; } catch( const char* emsg ) { std::cout << "***error: " << emsg << '\n' ; } } else { // If the text you enter is not a number, then an error message is displayed. std::cout << "***error: coordinates: invalid input\n" ; std::cin.clear() ; std::cin.ignore( 1000, '\n' ) ; } break ; } case '-' : { try { const queue::coordinates c = fifo.pop() ; std::cout << "popped {" << c.x << ',' << c.y << "}\n" ; } catch( const char* emsg ) { std::cout << "***error: " << emsg << '\n' ; } break ; } case '*' : fifo.show() ; break ; case '\$' : break ; default: // The first character you enter, other than [+ - * \$], is an error message std::cout << "*** error: invalid command\n" ; std::cin.clear() ; std::cin.ignore( 1000, '\n' ) ; } } while( command != '\$' ) ; }`````` @JLBorges hah, thanks for this. I think I understand the whole process of it