0

I dequeue a queue and if the employee's salary is less than 50,000. I am not sure how to enqueue it into another queue as my enqueue function takes three parameters. My assignment says to create a class and then two queues in main. I made the queues being an object of the class, is this correct? How do I enqueue into the second queue with only having one enqueue function in the class which takes three parameters. Thanks for all the help.

#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::fixed;
using std::setprecision;

struct node{
    string name;
    int id;
    int salary;
    struct node *next;
};

node *rear;
node *front;

class DynEmpQueue{
private:
    int counter = 0;
public:
    void enqueue(string, int, int);
    void dequeue();
    void traverse()const;
    DynEmpQueue()
    {
        rear = nullptr;
        front = nullptr;
        counter = 0;
    }
};

void DynEmpQueue::enqueue(string localName, int localID, int localSalary)
{
    node *temp;
    temp = new (struct node);
    temp -> name = localName;
    temp -> id = localID;
    temp -> salary = localSalary;
    temp -> next = nullptr;
    if (front == nullptr)
        front = temp;
    else
        rear -> next = temp;
    rear = temp;
    counter++;
}

void DynEmpQueue::dequeue()
{
    string localName;
    int localID;
    int localSalary;
    node *temp;
    if (front == nullptr)
        cout << "The queue is empty.";
    else
    {
        temp = front;
        localName = temp -> name;
        localID = temp -> id;
        localSalary = temp -> salary;
        front = front -> next;
        delete temp;
        counter--;
    }
}

void DynEmpQueue::traverse()const
{
    node *temp;
    temp = front;
    if (front == nullptr)
        cout << "Queue is empty.";
    else
    {
        cout << "Queue contains " << counter << " elements." << endl;
        cout << "Queue elements:" << endl;
        while (temp != nullptr)
        {
            cout << temp -> name << "\t" << temp -> id << "\t" << temp -> salary << endl;
            temp = temp -> next;
        }
    }
}

int main()
{
    const int NumberEmployees = 5;
    DynEmpQueue originalQueue;

    originalQueue.enqueue("Justin Gray", 100, 104000);
    originalQueue.enqueue("Mike Smith", 200, 207000);
    originalQueue.enqueue("Jose Cans", 400, 47000);
    originalQueue.enqueue("Auston Matts", 300, 31000);
    originalQueue.enqueue("Liz Learnerd", 600, 89100);

    node object;
    DynEmpQueue demandSalaryIncrease;

    for (int i = 0; i < NumberEmployees; i++)
    {
        originalQueue.dequeue();
        if (object.salary <= 50000)
            demandSalaryIncrease.enqueue();
    }

    demandSalaryIncrease.traverse();

    return 0;
}
hockey34
  • 3
  • 1
  • 4
  • 1
    Something that strikes me is that you have global variables `front` and `rear`. Why global variables? I would tend to think that the `front` and `rear` nodes belong to an instance of the queue class, rather than a translation unit. – adentinger Apr 24 '17 at 22:17
  • I have them as global variables as that is how they have always been set up in class. Should I search each node that is dequeued so that I can see if their salary is above or below 50,000? – hockey34 Apr 24 '17 at 22:21
  • Your dequeue operation needlessly stores the resulting pull into local data, then discards it. If you're going to pull something off the queue, perhaps store it somewhere first. It would seem you need a `front()` action, as well as an `empty()` state check. – WhozCraig Apr 24 '17 at 22:22
  • @hockey34 with `front` and `rear` as globals think on what happens to `originalQueue`'s list when you construct `demandSalaryIncrease` and `front = nullptr;` executes. – user4581301 Apr 24 '17 at 22:45

1 Answers1

0

You cannot know what employees exist in your queue. Look at how you defined your methods:

void enqueue(string, int, int);
void dequeue();
void traverse() const;

As you can see, no method returns either a node or the employee's data. So, as you currently declared the class, there is no way to get employees from your queue. And, since you can't even get employees are in the queue, you cannot add them into another queue.

Possible solution:

Modify your traverse() method so that it takes a salary as argument and returns an array (or even a queue) containing all the employees for which the salary is lower than that salary.

A better and much more flexible solution would be to use a predicate, but (since you are using global variables) it seems like you're not looking for perfect solutions anyway.

adentinger
  • 1,367
  • 1
  • 14
  • 30
  • My professor wants the enqueue to take those three parameters. Where should I put the front and rear pointers? Sorry, this is the way that our professor has taught us... – hockey34 Apr 24 '17 at 22:44
  • Where? Inside the `DynEmpQueue` class so that it is an attribute. Each queue should have its own `front` and `rear`. – adentinger Apr 24 '17 at 22:45
  • so I use a predicate, I would set it up to take in the salary and then return true if the salary is less than 50,000? – hockey34 Apr 24 '17 at 22:50
  • Let's finish the discussion in the [chat](http://chat.stackoverflow.com/rooms/116940/c-questions-and-answers). – adentinger Apr 24 '17 at 22:57
  • 1
    @hockey34 I'm almost certain you've misinterpreted your instructors instructions with respect to the `front` and `rear` pointers. Not having them inside the class violates one of the guiding OO principles: [encapsulation](http://stackoverflow.com/questions/16014290/simple-way-to-understand-encapsulation-and-abstraction). Not to mention it is an awesomely bad idea to have more than one queue using the same list pointers. If your teacher really meant it, they're batsmurph crazy. – user4581301 Apr 24 '17 at 22:59
  • Hi, sorry I think I deleted your message...Is there a different place to chat? Thank you for the help – hockey34 Apr 24 '17 at 23:00
  • @AnthonyD. it won't let me chat there because I don't have points or whatever on the website. So I added this function: bool DynEmpQueue::salary(int localSalary) { if (localSalary <= 50000) return true; return false; } – hockey34 Apr 24 '17 at 23:03
  • Hmm... It would be much too complicated to explain in the comments how to use predicates. Just try changing the traverse method so that it returns a queue containing all the employees that have a salary under 50k. I think your teacher should be okay with that. – adentinger Apr 24 '17 at 23:08