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;
}
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";
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.
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
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);
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...