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;
}