How to make a menu in C++ ?!

the problem with this code when the user choice a number of the menu it repeat it self again and again it
how could i fix it


int main()
{
int choice;

do
{


cout << endl
<< " 1 - Start the game.\n"
<< " 2 - Story.\n"
<< " 4 - Help.\n"
<< " 5 - Exit.\n"
<< " Enter your choice and press return: ";
cin >> choice;

switch (choice)
{
case 1:
//code to start the game
break;
case 2:
//code to make score for this game to count how many times u win the game
break;
case 3:
//code to make option for the game
break;
case 4:
//code to help the user like give him
//extra information about the mode and the controller
break;
case 5:
cout << "End of Program.\n";
break;
default:
cout << "Not a Valid Choice. \n"
<< "Choose again.\n";
break;
}

}while (choice !=5);
return 0;
}
First use code tag. Its to your right under format: it looks like <>.....

but anyway I really don't get your question... what's repeating... can you post the whole code?
if you input 'p',it will repeat again again .the reason of this is: the state of cin,when it is not good ,cin can't input .
if add two line after 'cin>>choice" ,it will solve it .
cin.clear();
cin.get();
You want to use a while loop that has a bool condition such as gameOn and set that to true, and as long as the condition to exit the program is != false the loop continues.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool gameOn = true;
while (gameOn !=false){

/*

game

*/

// here you would have:

if (case 5 == true){
gameOn = false;
// not sure if you would need break;
}

}

To make the menu "fancy" just requires some cout's and some *******'s
1
2
3
4
5
6
7
8
9
10
cout << "******************\n";
/*


game stuff


*/

cout << "******************\n";
Last edited on
Also get rid of your do, while. If I am reading that right it will create an infinite loop of menus and go *boom*. At anyrate, I rewrote your code. Entering a letter results in bad things.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;

int main()
{
int choice;
bool gameOn = true;
while (gameOn != false){
cout << "*******************************\n";
cout << " 1 - Start the game.\n";
cout << " 2 - Story.\n";
cout << " 3 - Help.\n";
cout << " 4 - Exit.\n";
cout << " Enter your choice and press return: ";

cin >> choice;

switch (choice)
{
case 1:
cout << "game start!\n";
// rest of code here
break;
case 2:
cout << "Story so far....\n";
// rest of code here
break;
case 3:
cout << "Ahahah, you really think I will help you?\n";
// rest of code here
break;
case 4:
cout << "End of Program.\n";
gameOn = false;
break;
default:
cout << "Not a Valid Choice. \n";
cout << "Choose again.\n";
cin >> choice;
break;
}

}
return 0;
}

use switch case
Why would you switch case? case is specific to the switch statement itself.

case 1 refers to the first possible option given (in this case 1). and goes in order after that in the form of case 2, case 3, etc.

You switch the variable you are using, in this case choice.
here the full code
i want to put my game inside the menu ?

#include "PrettyConsole.cpp"// to use setTextColor() and setCursorXY()
#include <iostream>
#include <conio.h> // to use _kbhit() and _getch()
#include <windows.h> // WinApi header

using namespace std;



void display_char(int rectX,int rectY,int rectWidth,int rectHeight){
setCursorXY(rectX+rectWidth/2,rectY+rectHeight/2);
cout << static_cast<char>(2);
}

void display_space(int rectX,int rectY,int rectWidth,int rectHeight){
setCursorXY(rectX+rectWidth/2,rectY+rectHeight/2);
cout << " ";
}

void clear(){
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl ;
cout <<" " << endl ;
}

