[Solved] function containing for are not expanded inline [closed]


This should compile, but it will not work as intended. I’m not going to try to fix it because the logic is too broken and handled better by reading up on queues in a text before continuing.

I cannot be certain this compiles in Turbo C++. I haven’t used Turbo C++ since the 90’s, and even then I just used it to compile C code.

Explanations are embedded in the comments to keep everything in one chunk.

#include<iostream> // don't need the .h
#include<conio.h>
#define MAXSIZE 10
#include<stdlib.h>

// need to specify namespaces. Don't pull in the whole namespace unless you
// know exactly what you are doing. JUst use the parts you need or explicitly
// state with every use eg. std::cout << "queue is full";
using std::cout;
using std::cin;
using std::endl;

class queue
{
    int s[MAXSIZE], front, rear; // no need to define i here. Only used in
                                 // the traverse method and has no need for
                                 // persistence
public:
    queue()
    {
        front = -1;
        rear = -1;
    }

    void insert(int val)
    { // this function almost certainly does not work logically
      // a queue adds to one end and takes from the other. This allows the
      // caller to put an element anywhere in the queue, destroying
      // whatever value was in that slot.

        if (rear == MAXSIZE - 1)
        { // always use all of the braces while learning. You can take the
          // training wheels off when you know how to do it safely.
            cout << "queue is full";
        }
        else if (front == -1)
        {
            front = 0;
        }
        rear = rear + 1;

        cout << "\n" << " Enter the info";
        // using \n in place of std::endl may have unexpected consequences
        // with respect to flushing. std:: endl means end the line and write the
        // output. In this case,display on the console. \n means end the line and
        // write when you feel like it. Result is the user prompt may not be
        // written before it stops for user input on the next line.

        cin >> s[val];
    }

    void del()
    { // this function's logic is also broken.
        if (front == -1)
        {
            cout << "queue is empty";
        }
        else
        {
            cout << "item deleted" << s[front];
        }
        if (front == rear)
        {
            front = rear = -1;
        }
        else
        {
            front = front + 1;
        }
    }

    void traverse()
    {
        if (front == -1)
        {
            cout << "queue is empty"; // missed the <<. This broke the for loop
                                      // because the braces weren't used and
                                      // the compiler choked rather than giving
                                      // a reasonable error message.
        }
        else
        {
            for (int i = front; i <= rear; i++) // defined the i removed earlier
            {
                cout << "\t" << s[i];
            }
// braces here were completely messed up.
        }
    }
};

void main()
{

    queue l1; // was linklist rather than queue

    l1.insert(10);
    l1.insert(20);
    l1.insert(30);
    l1.traverse();
    l1.del();
    l1.insert(40);
    getch();
}

solved function containing for are not expanded inline [closed]