I'm writing a command-line tic-tac-toe game and i've encountered a problem in my helpMenu() function, whenever i recursively call the function with an argument, the cin.get() pause ceases to work. Any help appreciated, thanks.

The following is my test.cpp file, whenever i write some new function i always put it in a separate file first until i get it working exactly the way i want it to, then i place it in my main.cpp file.

#include <iostream>
#include <stdlib.h>

#ifdef _WIN32
	#define CMD_CLEAR "cls"
#ifdef linux
	#define CMD_CLEAR "clear"

using namespace std;

int helpMenu(int choice = 0);

int main() {
	return 0;

int helpMenu(int hM_choice) {
	if (hM_choice != 0) {
		// Argument specified.
		if ((hM_choice >= 1) && (hM_choice <= 3)) {
			cout << "\n\n\n\t\t\t";
			if (hM_choice == 1) {
				cout << "[ How to Play ]\n\n";
				cout << "\tThe aim of the game is to build a 5-in-a-row line across\n";
				cout << "\tthe grid in any direction before your opponent. This is\n";
				cout << "\tthe only way to win the game.\n\n";
			} else if (hM_choice == 2) {
				cout << "[ About ]\n\n";
				cout << "\tThis game was created for educational purposes by Exussum.\n\n";
				cout << "\tE-mail: exussum@gmx.com\n\n";
			} else { return 0; }

			cout << "\n\n\n Hit the return key to continue..\n";
		else { helpMenu(); }
	else {
		// No argument, display help menu.
		string hM_menuChoice;
		cout << "\n\n\n\t1) How To Play.\n\t2) About.\n\n\t3) Close Help Menu.\n ";
		cin >> hM_menuChoice;

		if (hM_menuChoice.length() == 1) {
			if (hM_menuChoice[0] == '1') { helpMenu(1); }
			else if (hM_menuChoice[0] == '2') { helpMenu(2); }
			else { helpMenu(3); }
		else {
			cout << "\n\n\n\n\n\t\t";
			cout << "Invalid menu choice." << ".\n\n\n\n";
			sleep(2); helpMenu();

	return 0;

A recursion does not bear a relation to your problems with cin. You never get a single keystroke from the cin stream until press Enter. The cin stream is not a keyboard (console) device. Moreover, after cin.get() the next waiting char '\n' (end of line) is still in the cin stream. You with get it on the next get call.
Probably, the most simple method to get user input is:

std::string input;
if (std::getline(cin,input)) {
   // OK, process input string
} else {
   // cin closed (Ctrl-Z pressed, for example)...

Have you ever seen Help with Code Tags link? Use code tag with the language specifier:
[code=cplusplus] source


This article has been dead for over six months. Start a new discussion instead.