I'm quite new too C++ but finally my experience is good enough to actually create something, that's what I thought... So I triedto make this easy Tic Tac Toe Game and I think i got it all right but obviously not cause when I try to choose somewhere to put my X or O the window shuts down. This is the Code if you know what's wrong then please help me.

#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <stdio.h>
#include <sstream>
using namespace std;
 
int main()
{
    char Csquare1('1');
    char Csquare2('2');
    char Csquare3('3');
    char Csquare4('4');
    char Csquare5('5');
    char Csquare6('6');
    char Csquare7('7');
    char Csquare8('8');
    char Csquare9('9'); 
    int PlayerTurn(1);
    bool GameOverWin(true);
    bool GameOver(false);
    
    do {
        cout << Csquare1 << "|" << Csquare2 << "|" << Csquare3 << "|" << endl;
        cout << "-+-+-" << endl;
        cout << Csquare4 << "|" << Csquare5 << "|" << Csquare6 << "|" << endl;
        cout << "-+-+-" << endl;
        cout << Csquare7 << "|" << Csquare8 << "|" << Csquare9 << "|" << endl;
        cout << "-+-+-" << endl;
        
        char cPlayerMark;
        if(PlayerTurn == 1) {
                      cPlayerMark = 'X';
        } else {
                      cPlayerMark = 'O';
        }
        
        cout << "Player" << PlayerTurn << "'s move" << endl;
        bool bValidMove;
        
        do{
             char NextMove;
             cin >> NextMove;
             bValidMove = true;
             
             if(NextMove == '1' && Csquare1 == '1') {
                         Csquare1 = cPlayerMark;
             } else if (NextMove == '2' && Csquare2 == '2') {
                         Csquare2 = cPlayerMark;
             } else if (NextMove == '3' && Csquare3 == '3') {
                         Csquare3 = cPlayerMark;
             } else if (NextMove == '4' && Csquare4 == '4') {
                         Csquare4 = cPlayerMark;
             } else if (NextMove == '5' && Csquare5 == '5') {
                         Csquare5 = cPlayerMark;
             } else if (NextMove == '6' && Csquare6 == '6') {
                         Csquare6 = cPlayerMark;
             } else if (NextMove == '7' && Csquare7 == '7') {
                         Csquare7 = cPlayerMark;
             } else if (NextMove == '8' && Csquare8 == '8') {
                         Csquare8 = cPlayerMark;
             } else if (NextMove == '9' && Csquare9 == '9') {
                         Csquare9 = cPlayerMark;
             } else {
                    cout << "Invalid move, try again" << endl;
             bValidMove = false;
             }
             
             }
             while (!bValidMove);
                   if(Csquare1 != '1') {
                               if(Csquare2 == Csquare1 && Csquare3 == Csquare1) {
                                            GameOverWin = true;
                   }           if(Csquare4 == Csquare1 && Csquare7 == Csquare1) {
                                           GameOverWin = true;
                   }           if(Csquare5 == Csquare1 && Csquare9 == Csquare1) {
                                           GameOverWin = true;
                   }
             }
                   if(Csquare3 != '3') {
                               if(Csquare6 == Csquare3 && Csquare9 == Csquare3) {
                                           GameOverWin = true;
                   }           if(Csquare5 == Csquare3 && Csquare7 == Csquare3) {
                                           GameOverWin = true;
                   }
             }
                   if(Csquare5 != '5') {
                               if(Csquare2 == Csquare5 && Csquare8 == Csquare5) {
                                           GameOverWin = true;
                   }           if(Csquare4 == Csquare5 && Csquare6 == Csquare5) {
                                            GameOverWin = true;
                   }   
             }
                   if(Csquare6 != '9') {
                               if(Csquare8 == Csquare9 && Csquare7 == Csquare9) {
                                            GameOverWin = true;
                   }   
             }
             
                   if(Csquare1 != '1' && Csquare2 != '2' && Csquare3 != '3' &&
                      Csquare4 != '4' && Csquare5 != '5' && Csquare6 != '6' &&
                      Csquare7 != '7' && Csquare8 != '8' && Csquare9 != '9' && !GameOverWin)
             {
                      GameOver = true;
             
             }
                   if(GameOverWin) {
                    cout << "Player" << PlayerTurn << "wins!" << endl;
                                   }
                    else {
                         cout << "Game Over\n" << endl;
             }
        cout << Csquare1 << "|" << Csquare2 << "|" << Csquare3 << "|" << endl;
        cout << "-+-+-" << endl;
        cout << Csquare4 << "|" << Csquare5 << "|" << Csquare6 << "|" << endl;
        cout << "-+-+-" << endl;
        cout << Csquare7 << "|" << Csquare8 << "|" << Csquare9 << "|" << endl;
        cout << "-+-+-" << endl;
             if(PlayerTurn == 1) {
                           PlayerTurn = 2;
            } else {
                   PlayerTurn = 1;
            }
} while(!GameOverWin);
}

