Can anyone see any way of improving this?

//____________________________________________________________________________________________
// START

#include <iostream>

using namespace std;
int maxQ, total1;


//____________________________________________________________________________________________
// CLASS(ES)

class Queue
{
	int array[];
public:
	//variabes
	int count;
	
	//constructors
	Queue();
	Queue(int q);
	
	//functions
	void add(int i);
	void take();
};

//constructors initialization


Queue::Queue(int q)
{
	array[maxQ];
	count = 1;
	total1 =0;
}

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

//functions initialization

void Queue::add(int i)
{
	if (count == maxQ+1) 
	{
		count=1;
		cout << "\nyou have reached the end of the queue\nnow the values are being placed at the front of the queue again,\nerasing the values already there!!\n\n";
		array[count] = i;
	}	
	else if (count < maxQ+1)
	{
		array[count] = i;
		count++;
		total1++;
	}
	
	if (total1 == maxQ+1) 
	{
		total1--;
	}
}


void Queue::take()
{
	if(total1 == 0)
	{
		cout << "\nThere are no values left in the queue!\n";
	}
	else
	{
		if (count == 1) 
		{
			count = maxQ+1;
		}
		cout << "\nYou have taken " << array[count-1] << " out of the queue";
		total1--;
		array[count-1] = 0;
		count--;
	}
}

//____________________________________________________________________________________________________________________________
// MAIN

int main () 
{
	int choice, values, answer, integer;
	Queue value;
	
	
    cout << "\nwould you like to initialize a queue of:\n-20 values(enter 1)\n-100 values (enter 2)\n-an amount of your choice (enter 3)\n";
	cin >> choice;
	
	if (choice == 1)
	{
		Queue value(20);
		maxQ = 20;
	}
	else if (choice == 2) 
	{
		Queue value();
		maxQ = 100;
	}
	else if (choice == 3)
	{
		cout << "\nHow many values would you like to have in your queue?: ";
		cin >> values;
		Queue value(values);
		maxQ = values;
	}
	
	while (choice !=1 && choice !=2 && choice !=3)
	{
		cout << "\n" << choice << " isnt 1,2 or 3! Please re-enter 1,2 or 3: ";
		cin >> choice;
	}
	do{
		cout << "\nWould you like to:\n-Add integers to the queue (enter 1)\n-take an integer from the queue (enter 2)\n-exit (enter 3)\n";
		cin >> answer;
		
		while (answer !=1 && answer !=2 && answer !=3) 
		{
			cout << "\n" << answer << " isnt 1,2 or 3! Please re-enter 1,2 or 3: ";
			cin >> answer;
		}
		
		if (answer == 1) 
		{
			cout << "\nWhat integer would you like to add: ";
			cin >> integer;
			value.add(integer);
			cout << "\n" << integer << " has been added\n";
			cout << "\nThere are now " << total1 << " values in the queue.";
		}		
		
		else if (answer == 2)
		{
			value.take();
			cout << "\n\nThere are now " << total1 << " values in the queue.";
		}
		
	}while (answer !=3);
	
	return 0;
}

//____________________________________________________________________________________________
// END

Recommended Answers

All 3 Replies

The easiest way to improve it is to throw it out and use the queue implementation from the standard library. Why reinvent the wheel?

If your purpose in writing this code is as an exercise, rather than to do something that you intend to be useful, then I don't understand how you got this code to compile. Line 15 is not valid C++, and lines 34 and 41 make no sense. I can guess what you intended them to do, but they don't do that.

Despite your name of Queue, the code you have written appears to implement a stack, not a queue. I say "appears" because it's not hard to find errors in the code--though it would be easier if it were possible to compile the code in the first place.

I would like to suggest that before you ask other people to spend time reading and discussing code you have written, that you first take the trouble to (1) explain what you expect the code to do and why you are writing it, and (2) verify that the code actually compiles and does what you claim it does.

ive completely changed it now=>

#include <iostream>

using namespace std;

const int maxQ = 100;
int total=0;

class Queue
{
	int array[maxQ];
	int size;
	int putloc, getloc;
	
public:
	
	Queue(int i)
	{
		//total = 0;
		if (i > maxQ) 
		{
			i = maxQ;
		}
		else if (i < 1)
		{
			i=1;
		}
		size = i;
		putloc = getloc = 0;
	}
	
	Queue()
	{
		size = maxQ;
		putloc = getloc = 0;
	}
	
	void put(int j)
	{
		if (putloc == size) 
		{
			cout << "queue is full, your value will be put at the start of the queue\n\n";
			putloc = 0;
		}
		
		total++;
		putloc++;
		array[putloc] = j;
		
		if (total == size+1) 
		{
			total--;
		}
	}
	
	int get()
	{
		if (total == 0) 
		{
			getloc = putloc = 0;
			cout << "Queue is empty\n\n";
			return 0;
		}
		if (getloc == size) 
		{
			getloc = 0;
		}
		
		getloc++;
		total--;
		return array[getloc];
		array[getloc] = 0;

	}
	
	
};
	
int main () 
{
	int choice2, integer;
	Queue value(20);

	do{
		cout << "\nWould you like to:\n-Add integers to the queue (enter 1)\n-take an integer from the queue (enter 2)\n-exit (enter 3)\n";
		cin >> choice2;
		
		while (choice2 !=1 && choice2 !=2 && choice2 !=3) 
		{
			cout << "\n" << choice2 << " isnt 1,2 or 3! Please re-enter 1,2 or 3: ";
			cin >> choice2;
		}
		
		if (choice2 == 1) 
		{
			cout << "\nWhat integer would you like to add: ";
			cin >> integer;
			value.put(integer);
			cout << "\nyou have added " << integer << " to the queue\n\n";
			cout << "There are now " << total << " values in the queue\n\n";
		} 
		
		else if (choice2 == 2)
		{
			int k;
			k = value.get();
			cout << "You have taken " << k << " from the queue\n\n";
			cout << "There are now " << total << " values in the queue\n\n";

		}
		
	}while (choice2 !=3);
	
	
    return 0;
}

I'm sticking with my last suggestion:

Before you ask other people to spend time reading and discussing code you have written, that you first take the trouble to (1) explain what you expect the code to do and why you are writing it, and (2) verify that the code actually compiles and does what you claim it does.

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.