g++ problem?

I compiled a tictactoe game that seg faults when i compile it and run it on my machine (Ubuntu 12.04 LTS with 128gb SSD i7 proccessor 8gb ram) but when compiled on a windows machine it compiles and runs as designed, what could me the problem? I can provide any information you might need.
Thanks,
hunterb24
closed account (Dy7SLyTq)
can you post the source
3 cpp files and 2 header files:

main.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "player.h"
using namespace std;

int main()
{
	//cout << "Welcome to Tic-Tac-Toe" << endl<< "----------------------" << endl << endl;

	//create player objects
	Player firstPlayer, secondPlayer;

	//create temp string to store player name
	string tempName;

	//generate random person to start game
	unsigned seed = time(0);
	srand(seed);
	int playerNumber;
	playerNumber = 1 + rand() % 2;


	//enter player 1 name, then set name and player number
	cout << "Enter your name, Player 1: ";
	cin >> tempName;
	firstPlayer.setValue(tempName, 1);
	tempName = "";

	//enter player 1 name, then set name and player number
	cout << "Enter your name, Player 2: ";
	cin >> tempName;
	secondPlayer.setValue(tempName, 2);

	//display toss winner
	if(playerNumber == 1)
		cout << firstPlayer.getName() << " won the toss."<< endl;
	else if (playerNumber == 2)
		cout << secondPlayer.getName() << " won the toss."<< endl;


	//initialize board
	tictactoe game;

	//declare game board argument
	string board;

	do
	{

		if(playerNumber == 1)
		{
			//display board
			game.operator <<(board);

			firstPlayer.NextMove(game);
			//if player1 wins end game
			if(game.getStatus() == 1)
			{
				cout << firstPlayer.getName() << " wins!";
				break;
			}
			playerNumber = 2;
		}
		else if(playerNumber == 2)
		{
			//display board
			game.operator <<(board);
			secondPlayer.NextMove(game);

			//if player1 wins end game
			if(game.getStatus() == 2)
			{
				cout << secondPlayer.getName() << " wins!";
				break;
			}

			playerNumber = 1;
		}


	}while (game.getStatus() != 0);

	return 0;
}


tictactoe.h
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
#include <iostream>
#include <string>
using namespace std;

class tictactoe
{
	public:
		bool setValue(int row, int column, int playerIndex); // set array to X or O if valid
		enum status{tie, player1, player2, notFinished}; // returns: 0 tie, 1 player1, 2 player2, 3 game not finished
		status getStatus();
		string operator<<(string board1)
		{
			    cout << "  " 
					 << gameBoard[0][0] << " | "         
					 << gameBoard[0][1] << " | "         
                     << gameBoard[0][2] << endl
				     << "-------------" << endl  
				     << "  " 
					 << gameBoard[1][0] << " | "         
					 << gameBoard[1][1] << " | "         
					 << gameBoard[1][2] << endl         
				     << "-------------" << endl
				     << "  " 
				     << gameBoard[2][0] << " | "         
					 << gameBoard[2][1] << " | "         
					 << gameBoard[2][2] << endl; 

				return board1;
		};

		//initial game board
		tictactoe()
			{
			for(int i = 0; i<= 3; i++)
				for(int j = 0; j <= 3; j++)
					gameBoard[i][j] = '\0';
			};


	private:
		char gameBoard[3][3];
};


tictactoe.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <string>
#include "tictactoe.h"
using namespace std;

//function checks for valid input and sets the values of the array to the player specified location
bool tictactoe::setValue(int row, int column, int playerIndex)
{
	bool returnValue;

	//change row and column from array form to normal
	row--; column--;

	//check for valid input and set X or O
	if (((row >= 0) && (row <= 2)) && ((column >= 0) && (column <= 2)) && (gameBoard[row][column] == '\0'))
	{
		if(playerIndex == 1)
		{
			gameBoard[row][column] = 'X';
			returnValue = true; //valid input // plact X at designated spot
		}
		else if(playerIndex == 2)
		{
			gameBoard[row][column] = 'O';
			returnValue = true; //valid input //place O at designated spot
		}
	}
	else
		returnValue = false; //invalid input

	return returnValue;
}

