Hi,
I have a project to do for a C++ class that asks us to use a class termed "queue" to store some values (20/100/user defined) and allow the user to add/remove values. I'm sure it seems really basic, but I just can't get my code to display the class.

I was hoping to allow the user to display their class on request from the main, but for all my attempts I just can't get it to work:sad:

Anyway, I would be reeally grateful if anyone has some tips or anything!
Thanks!

#include <iostream>
#include <cstring>

using namespace std;
int Lim;
int Value;

class Queue

{
int array[];
public:
	int  count;
	     Queue();
	     Queue(int a);
	void add(int b);
	void take();
};

Queue::Queue(int a)

{
	array[Lim];
	count = 1;
	Value =0;
}

Queue::Queue()
{
	array[100];
	count = 1;
	Value = 0;
}


void Queue::add(int b)
{
	if (count == Lim+1)
	{
		count=1;
		cout << "\n The queue has been filled. \n";
		cout << " Any new values entered will replace values entered earlier, \n working from first to last. \n\n";
		array[count] = b;
	}
	else if (count < Lim+1)
	{
		array[count] = b;
		count++;
		Value++;
	}

	if (Value == Lim+1)
	{
		Value--;
	}
}


void Queue::take()
{
	if(Value == 0)
	{
		cout << "\n No values stored. \n";
	}
	else
	{
		if (count == 1)
		{
			count = Lim+1;
		}
		cout << array[count-1] << " has been removed from the queue. \n";
		Value--;
		array[count-1] = 0;
		count--;
	}
}


int main ()
{
	int var;
	int x;
	int ans;
	int num;


	Queue value;

    cout << "How many values would you like the queue to hold? \n";
    cout << "   20 (Enter 1) \n";
    cout << "   100 (Enter 2) \n";
    cout << "   A Different Quantity (Enter 3) \n";
	cin >> var;

	if (var == 1)
	{
		Queue value(20);
		Lim = 20;
	}
	else if (var == 2)
	{
		Queue value();
		Lim = 100;
	}
	else if (var == 3)
	{
		cout << "\n How many values do you want to store in this queue? ";
		cin >> x;
		Queue value(x);
		Lim = x;
	}

	while (var !=1 && var !=2 && var !=3)
	{
		cout << "\n" << var << " Invalid input. ";
		cin >> var;
	}
	do{
		cout << "\n Do you want to:\n\n";
		cout << "   Store a value(Enter 1) \n";
		cout << "   Remove the last value (Enter 2) \n";
		cout << "   Exit (Enter 3) \n";
		cout << "   View the queue (Enter 4) \n";
		cin >> ans;

		while (ans !=1 && ans !=2 && ans !=3 && ans !=4)
		{
			cout << "\n" << ans << " Invalid input. ";
			cin >> ans;
		}

		if (ans == 1)
		{
			cout << "\n Enter the value to be stored. ";
			cin >> num;
			value.add(num);
			cout << "\n" << num << " stored.";
			cout << "\n" << Value << " values stored.\n";
		}

		else if (ans == 2)
		{
			value.take();
			cout << "\n\n" << Value << " values now queued.";
		}

        else if (ans == 4)
        {
            cout <<" Here is your queue"<< class <<" It stops here.\n";
        }

	}while (ans !=3);
{
    cout << "Thank you. Press any key to close.";
}
	return 0;
}

I'm not trying to be harsh, but this needs a lot of work.

Line 12 is not legal C++ syntax. If you want an array of variable size, you need to use a pointer, dynamic memory management functions (operator new[] and operator delete[]), a proper destructor, and variables to track capacity and usage.

const int defaultArraySize = 10;

class Sample {
 private:
   int *myArray;
   int ArrayCapacity;
   int ArrayUsed;
 public:
   Sample():ArrayCapacity(defaultArraySize), ArrayUsed(0)
     { myArray = new int[defaultArraySize]; }   //default constructor

   Sample (int newSize):ArrayCapacity(newSize), ArrayUsed(0)
     { myArray = new int[newSize]; }            //specified constructor

   ~Sample() { delete[] myArray; }              //destructor
};  //end Sample class definition

Also, you really shouldn't use global variables if you can avoid it. Global constants are okay, even advisable, but not variables. It would be a good idea to get rid of Lines 6 and 7 and find a better way to do that.

commented: Clear, direct, constructive criticism +0

Oh no, by all means, I know I'm not great at this yet, by any means, thanks for the pointer, I'll try working with what you included and see if I can get any further, thanks!!:icon_redface:

Hmm... I understand my program is heavily flawed, but it is working correctly. I feel like implementing your last suggestion would be a pretty dramatic restructuring, and I'm not sure if I have time before the deadline to build it from scratch. I will give it a shot, but if anyone knows of a way of displaying the class on request, it would be a huge help! Thanks!

Do you know how to traverse an array?

const int arraySize = 10;
int anArray[arraySize] = {0};

for (int i = 0; i < arraySize; ++i) {
  cout << "Array element " << i << " is " << anArray[i] << endl;
}

Add a Display method to the class. Then, inside the method, apply this concept formatting the output as you see fit.

Feel free to ignore this if you're tired of trying to help, but I'm not sure how to impliment a display method that can be controlled from the main? Sorry, I'm not even sure if I'm explaining my problem correctly. I'm sure this s getting kind of tedious, but it is a huge help, sorry!

>>I'm sure this s getting kind of tedious, but it is a huge help, sorry!
No worries, your listening and trying, that's good enough for me.

