Hello, I've tried to implement a queue using a array which loops. (Circular lists). However something is wrong. When I add a new item to the list , I get the 'case digit' aswell...
heres the code :
#include <iostream>
#define MAXSIZE 3
using namespace std;
void insert(int array[]);
void print(int array[]);
void del(int array[]);
int main()
{
int selection;
int array[MAXSIZE] = {0};
cout << "Please select:\n 1 - Add\n 2 - Print\n 3 - Delete\n 4 - Quit" << endl;
cin >> selection;
while(selection !=4)
{
switch(selection)
{
case 1:
{
cout << "Calling function insert" << endl;
insert(array);
break;
}
case 2:
{
cout << "printing array function selected" << endl;
print(array);
break;
}
case 3:
{
cout <<"Calling deleting function" << endl;
del(array);
break;
}
default:
cout << "Error!" <<endl;
} // terminate switch
cout << "Please select:\n 1 - Add\n 2 - Print\n 3 - Delete\n 4 - Quit" << endl;
cin >> selection ;
} // terminate while
return 0;
}
void insert(int array[])
{
int value;
int *frontPtr = array; // referenced both pointers to first element into the array
int *rearPtr = array;
cout << "please enter an element you wish to add into the array\n";
cin >> value;
if(((*frontPtr == 0) && (*rearPtr == MAXSIZE)) || (*frontPtr == (*rearPtr + 1)))
{
cout << "Queue is full" << endl;
}
else
if(*frontPtr == 0)
{
*frontPtr = 1;
*rearPtr = 1;
}
else if
(*rearPtr == MAXSIZE)
{
*rearPtr = 1;
}
else
{
*rearPtr = *rearPtr + 1;
}
array[*rearPtr] = value;
}
void print(int array[])
{
cout << "Now printing the array\n"<<endl;
for (int i = 0 ; i <=MAX ; i++)
cout << "element : " << i << " ,has the value : " << array[i] <<" \n" <<endl;
}
void del(int array[])
{
int item;
cout << "Please enter an item to delete" << endl;
cin >> item;
int *frontPtr = array;
int *rearPtr = array;
if(*frontPtr == 0)
{
cerr << "YAK" << endl;
}
else
{
item = array[*frontPtr];
if(*frontPtr == *rearPtr)
{
*frontPtr = 0;
*rearPtr = 0;
}
else if(*frontPtr == MAXSIZE)
{
*frontPtr = 1;
}
else
{
*frontPtr = *frontPtr +1;
}
}
}
output:
Please select:
1 add
2 Print
3 - Delete
4 - Quit(user enters 2, which prints!)
element 0, has the value : 0
element 1, has the value : 0
element 2, has the value : 0
element 3, has the value : 2
---------------------
Why is it storing 2, when in fact it should be 0 hence I've set the array to 0 by array = {0}; and nothing as been entered??