Change

while( !GameOverWin );

to

while( GameOverWin );

First some remarks on your code:

  • Where are you using these libraries in your code?
    #include <windows.h>
    #include <cstdlib> // for system("pause"); junk
    #include <conio.h>
    #include <stdio.h>
    #include <sstream>
  • BTW, instead of creating a variable for each field, like this:
    char Csquare1('1');
    char Csquare2('2');
    char Csquare3('3');
    char Csquare4('4');
    char Csquare5('5');
    char Csquare6('6');
    char Csquare7('7');
    char Csquare8('8');
    char Csquare9('9');

    An array is the more common way to do that, e.g.:

    char board[9];
  • It's better to avoid using system("pause");

    Use cin.get(); as a replacement of system("pause"); , less typing and more portable :) !!

    Check out this if you want to know why :)

:)

Change

while( !GameOverWin );

to

while( GameOverWin );

well it does help, now it's not just shutting down, but every time I make a move I win, so it's still not working. D'you have any ideas why?

>now it's not just shutting down
But where did you wrote the code to shut down the PC? You haven't.
Your coding style is rusted. Have a look at this guide to improve.
Especially read the 2.3 and 2.5 section of this guide.

>now it's not just shutting down
But where did you wrote the code to shut down the PC? You haven't.
Your coding style is rusted. Have a look at this guide to improve.
Especially read the 2.3 and 2.5 section of this guide.

well actually i looked at some tutorials when I made this, I had some questions about what the best way to do certain stuffs were, and in the tutorial that I used there was no shutdown code. And by the way it's not my PC that's shutting down it's the program...

#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <stdio.h>
#include <sstream>
using namespace std;

int main()
{
    char Csquare1('1');
    char Csquare2('2');
    char Csquare3('3');
    char Csquare4('4');
    char Csquare5('5');
    char Csquare6('6');
    char Csquare7('7');
    char Csquare8('8');
    char Csquare9('9'); 
    int PlayerTurn(1);
    bool GameOverWin(false);
    bool GameOver(false);

    do {
        cout << Csquare1 << "|" << Csquare2 << "|" << Csquare3 << "|" << endl;
        cout << "-+-+-" << endl;
        cout << Csquare4 << "|" << Csquare5 << "|" << Csquare6 << "|" << endl;
        cout << "-+-+-" << endl;
        cout << Csquare7 << "|" << Csquare8 << "|" << Csquare9 << "|" << endl;
        cout << "-+-+-" << endl;


        char cPlayerMark;
        if(PlayerTurn == 1) {
                      cPlayerMark = 'X';
        } else {
                      cPlayerMark = 'O';
        }

        cout << "Player" << PlayerTurn << "'s move" << endl;
        bool bValidMove;

        do{
             char NextMove;
             cin >> NextMove;
             bValidMove = true;

             if(NextMove == '1' && Csquare1 == '1') {
                         Csquare1 = cPlayerMark;
             } else if (NextMove == '2' && Csquare2 == '2') {
                         Csquare2 = cPlayerMark;
             } else if (NextMove == '3' && Csquare3 == '3') {
                         Csquare3 = cPlayerMark;
             } else if (NextMove == '4' && Csquare4 == '4') {
                         Csquare4 = cPlayerMark;
             } else if (NextMove == '5' && Csquare5 == '5') {
                         Csquare5 = cPlayerMark;
             } else if (NextMove == '6' && Csquare6 == '6') {
                         Csquare6 = cPlayerMark;
             } else if (NextMove == '7' && Csquare7 == '7') {
                         Csquare7 = cPlayerMark;
             } else if (NextMove == '8' && Csquare8 == '8') {
                         Csquare8 = cPlayerMark;
             } else if (NextMove == '9' && Csquare9 == '9') {
                         Csquare9 = cPlayerMark;
             } else {
                    cout << "Invalid move, try again" << endl;
             bValidMove = false;
             }

             }
             while (!bValidMove);
                   if(Csquare1 != '1') {
                               if(Csquare2 == Csquare1 && Csquare3 == Csquare1) {
                                            GameOverWin = true;
                   }           if(Csquare4 == Csquare1 && Csquare7 == Csquare1) {
                                           GameOverWin = true;
                   }           if(Csquare5 == Csquare1 && Csquare9 == Csquare1) {
                                           GameOverWin = true;
                   }
             }
                   if(Csquare3 != '3') {
                               if(Csquare6 == Csquare3 && Csquare9 == Csquare3) {
                                           GameOverWin = true;
                   }           if(Csquare5 == Csquare3 && Csquare7 == Csquare3) {
                                           GameOverWin = true;
                   }
             }
                   if(Csquare5 != '5') {
                               if(Csquare2 == Csquare5 && Csquare8 == Csquare5) {
                                           GameOverWin = true;
                   }           if(Csquare4 == Csquare5 && Csquare6 == Csquare5) {
                                            GameOverWin = true;
                   }   
             }
                   if(Csquare6 != '9') {
                               if(Csquare8 == Csquare9 && Csquare7 == Csquare9) {
                                            GameOverWin = true;
                   }   
             }

                   if(Csquare1 != '1' && Csquare2 != '2' && Csquare3 != '3' &&
                      Csquare4 != '4' && Csquare5 != '5' && Csquare6 != '6' &&
                      Csquare7 != '7' && Csquare8 != '8' && Csquare9 != '9' && !GameOverWin)
             {
                      GameOver = true;

             }
                   if(GameOverWin) {
                    system("CLS");
                    cout << "Player" << PlayerTurn << "wins!\n" << endl;

        cout << Csquare1 << "|" << Csquare2 << "|" << Csquare3 << "|" << endl;
        cout << "-+-+-" << endl;
        cout << Csquare4 << "|" << Csquare5 << "|" << Csquare6 << "|" << endl;
        cout << "-+-+-" << endl;
        cout << Csquare7 << "|" << Csquare8 << "|" << Csquare9 << "|" << endl;
        cout << "-+-+-" << endl;
        cin.get();
        cin.get();
        }
             if(PlayerTurn == 1) {
                           PlayerTurn = 2;
                           system("CLS");
            } else {
                   PlayerTurn = 1;
                   system("CLS");
            }
} while(!GameOverWin);
}