//checks status of game
tictactoe::status tictactoe::getStatus()
{
	//return value
	status returnStatus = notFinished;
		//check if player1 wins
	    if ((gameBoard[0][0] == 'X') && (gameBoard[1][1] == 'X') && (gameBoard[2][2] == 'X'))
	    	returnStatus = player1;

	    else if ((gameBoard[2][0] == 'X') && (gameBoard[1][1] == 'X') && (gameBoard[0][2] == 'X'))
	    	returnStatus = player1;

	    else if ((gameBoard[0][0] == 'X') && (gameBoard[0][1] == 'X') && (gameBoard[0][2] == 'X'))
	    	returnStatus = player1;

	    else if ((gameBoard[0][0] == 'X') && (gameBoard[0][1] == 'X') && (gameBoard[0][2] == 'X'))
	    	returnStatus = player1;

	    else if ((gameBoard[1][0] == 'X') && (gameBoard[1][1] == 'X') && (gameBoard[1][2] == 'X'))
	    	returnStatus = player1;

	    else if ((gameBoard[2][0] == 'X') && (gameBoard[2][1] == 'X') && (gameBoard[2][2] == 'X'))
	    	returnStatus = player1;

	    else if ((gameBoard[1][0] == 'X') && (gameBoard[1][1] == 'X') && (gameBoard[1][2] == 'X'))
	    	returnStatus = player1;

	    else if ((gameBoard[2][0] == 'X') && (gameBoard[2][1] == 'X') && (gameBoard[2][2] == 'X'))
	    	returnStatus = player1;

	    //check if player1 wins
	    else if ((gameBoard[0][0] == 'O') && (gameBoard[1][1] == 'O') && (gameBoard[2][2] == 'O'))
	    	returnStatus = player2;
	    else if ((gameBoard[2][0] == 'O') && (gameBoard[1][1] == 'O') && (gameBoard[0][2] == 'O'))
	    	returnStatus = player2;

   	    else if ((gameBoard[0][0] == 'O') && (gameBoard[0][1] == 'O') && (gameBoard[0][2] == 'O'))
   	    	returnStatus = player2;

   	    else if ((gameBoard[0][0] == 'O') && (gameBoard[0][1] == 'O') && (gameBoard[0][2] == 'O'))
   	    	returnStatus = player2;

   	    else if ((gameBoard[1][0] == 'O') && (gameBoard[1][1] == 'O') && (gameBoard[1][2] == 'O'))
   	    	returnStatus = player2;

   	    else if ((gameBoard[2][0] == 'O') && (gameBoard[2][1] == 'O') && (gameBoard[2][2] == 'O'))
   	    	returnStatus = player2;

   	    else if ((gameBoard[1][0] == 'O') && (gameBoard[1][1] == 'O') && (gameBoard[1][2] == 'O'))
   	    	returnStatus = player2;

   	    else if ((gameBoard[2][0] == 'O') && (gameBoard[2][1] == 'O') && (gameBoard[2][2] == 'O'))
   	    	returnStatus = player2;

	    //check if game still playing
	    else if(returnStatus != player1 || returnStatus != player2)
   	    {

    	    for(int row = 0; row <= 2; row++)
   	    		for(int column = 0; column <= 2; column++)
   	    			if (gameBoard[row][column] == 0)
   	    			{
    					returnStatus = notFinished;
   	    			}
	    	    }

	    //game ends in tie
	    else
	    {
	    	returnStatus = tie;
	    }

	    return returnStatus;
}


player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "tictactoe.h"
using namespace std;

class Player
{
	public:
		int getIndex(); //return 1 or 2
		string getName(); //return name of player
		void nextMove(); //accepts object by reference, prompt player to enter move, accept move and make changes to array
		void setValue(string tempName, int number); //set name and number to players
		void NextMove(tictactoe &tictactoe1);
	private:
		string playerName;
		int playerNumber;

};


player.cpp
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
#include <iostream>
#include <string>
#include "player.h"
using namespace std;

//return name of player
string Player::getName()
{
	return playerName;
}

//sets players name and number
void Player::setValue(string tempName, int number)
{
	playerName = tempName;
	playerNumber = number;
}

//plays the next specified move
void Player::NextMove(tictactoe &tictactoe1)
{
	int row = 0, column = 0;
	bool finished = false;

	cout << playerName << " make your move.";
	do
	{
			cin >> row >> column;

			finished = tictactoe1.setValue(row, column, playerNumber);

			if(!finished)
				cout << "Illegal move, make another one: ";


	}while(!finished);
}
1
2
for(int i = 0; i<= 3; i++)
    for(int j = 0; j <= 3; j++)


Should be:

1
2
3
4
5
for(int i = 0; i < 3; i++) {
    for(int j = 0; j < 3; j++) {
        // do something
    }
}


It's a good idea to always use the braces, even though there is 1 statement following. It will save you one day when you add more code.


gameBoard[3][3] is out of bounds for the array. Valid subscripts are 0, 1, 2 .

The body of the for loop is not evaluated when the end condition becomes false, so it stops at 2.

I would change the other ones that look like this for(int row = 0; row <= 2; row++) .

I haven't looked any further for other errors.

The way to find out about runtime errors is to use the debugger - should be easy if you are using an IDE, a little bit more tricky from the command line but not impossible. You can have a watch list of variables, see how they change as you step through the code 1 line at a time, see where they go wrong & deduce the problem.

I find function names, that are too general as well as being the same across different classes, a bit confusing . You have setValue in the player class & the tictactoe class, can you rename them to something more meaningful?

HTH
Last edited on
Topic archived. No new replies allowed.