Hi guys,i am new to both c++ and this forum...I got a problem with a question i m supposed to do.I am supposed to create a queue system but i am stuck at a point where i can't add numbers to the queue system.This is the question...
You have been assigned to code the world longest queue that can store up to a maximum of 10 integers.

Create a queue system interface with the following functionalities:
- option to Add number to end of queue
- option to Remove number from end of queue
- option to Sort queue in ascending order
- option to Show queue content
- quit
Sample Output:

Queue System [Free: 10]
==================================
1. Add number to end of queue
2. Remove number from end of queue
3. Sort queue in ascending order
4. Show queue content
5. Quit
Choice: 1

Enter the number to add: 10

The problem is that i can't add number to the queue.Here is my code...

# include<iostream>
using namespace std;

void show(int a[],int size)
{
	for(int b=0;b<=size;b++)
	{
		cout<<"Item "<<b<<": "<<a[1]<<endl;
	}
}
void subno(void)
{
	int sub=0,b=0,c=0;
	cout<<"Status: Last number successfully removed."<<endl;
	
}
void addno(void)
{
	int arr[10];
	int b=0;
		cout<<"Enter number to add: ";
		for(int i=0; ;i++)
		{
			cin>>arr[i];
		}
}
void main(void)
{
	int b=10,c=0,e=0;
	int a=0;
	do
	{

			cout<<"Queue system [Free:"<<b<<"]"<<endl;
			for(int f=1;f<36;f++)
			{
				cout<<"=";
			}
			cout<<endl;
			cout<<"1. Add number to the end of queue."<<endl;
			cout<<"2. Remove number from end of queue."<<endl;
			cout<<"3. sort the queue in ascending order."<<endl;
			cout<<"4. show queue content."<<endl;
			cout<<"5. Quit."<<endl;
			cout<<"choice: ";
			cin>>a;
			cout<<endl;
		
		if(a==1)
		{
			addno();
			b--;
		}
		else if(a==2)
		{
			subno();
			b++;
		}	

		}
	
while(b!=0);

}

i don't know how to add number to the array.

Recommended Answers

All 5 Replies

Hi guys,i am new to both c++ and this forum...I got a problem with a question i m supposed to do.I am supposed to create a queue system but i am stuck at a point where i can't add numbers to the queue system.This is the question...
You have been assigned to code the world longest queue that can store up to a maximum of 10 integers.

Create a queue system interface with the following functionalities:
- option to Add number to end of queue
- option to Remove number from end of queue
- option to Sort queue in ascending order
- option to Show queue content
- quit
Sample Output:

Queue System [Free: 10]
==================================
1. Add number to end of queue
2. Remove number from end of queue
3. Sort queue in ascending order
4. Show queue content
5. Quit
Choice: 1

Enter the number to add: 10

The problem is that i can't add number to the queue.Here is my code...

# include<iostream>
using namespace std;

void show(int a[],int size)
{
	for(int b=0;b<=size;b++)
	{
		cout<<"Item "<<b<<": "<<a[1]<<endl;
	}
}
void subno(void)
{
	int sub=0,b=0,c=0;
	cout<<"Status: Last number successfully removed."<<endl;
	
}
void addno(void)
{
	int arr[10];
	int b=0;
		cout<<"Enter number to add: ";
		[B]for(int i=0; ;i++)[/B]
		{
			cin>>arr[i];
		}
}
void main(void)
{
	int b=10,c=0,e=0;
	int a=0;
	do
	{

			cout<<"Queue system [Free:"<<b<<"]"<<endl;
			for(int f=1;f<36;f++)
			{
				cout<<"=";
			}
			cout<<endl;
			cout<<"1. Add number to the end of queue."<<endl;
			cout<<"2. Remove number from end of queue."<<endl;
			cout<<"3. sort the queue in ascending order."<<endl;
			cout<<"4. show queue content."<<endl;
			cout<<"5. Quit."<<endl;
			cout<<"choice: ";
			cin>>a;
			cout<<endl;
		
		if(a==1)
		{
			addno();
			b--;
		}
		else if(a==2)
		{
			subno();
			b++;
		}	

		}
	
while(b!=0);

}

i don't know how to add number to the array.

The bolded part is the problem. Try adding--

cout<<"Enter number to add: ";
		for(int i=0; i < 10 ;i++) //i < 10
		{
			cin>>arr[i];
		}

Not only that, but it may be more intuitive to tell the person what indice number they're placing their values in.

cout<<"Enter number to add: ";
		for(int i=0; i < 10 ;i++) //i < 10
		{
                        cout << "for indice: " << i << endl;
			cin>>arr[i];
		}

Edit: Also you'll want to declare your array outside of the method. It is currently in local scope to the method that it is defined. It will cease to exist and the information you stored in it will go poof!

erm.You see the cin will just keep on looping untill when i is 10.so the user have to keep on inputing the values.that is the problem i can't solve.I only want the user to input once then it returns to the main function and execute everything again...But still thanks alot for helping.

Hi Yuichi,
My suggestion is almost similar to Alex's, only looking at your prigram I think you should not only declare your array which which is holding the queue as global, but also a variable which holds your actual queue size (i.e no of elements currently in the queue and not the size of the array).
Something like

using namespace std;

int queue[10];
int lastElementIdx=-1;

So when you want to add just one element to the queue you have to :

queue[++lastElementIdx]=...

instead of the for loop.
and to delete the last element from the queue just decrement the variable "lastElementIdx" and of course check to ensure you do not delete an element from empty queue etc.

erm.You see the cin will just keep on looping untill when i is 10.so the user have to keep on inputing the values.that is the problem i can't solve.I only want the user to input once then it returns to the main function and execute everything again...But still thanks alot for helping.

You have initialized b to 10 and your do while loop will keep going until 0 is reached. I would suggest you prompt the user to continue then you code the loop like this

do
{

    //code here

}while (response=='y')

Ahh thanks stephen84s that solved my adding poblem although i dun get the decreasing part.Thanks too joshmo.

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.