start the game by pressing enter

I have two more questions.
The 1st question is that I want the player start the game by pressing enter and end the game by entering "no", but I fail! The game doesn't start by just pressing enter...what should I do to fix it ?
1
2
3
4
5
6
7
8
9
10
bool playthegame=true;
	cout<<"You wnat to play the game ?    )";
	char choice;
	cin>>choice;
	if (choice=="No"||choice=="No"||choice=="NO"||choice=="nO"||choice=="n"||choice=="N")
		playthegame=false;
	else if (choice=="a")
		playthegame=true;
	while (playthegame==true)
{....}
cin>>choice;
expects you to type in a value,
yes, no, 0, 1


To start the game just by pressing enter, look up getch()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <limits>
#include <iostream>

int main()
{
    const char* prompt = "To begin, press Enter.  (Or use 'n' to stop.)\n" ;

    char ch ;
    while ( std::cout << prompt && (ch= std::cin.get()) != '\n' && ch != 'n' && ch != 'N' )
    {
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') ;
        std::cout << "Invalid input!\n" ;
    }

    bool play_game = ch == '\n' ;

    std::cout << "play_game = " << (play_game ? "true\n" : "false\n") ;
}
Topic archived. No new replies allowed.