Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Queue class and then a menu driven main function. There are only 3 opening brackets in the main function, and there are three to match them. So I am not sure why I am getting this error. Below is the code:

C++
#include <stdio.h>
#include <iostream>
#include <vector>

using namespace std;

class Queue {
private:
    int R;
    int F;
    vector<int> A{vector<int>(5,0)};
public:
    //Constructor
    Queue() {
    R=-1;
    F=-1;
    
        for (int i = 0; i < A.size();i++) {
            A[i] = 0;
        }
    }

    bool isEmpty(){
        if (R== -1 && F==-1) {
            return true;
        }
        else {
            return false;
        }
    }
    
    bool isFull(){
        if (R==A.size()-1) {
            return true;
        }
        else {
            return false;
        }
    }
    
    void enqueue(int val) {
        if (isFull() {
            cout << "Queue Full!";
            //return;
        }
        else if (isEmpty()){
            F=R=0;
            A[R] = val;
        }
        else {
            R++;
            A[R] = val;
        }
    }
    
    int dequeue(int val) {
        int x{0};
        if (isEmpty) {
            cout << "Queue is empty!";
            //return;
        }
        else if (F=R){
            x = A[F];
            A[F] = 0;
            F=R=-1;
        }
        else {
            x = A[F];
            A[F] = 0;
            F++;
        }
        return x;
    }
    
    int count() {
        if (isEmpty()) {
            return 0;
        }
        else {
            return R-F+1;
        }
    }
    
    void display() {
        for (int i = F; i <= R;i++) {
            cout << A[i] << " ";
        }
    }
};

int main(int argc, char **argv)
{
	Queue q1;
  int value, option;

  do {
    cout << "\n\nWhat operation do you want to perform? Select Option number. Enter 0 to exit." << endl;
    cout << "1. Enqueue()" << endl;
    cout << "2. Dequeue()" << endl;
    cout << "3. isEmpty()" << endl;
    cout << "4. isFull()" << endl;
    cout << "5. count()" << endl;
    cout << "6. display()" << endl;
    cout << "7. Clear Screen" << endl << endl;

    cin >> option;

    switch (option) {
    case 0:
      break;
    case 1:
      cout << "Enqueue Operation \nEnter an item to Enqueue in the Queue" << endl;
      cin >> value;
      q1.enqueue(value);
      break;
    case 2:
      cout << "Dequeue Operation \nDequeued Value : " << q1.dequeue() << endl;
      break;
    case 3:
      if (q1.isEmpty())
        cout << "Queue is Empty" << endl;
      else
        cout << "Queue is not Empty" << endl;
      break;
    case 4:
      if (q1.isFull())
        cout << "Queue is Full" << endl;
      else
        cout << "Queue is not Full" << endl;
      break;
    case 5:
      cout << "Count Operation \nCount of items in Queue : " << q1.count() << endl;
      break;
    case 6:
      cout << "Display Function Called - " << endl;
      q1.display();
      break;
    case 7:
      system("cls");
      break;
    default:
      cout << "Enter Proper Option number " << endl;
    }

  } while (option != 0);

return 0;
}


What I have tried:

I checked all the brackets to see if I am missing any closing brackets.
Posted
Updated 21-Jul-22 17:47pm
v2
Comments
Patrice T 21-Jul-22 23:26pm    
this code do not contain "input"
Use Improve question to update your question.
Graeme_Grant 21-Jul-22 23:47pm    
recoomend posting the full error message when you improve the question.
FreedMalloc 21-Jul-22 23:49pm    
Perhaps my C++ is a bit rusty but, I am not familiar with this construct:
vector<int> A{vector<int>(5,0)};
What does this statement (near code line 12) do?
Rick York 22-Jul-22 0:27am    
That initializes a vector of five integers with a value of 0. There are other cleaner ways to do that.
FreedMalloc 22-Jul-22 1:13am    
Thank you. I assumed that was the intent. Enclosing the initializer in braces was what threw me. I've not seen that syntax before.

You're missing a closing parenthesis on an if statement.

The problem with the brace formatting you're using is that opening curly braces make it a bit more difficult to see where opening and closing curly braces and parenthesis match up.
C++
void enqueue(int val) {
        if (isFull() {                  <-------------------
            cout << "Queue Full!";
            //return;
        }
        else if (isEmpty()){


IMHO, this is better:
C++
void enqueue(int val)
{
    if (isFull()            <---- missing closing parenthesis
    {
        cout << "Queue Full!";
        //return;
    }
    else if (isEmpty())
    {
 
Share this answer
 
v2
Comments
Romyal Swarnkar 22-Jul-22 1:22am    
Thank you Dave. That missing parenthesis was the issue.
CPallini 22-Jul-22 2:08am    
5.
There are multiple issues in your code:
	1. #include <vector> to use vector class
	2. In 'void enqueue(int val)' method use close pranthesis ')' after isFull() method
	3. In 'int dequeue(int val)' method use isEmpty as function isEmpty() instead of variable

Hope this will solve your problem.
 
Share this answer
 
Comments
Romyal Swarnkar 22-Jul-22 1:26am    
@Imran the vector class is already included. It was the missing parenthesis that was causing the issue as Dave pointed out.
CPallini 22-Jul-22 2:08am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900