Condition never true

This is a piece of a simple Tic Tac Toe program...
The problem is that in the main menu function whenever else condition is true the program doesn't quit. (It supposed to return to main and check bool var and then returns 0)

How to fix this without changing the data type of MainMenu function? Also how can I write this in a more better way without changing the data type of MainMenu also?

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
#include <iostream>
#include <Windows.h> // For Sleep function
#include <ctime> // For srand change
#include <cstdlib> // For rand & srand
// #include <fstream> // To save results
using namespace std;

// Function Prototyping
void MainMenu(); // MainMenu
void gameBoard(); // Board
void BlankO(); // Saved function to import player 2 (O) moves
void BlankX(); // Saved function to import player 1 (X) moves
void checkWin(); // Who won?

// Global vars
bool gameOver = false; // Quit
char boardArray[3][3] = { { '1', '2', '3' },{ '4', '5', '6' },{ '7', '8', '9' } }; // Board Array that displays squares with numbers
int playerInput;
void gameStart(); // Start playing!!

int main()
{
	if (gameOver == true) { return 0; } // Check
	MainMenu(); // Launch MainMenu + Game function sequence
}

void MainMenu() // Options and game title
{
	cout << "** Tic Tac Toe**" << endl; // Game title
	cout << "-------------------------" << endl; // Seperator
	cout << "Do you want to start the game? (Y/N): "; // Option to start

	char firstChoice; // Input var
	cin >> firstChoice; // User Input
	
	if (firstChoice == 'Y' || firstChoice == 'y') { system("cls"); gameBoard(); } // Go to gameBoard if input = Yes
	else { cout << "Quiting..."; Sleep(2000); gameOver = true; main(); } // Quit
	gameStart(); // Start the game after game board finishes displaying
}
Last edited on
use system("pause") instead of return 0

it will not quit your program if the condition is false
I want to directly quit the program, not just to pause it.
The rest of the program if the user pressed 'Y' is working 100% fine, it's just that when 'n' is pressed the program doesnt quit
Last edited on
The problem is that in the main menu function whenever else condition is true the program doesn't quit. (It supposed to return to main and check bool var and then returns 0)


check your compiler or build it , tested your program at cpp.sh and it working fine
whenever the else statement is true is program is quitting
this is the output
** Tic Tac Toe**
-------------------------
Do you want to start the game? (Y/N): n
Quiting...


code
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
48
49
50
51
52
53
54
#include <iostream> // For Sleep function
#include <ctime> // For srand change
#include <cstdlib> // For rand & srand
// #include <fstream> // To save results
using namespace std;

// Function Prototyping
void MainMenu(); // MainMenu
void gameBoard(); // Board
//void BlankO(); // Saved function to import player 2 (O) moves
//void BlankX(); // Saved function to import player 1 (X) moves
//void checkWin(); // Who won?

// Global vars
bool gameOver = false; // Quit
char boardArray[3][3] = { { '1', '2', '3' },{ '4', '5', '6' },{ '7', '8', '9' } }; // Board Array that displays squares with numbers
int playerInput;
void gameStart(); // Start playing!!

int main()
{
	// Lets do this
	if (gameOver == true) { 
	    
	    return 0;
	    //system("pause");
	    } // Check
	MainMenu(); // Launch MainMenu + Game function sequence
}

void MainMenu() // Options and game title
{
	cout << "** Tic Tac Toe**" << endl; // Game title
	cout << "-------------------------" << endl; // Seperator
	cout << "Do you want to start the game? (Y/N): "; // Option to start

	char firstChoice; // Input var
	cin >> firstChoice; // User Input
	
	if (firstChoice == 'Y' || firstChoice == 'y') { system("cls"); gameBoard();
	} // Go to gameBoard if input = Yes
	else { cout << "Quiting..."; //Sleep(2000);
	gameOver = true;
	main();
	} // Quit
	//gameStart(); // Start the game after game board finishes displaying
}

void gameBoard()
{
    cout<<"\nHello";
}

your gameStart() function is calling again whenver ever the condition is false
problem fixed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void MainMenu() // Options and game title
{
    bool check=true;
	cout << "** Tic Tac Toe**" << endl; // Game title
	cout << "-------------------------" << endl; // Seperator
	cout << "Do you want to start the game? (Y/N): "; // Option to start

	char firstChoice; // Input var
	cin >> firstChoice; // User Input
	
	if (firstChoice == 'Y' || firstChoice == 'y') { system("cls"); gameBoard();
	} // Go to gameBoard if input = Yes
	else { cout << "Quiting..."; //Sleep(2000);
	gameOver = true;
	main();
	check=false;
	} // Quit
	if(check==true)
	gameStart(); // Start the game after game board finishes displaying
}


output

** Tic Tac Toe**
-------------------------
Do you want to start the game? (Y/N): n
Quiting... 



Last edited on
That worked thanks very much!
Topic archived. No new replies allowed.