Home > Mobile >  Printing a string from a private array using overloaded subscript operator
Printing a string from a private array using overloaded subscript operator

Time:12-11

I'm having trouble trying to figure out how to print a string from a private array using the square bracket overloaded operator. I've tried looking for a solution but I can't figure it out. The closest I got was using cout << &del[i]; and that at least printed some memory address or something. How do I use the overloaded subscript operator to return the strings that I need? passing in i should return the orderItems array at i, but I just can't figure out how to call it.

edit I'm stuck on the dequeue method of the OrderQueue class, and the overloaded operator is in the OrderNode struct.

#include <iostream>

using namespace std;

struct OrderNode {
private:
    string orderItems[5] = { "", "", "", "", "" };

public:
    double subTotal;
    int itemCount;

    OrderNode* next;

    OrderNode(OrderNode* n = NULL) { next = n; subTotal = 0.0; itemCount = 0;}

    void addItem(string item) {
        if (itemCount < 5) {
            orderItems[itemCount] = item;
            itemCount  ;
        }
        else {
            throw string("Order full. Please begin another.");
        }
    }
    string operator[](int index) {
        if (index < 0 || index >= itemCount)
            throw string("Index out of range");
        else
            return orderItems[index];
    }
};

void menu() {
    cout << "Select an item:\n";
    cout << "1) Coffee     -    $2.99\n";
    cout << "2) Tea        -    $2.59\n";
    cout << "3) Soft Drink -    $1.49\n";
    cout << "4) Water      -    $0.99\n";
    cout << "5) No Other Items\n";
}

class OrderQueue {
private:
    OrderNode* front;
    OrderNode* rear;
public:
    OrderQueue() {
        front = NULL;
        rear = NULL;
    }
    void enqueue() {
        int choice{ 0 };
        OrderNode* tmp = new OrderNode;
        if (rear == NULL) {
            front = tmp;
            rear = tmp;
        }
        while (tmp->itemCount < 5 && choice != 5) {
            menu();
            cin >> choice;
            if (choice == 1) {
                tmp->addItem("Coffee");
                tmp->subTotal  = 2.99;
            }
            else if (choice == 2) {
                tmp->addItem("Tea");
                tmp->subTotal  = 2.59;
            }
            else if (choice == 3) {
                tmp->addItem("Soft Drink");
                tmp->subTotal  = 1.49;
            }
            else if (choice == 4) {
                tmp->addItem("Water");
                tmp->subTotal  = 0.99;
            }
            else
                break;
        }
        rear->next = tmp;
        rear = tmp;
        cout << "\n\nOrder placed" << endl;
    }
    void dequeue() {
        if (front == NULL) {
            cout << "No pending order\n";
            return;
        }
        OrderNode* del = front;
        front = front->next;
        cout << "This order consists of the following items:\n";
        for (int i = 0; i < del->itemCount; i  ) {
            cout << "In Loop index: " << i << endl;
            &del[i];
        }
        delete del;
        }
    void traverse();
};

void displayMenu() {
    cout << "COVID Cafe Drive-Thru Order Manager" << endl;
    cout << "1) Take an Order" << endl;
    cout << "2) Close out an Order" << endl;
    cout << "3) List all pending Orders" << endl;
    cout << "4) Go home and self-isolate" << endl;
}

int main() {
    OrderQueue* test = new OrderQueue;

    int c{};
    do {
        displayMenu();
        while (!(cin >> c)) {
            cout << "Error, please select a valid choice, use enter 4 to exit the program\n";
            displayMenu();
            cin.clear();
            cin.ignore();
        }
        switch (c) {
        case 1://enqueue order
            test->enqueue();
            break;
        case 2://dequeue order
            test->dequeue();
            break;
        case 3://traverse current orders in queue
            break;
        case 4://quit the program
            break;
        default:
            break;
        }
    } while (c != 4);

    return 0;
}

CodePudding user response:

&del[i];

The shown code defines the [] operator OrderNode objects .

del is not an OrderNode object. It is a pointer to an OrderNode object. As you already know, if you have a pointer to an object, the unary * operator dereferences the pointer and gives you a reference to the pointed-to object.

Therefore, given the priority and associativity of * and [] operators, the correct syntax must, therefore, be:

(*del)[i]

Of course, you'll have to do something with the returned std::string. This'll get you the std::string that's returned from the [] operator. and it will be up to you to figure out what to do with it, like:

std::cout << (*del)[i]
  • Related