You would declare, implement, and call it just like you did your Add() or Take() method. The only difference is that it displays the contents instead of manipulating them. I would also suggest declaring it as a "const" method, so that you can't accidentally edit its contents inside the function. Extending above example:

const int defaultArraySize = 10;

class Sample {
 private:
   int *myArray;
   int ArrayCapacity;
   int ArrayUsed;
 public:
   Sample():ArrayCapacity(defaultArraySize), ArrayUsed(0)
     { myArray = new int[defaultArraySize]; }   //default constructor

   Sample (int newSize):ArrayCapacity(newSize), ArrayUsed(0)
     { myArray = new int[newSize]; }            //specified constructor

   ~Sample() { delete[] myArray; }              //destructor

   void Display() const;                        //declare a const Display function
};  //end Sample class definition

void Sample::Display() const{  //implement the Sample::Display method
  for (int i = 0; i < this->ArrayUsed; ++i) {
    cout << "Array element " << i << " is " << myArray[i] << endl;
  }  //end for
} //end Sample::Display



int main() {
  Sample aSample;     //create a default-size Sample object

  //populate and manipulate the contents of aSample

  aSample.Display();  //tell the Sample to Display itself

  //more actions

  return 0;
} //end main()

Sorry, I think I have only managed to massacre your code aswell as my own! I'm sure there's some massive problems with it, but if you can see any quick fixes I'll change it around, otherwise I think I'll just go with the original and see how many holes the correctors poke in it! Thanks for your help anyway! You really have been great, I think I'm just a bit hopeless!

#include <iostream>
#include <cstring>

using namespace std;
const int defaultArraySize = 10;

class Queue {
 private:
   int *myArray;
   int Lim;
   int count;
 public:
   Queue():Lim(defaultArraySize), count(0)
     { myArray = new int[defaultArraySize]; }

   Queue (int newSize):Lim(newSize), count(0)
     { myArray = new int[newSize]; }

   ~Queue() { delete[] myArray; }
   void Display() const;
};



void Queue::Display() const{
  for (int i = 0; i < this->count; ++i) {
    cout << "Array element " << i << " is " << myArray[i] << endl;
  }
}




Queue::Queue(int a)
{
	array[Lim];
	count = 1;
	Value =0;
}

Queue::Queue()
{
	array[100];
	count = 1;
	Value = 0;
}


void Queue::add(int b)
{
	if (count == Lim+1)
	{
		count=1;
		cout << "\n The queue has been filled. \n";
		cout << " Any new values entered will replace values entered earlier, \n working from first to last. \n\n";
		array[count] = b;
	}
	else if (count < Lim+1)
	{
		array[count] = b;
		count++;
		Value++;
	}

	if (Value == Lim+1)
	{
		Value--;
	}
}


void Queue::take()
{
	if(Value == 0)
	{
		cout << "\n No values stored. \n";
	}
	else
	{
		if (count == 1)
		{
			count = Lim+1;
		}
		cout << array[count-1] << " has been removed from the queue. \n";
		Value--;
		array[count-1] = 0;
		count--;
	}
}


int main ()
{
	int var;
	int x;
	int ans;
	int num;


	Queue value;

    cout << "How many values would you like the queue to hold? \n";
    cout << "   20 (Enter 1) \n";
    cout << "   100 (Enter 2) \n";
    cout << "   A Different Quantity (Enter 3) \n";
	cin >> var;

	if (var == 1)
	{
		Queue value(20);
		Lim = 20;
	}
	else if (var == 2)
	{
		Queue value();
		Lim = 100;
	}
	else if (var == 3)
	{
		cout << "\n How many values do you want to store in this queue? ";
		cin >> x;
		Queue value(x);
		Lim = x;
	}

	while (var !=1 && var !=2 && var !=3)
	{
		cout << "\n" << var << " Invalid input. ";
		cin >> var;
	}
	do{
		cout << "\n Do you want to:\n\n";
		cout << "   Store a value(Enter 1) \n";
		cout << "   Remove the last value (Enter 2) \n";
		cout << "   Exit (Enter 3) \n";
		cout << "   View the queue (Enter 4) \n";
		cin >> ans;

		while (ans !=1 && ans !=2 && ans !=3 && ans !=4)
		{
			cout << "\n" << ans << " Invalid input. ";
			cin >> ans;
		}

		if (ans == 1)
		{
			cout << "\n Enter the value to be stored. ";
			cin >> num;
			value.add(num);
			cout << "\n" << num << " stored.";
			cout << "\n" << Value << " values stored.\n";
		}

		else if (ans == 2)
		{
			value.take();
			cout << "\n\n" << Value << " values now queued.";
		}

        else if (ans == 4)
        {
        Queue.Display();
        }


	}while (ans !=3);
{
    cout << "Thank you. Press any key to close.";
}
	return 0;
}

I gave it a quick once-over, it looks like you've got the right idea, you just need to clean it up a little and make it your own.

Make sure to modify the output(s) to fit the requirements of your assignment. I have no idea what your assignment requires, the code that you copy/pasted was intended to be generic for demonstration purposes only.

Thanks for taking a look over it, I've made some changes and I think I'm getting there, thanks a million for all your help!

cout << "Thank you. Press any key to close.";

I just noticed this. There is no legal way in Standard C++ to do this. The best way to do it is place a cin.get(); directly before the return 0; and require the user to press the ENTER key:

int main() {

  //misc code...

  cout << "Please press ENTER to close...";
  cin.get();
  return 0;
} //end main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.