now it's working just fine...

Here is how would I change your code ... :)

#include <iostream>
#include <windows.h>

using namespace std;

void board(const char* Csquare)
{
	cout << Csquare[0] << "|" << Csquare[1] << "|" << Csquare[2] << "|" << endl;
	cout << "-+-+-" << endl;
	cout << Csquare[3] << "|" << Csquare[4] << "|" << Csquare[5] << "|" << endl;
	cout << "-+-+-" << endl;
	cout << Csquare[6] << "|" << Csquare[7] << "|" << Csquare[8] << "|" << endl;
	cout << "-+-+-" << endl;
}

int check(const char* Csquare, const char mark)
{
	if(Csquare[0] == mark && Csquare[1] == mark && Csquare[2] == mark ) return 1;
	else if(Csquare[0] == mark && Csquare[4] == mark && Csquare[8] == mark) return 1;
	else if(Csquare[0] == mark && Csquare[3] == mark && Csquare[6] == mark) return 1;
	else if(Csquare[2] == mark && Csquare[5] == mark && Csquare[8] == mark) return 1;
	else if(Csquare[2] == mark && Csquare[4] == mark && Csquare[6] == mark) return 1;
	else if(Csquare[3] == mark && Csquare[4] == mark && Csquare[5] == mark) return 1;
	else if(Csquare[1] == mark && Csquare[4] == mark && Csquare[7] == mark) return 1;
	else if(Csquare[6] == mark && Csquare[7] == mark && Csquare[8] == mark) return 1;
	else return 0;
}

int main()
{
	char Csquare[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
	int PlayerTurn(1);

	do
	{
		system("cls");
		board(Csquare);

		char cPlayerMark;
		if(PlayerTurn == 1) cPlayerMark = 'X';
		                else cPlayerMark = 'O';

		cout << "Player" << PlayerTurn << "'s move" << endl;

		while(true)
		{
			int NextMove;
			cin >> NextMove;

			if((Csquare[NextMove - 1] != 'X' && Csquare[NextMove - 1] != 'O') &&
			    (NextMove < 10 && NextMove > 0))
			{
				Csquare[NextMove - 1] = cPlayerMark;
				break;
			}
			else 
				cout << "Invalid move, try again!\n-> ";
		}

		if(check(Csquare, cPlayerMark))
		{
			system("cls");
			board(Csquare);
			cout << "Player" << PlayerTurn << " wins!" << endl;
			break;
		}
		else if (Csquare[0] != '1' && Csquare[1] != '2' && Csquare[2] != '3' &&
		        Csquare[3] != '4' && Csquare[4] != '5' && Csquare[5] != '6' &&
		        Csquare[6] != '7' && Csquare[7] != '8' && Csquare[8] != '9' )
		{
			system("cls");
			board(Csquare);
			cout << "Game Over!" << endl;
			break;
		}

		if (PlayerTurn == 1) PlayerTurn = 2;
		                else PlayerTurn = 1;
	}
	while (true);

	return 0;
}

Its in Siddhant3's signature ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.