int main (){

{
Beep(523,500); // 523 hertz (C5) for 500 milliseconds
Beep(587,500);
Beep(659,500);
Beep(698,500);
Beep(784,500);

cin.get(); // wait

}
int rectHeight = 8;
int rectWidth = 10;
int rectX = 10;// Initial rectangle's x position
int rectY = 1;// Initial rectangle's y position
int size = 21 , count_y =0 , size_y = 7,count_x =0 ;
bool bomb = 0 , flute = 0 ;


for( int x=0; x<rectWidth; ++x )
{
if (x==0 || x==9){
setCursorXY(rectX+x,rectY);
cout << '#';
}
else if ( (x==4||x==5)&&((count_x==2 && count_y == 0)||(count_x ==1&&count_y == 2))){
setCursorXY(rectX+x,rectY);
cout << " " ;
}
else{
setCursorXY(rectX+x,rectY);
cout << "=";
}

}
for( int x=0; x<rectWidth; ++x )
{
if ( x==0 || x==9){
setCursorXY(rectX+x,rectY+rectHeight-1);
cout << '#';
}
else{
setCursorXY(rectX+x,rectY+rectHeight-1);
cout << '=';
}
}
for( int y=0; y<rectHeight; ++y )
{
if ( y == 4 && ((count_x == 1 && count_y == 2)||(count_x == 1 && count_y == 0))){
setCursorXY(rectX+rectWidth-1,rectY+y);
}
else{
setCursorXY(rectX+rectWidth-1,rectY+y);
cout << '#';
}
}
for( int y=0; y<rectHeight; ++y )
{
setCursorXY(rectX,rectY+y);
cout << '#';
}
rectY= 8;
rectX=1 ;
int rect_y=0;
int head_move_x , head_move_y ;
for ( int corre_y = rectY ; corre_y<= rectY+size_y;corre_y+=7){
int count_x = 0 ;
int rect_x=0;
for(int corre_x = rectX;corre_x<=rectX+size;corre_x+=9){
for( int x=0; x<rectWidth; ++x )
{
if (x==0 || x==9){
setCursorXY(corre_x+x,corre_y);
cout << '#';
}
else if ( (x==4||x==5)&&((count_x==1 && count_y == 1)||(count_x ==2&&count_y == 1)||(count_x ==1&&count_y == 0)||(count_x ==0&&count_y == 1))){
setCursorXY(corre_x+x,corre_y);
cout << " " ;
}
else{
setCursorXY(corre_x+x,corre_y);
cout << '=';
}
}
for( int x=0; x<rectWidth; ++x )
{
if ( x==0 || x==9){
setCursorXY(corre_x+x,corre_y+rectHeight-1);
cout << '#';
}
else{
setCursorXY(corre_x+x,corre_y+rectHeight-1);
cout << '=';
}
}
for( int y=0; y<rectHeight; ++y )
{
if ( y == 4 && ((count_x == 0 && count_y == 1)||(count_x == 1 && count_y == 1))){
setCursorXY(corre_x+rectWidth-1,corre_y+y);
}
else{
setCursorXY(corre_x+rectWidth-1,corre_y+y);
cout << '#';
}
}
for( int y=0; y<rectHeight; ++y )
{
setCursorXY(rectX,corre_y+y);
cout << '#';
}


if (rect_x==1 && rect_y == 1)
{

setCursorXY(corre_x+rectWidth/2,corre_y+rectHeight/2);
cout << static_cast<char>(2);
head_move_x = corre_x ;
head_move_y = corre_y ;
}
if (rect_x==1 && rect_y ==0)
{
setCursorXY(corre_x+rectWidth/2,corre_y+rectHeight/2);
cout << static_cast<char>(3);
}
if (rect_y == 0 && rect_x==0)
{
setCursorXY(corre_x+rectWidth/2,corre_y+rectHeight/2);
cout << static_cast<char>(3);
}
if (rect_y == 0 && rect_x==2)
{
setCursorXY(corre_x+rectWidth/2,corre_y+rectHeight/2);
cout << static_cast<char>(3);
}
count_x++;
rect_x++;
}
count_y++;
rect_y++;
}
char key = 0 ,esc=27;
int x = head_move_x , y=head_move_y ;

while( key!= esc){

if(_kbhit()){

key= _getch();// _getch() reads a single character from the console without echoing the character
if (key == 100){
if(head_move_x <x+1 && head_move_y >= y){
display_space(head_move_x,head_move_y,rectWidth,rectHeight);
head_move_x+=9;
display_char(head_move_x,head_move_y,rectWidth,rectHeight);

}

} else if(key == 97){
if(head_move_x >= x && head_move_y >= y){
display_space(head_move_x,head_move_y,rectWidth,rectHeight);
head_move_x-=9;
display_char(head_move_x,head_move_y,rectWidth,rectHeight);
}
}
else if (key==119){
if(head_move_y >= y ){
display_space(head_move_x,head_move_y,rectWidth,rectHeight);
head_move_y-=6;
display_char(head_move_x,head_move_y,rectWidth,rectHeight);
if(head_move_x == x+9 && head_move_y == y-6 && !flute){
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl ;
cout <<"Press <SPACEBAR> to get the FLUTE..." << endl ;
key= _getch();
if(key == 32){
flute = true ;
head_move_y-=1;
display_space(head_move_x,head_move_y,rectWidth,rectHeight);
head_move_y+=1;
}
}
else if(head_move_x == x-9 && head_move_y == y-6 && !bomb){
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl ;
cout <<"Press <SPACEBAR> to get the BOMB..." << endl ;
key= _getch();
if(key == 32){
bomb = true ;
head_move_y-=1;
display_space(head_move_x,head_move_y,rectWidth,rectHeight);
head_move_y+=1;
}
}
else {
clear();
}
}
}
else if (key==115){
if(head_move_y<y){
display_space(head_move_x,head_move_y,rectWidth,rectHeight);
head_move_y+=6;
display_char(head_move_x,head_move_y,rectWidth,rectHeight);
if(head_move_y <= y+6 ){
cout << endl << endl << endl << endl ;
cout <<" " << endl ;
}

}
}
}
}
cout << endl << endl ;

}




First thing, please use code tags.. thats unreadable. Second, that looks WAY different then your first program.. are they still the same? In console there is no graphics other then ASCII art and stuff. You want pictures and graphics your talking 2d/3d stuff.

Also, I assume you know the difference between ++x and x++ right? You should be carfule with those...
Topic archived. No new replies allowed.