C++ Queue Code: Keeps spamming output until it crashes

#include <iostream>
#define MAX_SIZE 100

using namespace std;

int main() {
int item, choice;
int rear = 0;
int exit = 1;

cout << "\nSimple Queue Example - Array";
do {
cout << "\n\n Queue Main Menu";

cout << "\n1.Insert \n2.Remove \n3.Display \nOthers to exit";
cout << "\nEnter Your Choice : ";
cin>>choice;
switch (choice) {
case 1:
if (rear == MAX_SIZE)
cout << "\n## Queue Reached Max!!";
else {
cout << "\nEnter The Value to be Insert : ";
cin>>item;
cout << "\n## Position : " << rear + 1 << " , Insert Value : " << item;
}
break;
}
} while (exit);
return 0;
}
Last edited on
Please describe the "does not work".

* Does not compile? What exact errors?
* Does crash? At what point?
* Unexpected output?

Ability to properly explain issues is vital.
It keeps looping and looping the same output, for example:

Simple Queue Example - Array

 Queue Main Menu
1.Insert 
2.Remove 
3.Display 
Others to exit
Enter Your Choice : 
 
 Queue Main Menu
1.Insert 
2.Remove 
3.Display 
Others to exit
Enter Your Choice : 

 Queue Main Menu
1.Insert 
2.Remove 
3.Display 
Others to exit
Enter Your Choice : 

 Queue Main Menu
1.Insert 
2.Remove 
3.Display 
Others to exit
Enter Your Choice :

 Queue Main Menu
1.Insert 
2.Remove 
3.Display 
Others to exit
Enter Your Choice : 

Until it crashes
How do I stop the loop like this, I just want one Main Menu:

Simple Queue Example - Array

 Queue Main Menu
1.Insert 
2.Remove 
3.Display 
Others to exit
Enter Your Choice : 


run the whole code on ideone.com, put it to c++, and you'll see what I mean
Last edited on
You never do anything to end the look. You initialise exit to 1, and you never change its value. And the break statement at line 21 doesn't break out the loop, it just ends the case in your switch statement.
I see, thanks. Initializing the exit to 0 stopped the loop. Solved.
But if you initialise it to 0, you will never enter the loop at all. Surely that's not what you want?

You want to initialise it to 1, and then set it to 0 when the conditions are right to exit the loop.
But if you initialise it to 0, you will never enter the loop at all.

Its a do..while loop; it will run the body once no matter what the condition is.
Oops... yes, you're right. Thanks!

Although my point still stands. If you want the loop to repeat until a certain condition is met, you should initialise the variable such that the loop keeps looping. Then, when that condition is met, you change the variable such that the loop exits.

So, initialising exit to 0 is not the answer.
Topic archived. No new replies allowed.