Hey I am currently making a main menu for a console app. It tests whether the user has hit the keys: 1,2 or 3 and then performs the relevant task.
Only problem is that the user can hold down the keys for the task to repeat endlessly. How can i test for on release of the key so you can't see an endless amount of spamming?
int main(int argc, char *argv[])
{
bool game = false; //whether to reset info and screen or not
bool show_numbers = true; //whether to show numbers or - instead
//computer_game();
while(1)
{
system("cls");
cout<<"Welcome to Tic-Tac-Toe, created by Charlie Edmunds.\nPlease select from the following options:\n\n1) Play against a friend\n2) Play against computer\n3) Currently showing ";
if (show_numbers) {cout<<"numbers instead of -";}
else {cout<<"- instead of numbers";}
cout<<". Press 3) to change";
cout<<"\n\n";
game = false;
//start games options
while (game == false)
{
if(kbhit())
{
int m = getch()-48;
if (m == 1)
{
player_game();
//cout<<endl<<"Test";
break;
game = true;
}
elseif (m == 2)
{
computer_game();
//cout<<"\nGame mode "<<m<<" not yet active please wait whilst its being built";
}
elseif (m == 3)
{
show_numbers = !show_numbers;
//redraw
system("cls");
cout<<"Welcome to Tic-Tac-Toe, created by Charlie Edmunds.\nPlease select from the following options:\n\n1) Play against a friend\n2) Play against computer\n3) Currently showing ";
if (show_numbers) {cout<<"'numbers' instead of '-'";}
else {cout<<"'-' instead of 'numbers'";}
cout<<". Press 3) to change";
cout<<"\n\n";
}
}
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
EDIT: This does not actually work as desired. its stuck in a continuous loop :| i though kbhit() only returned true if a key was pressed down while it was called? There are way to many needed scripts not included in standard c libraries :|
bool game = false; //whether to reset info and screen or not
bool show_numbers = true; //whether to show numbers or - instead
bool can_change = true; //whether to be able to change
//computer_game();
while(1)
{
system("cls");
cout<<"Welcome to Tic-Tac-Toe, created by Charlie Edmunds.\nPlease select from the following options:\n\n1) Play against a friend\n2) Play against computer\n3) Currently showing ";
if (show_numbers) {cout<<"'numbers' instead of '-'";}
else {cout<<"'-' instead of 'numbers'";}
cout<<". Press 3) to change";
cout<<"\n\n";
game = false;
//start games options
while (game == false)
{
if(kbhit())
{
int m = getch()-48;
if (m == 1)
{
player_game(show_numbers);
//cout<<endl<<"Test";
//break;
//game = true;
}
elseif (m == 2)
{
computer_game(show_numbers);
//cout<<"\nGame mode "<<m<<" not yet active please wait whilst its being built";
}
elseif (m == 3)
{
do
{
if (can_change == true)
{
show_numbers = !show_numbers;
//redraw
system("cls");
cout<<"Welcome to Tic-Tac-Toe, created by Charlie Edmunds.\nPlease select from the following options:\n\n1) Play against a friend\n2) Play against computer\n3) Currently showing ";
if (show_numbers) {cout<<"'numbers' instead of '-'";}
else {cout<<"'-' instead of 'numbers'";}
cout<<". Press 3 to change";
cout<<"\n\n";
can_change = false;
}
}
while(kbhit());
}
}
can_change = true;
}
}
if i press the button '3' it will change but if held down after a short time it spams out and is rendered useless :| how can i solve this?