4 Replies - 1391 Views - Last Post: 14 January 2009 - 10:41 AM Rate Topic: -----

#1 lukeyboyuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-January 09

Naughts and Crosses game program, void error

Posted 14 January 2009 - 08:38 AM

I have to have a complete 0's and X's program for university work, im only a first year and i cannot seem to get this fixed.
I added the void gboard function before i added the code to allow the users to enter where they would like to place their piece and it was working. Since then, it stopped working and Ive tried lots to fix it, i dont know if im just missing a simple mistake or what. I havnt added any code to check if a player has won, but i will get that done soon, i just need to overcome this problem first. Any help would be greatly appreciated, thanks:

Errors:
1>.\Coursework3.cpp(22) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>.\Coursework3.cpp(229) : error C2062: type 'void' unexpected
1>.\Coursework3.cpp(230) : error C2143: syntax error : missing ';' before '{'

// Coursework3.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <string>
#include <cstdlib>
#include <stdlib.h>
using namespace std;

void gboard();

int store, store1, count, player, turn;
int gameboard [3][3];
string player1 = player1;
string player2 = player2;
char start;

int main()

{
	srand ( time(NULL));

	cout << "Hello, and welcome to the game of Naughts and Crosses.\n";
	cout << "A random select has chosen who will go first, and who will play as O and who	will play as X.\n\n";

	player = rand() % 2 + 1;
	count = 1;
	store = 0;

		do
	{
		store1 = 0;

		do
		{
			gameboard[store][store1] = count;
			count++;
			store1++;
		} while (store1 < 3);
		store++;
	} while (store <3);

	cout << "Instructions: This is the layout of the Naughts and Crosses board.  You enter a number which corresponds to the place on the board.\n";
	cout << "These are the different ways in which you can win. You must get 3 of your piecesin a row.\n\n";
	cout << " Start	  Win	   Win	   Win	  Draw  \n";
	cout << " 1 2 3	 O O O	 O 2 3	 1 2 O	 O O X \n";
	cout << " 4 5 6  -  4 5 6  -  O 5 6  -  4 O 6  -  X X O \n";
	cout << " 7 8 9	 7 8 9	 O 8 9	 O 8 8	 O X O \n";

	do
	{
	cout << "When you are ready enter 's' to start the game, if you would like to quit enter 'q'.\n";
	cin >> start;
	start = tolower(start);
	if (start == 's')
	{
		system("cls");
		gboard();

	if (player == 1)
		cout << "Player 1's turn first. \n\n";
	else
		cout << "Player 2's turn first. \n\n";

	if (player == 1)
	{
		cout << "Player1: 0 \n";
		cout << "Player2: X \n\n";
	}
	else
	{
		cout << "Player1: X \n";
		cout << "Player2: O \n\n";
	}
	cout << "player " << player << "'s turn, please enter where you would like to place your piece: \n";


	do
	{
		
			do
			{
				turn = 0;
				cin >> turn;
				store1 = 0;

				if (turn < 1 )
					cout <<"move is less than 1";
				cin >> start;

				if (turn > 9)
					cout << "move is greater than 9";
				cin >> start;

			}while ((turn < 1) || (turn > 9)); 

			switch (turn)
			{
			case 1:
					if (gameboard[0][0] <=9)
					{
						gameboard[0][0] = player*1000;
					}
					else
					{
						cout << "You cannot place your piece here, slot is taken";
						store1 = 1;
					}
					break;

			case 2:
					if  (gameboard[0][1] <=9)
					{
						gameboard[0][1] = player*1000;
					}
					else
					{
					cout << "You cannot place your piece here, slot is taken";
					store1 = 1;
					}
					break;

			case 3:
					if  (gameboard[0][2] <=9)
					{
						gameboard[0][2] = player*1000;
					}
					else
					{
					cout << "You cannot place your piece here, slot is taken";
					store1 = 1;
					}
					break;
			case 4:
					if  (gameboard[1][0] <=9)
					{
						gameboard[1][0] = player*1000;
					}
					else
					{
					cout << "You cannot place your piece here, slot is taken";
					store1 = 1;
					}
					break;
			case 5:
					if  (gameboard[1][1] <=9)
					{
						gameboard[1][1] = player*1000;
					}
					else
					{
					cout << "You cannot place your piece here, slot is taken";
					store1 = 1;
					}
					break;
			case 6:
					if  (gameboard[1][2] <=9)
					{
						gameboard[1][2] = player*1000;
					}
					else
					{
					cout << "You cannot place your piece here, slot is taken";
					store1 = 1;
					}
					break;
			case 7:
					if  (gameboard[2][0] <=9)
					{
						gameboard[2][0] = player*1000;
					}
					else
					{
					cout << "You cannot place your piece here, slot is taken";
					store1 = 1;
					}
					break;
			case 8:
					if  (gameboard[2][1] <=9)
					{
						gameboard[2][1] = player*1000;
					}
					else
					{
					cout << "You cannot place your piece here, slot is taken";
					store1 = 1;
					}
					break;
			case 9:
					if  (gameboard[2][2] <=9)
					{
						gameboard[2][2] = player*1000;
					}
					else
					{
					cout << "You cannot place your piece here, slot is taken";
					store1 = 1;
					}
					break;

					default:
						cout << "You have entered an incorrect place";
						cin >> store;

	}
	if (start == 'q')
	exit(0);
	
	else
	{
		system("cls");
	cout << "You have entered an incorrect choice please enter your choice again.";
	}
	}while (( start != 's') || (start != 'q'));

}





	return 0;
}




void gboard()
{
	store = 0;
	store1 = 0;
	do
	{
		store1 = 0;
	
	do
	{
		if (gameboard[store][store1] > 9)

			if (gameboard[store][store1] == 1000)
			{
				cout <<" X ";
			}
			else if (gameboard[store][store1] == 2000)
			{
				cout << " O ";
			}
			else;

		else if (gameboard[store][store1] < 100)
		{
			cout << gameboard[store][store1] << "  ";
		}

		store1++;

		if (store1 < 3) cout << "|  ";

	} while (store1 < 3);

	if (store < 2)
	{
		cout << "\n";
		cout << "   |	 |\n";
		cout << "---|-----|---\n";
		cout << "   |	 |\n";
	}
	store++;
}while (store < 3);
cout << "\n\n\n\n";

}


This post has been edited by lukeyboyuk: 14 January 2009 - 08:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Naughts and Crosses game program, void error

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Naughts and Crosses game program, void error

Posted 14 January 2009 - 08:53 AM

Your braces are not matching up. Go through and check them, particularly your do-whiles. Your indentation is a mess, and if that were cleaned up, perhaps it would have been more obvious. That's a start.
Was This Post Helpful? 0
  • +
  • -

#3 lukeyboyuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-January 09

Re: Naughts and Crosses game program, void error

Posted 14 January 2009 - 09:04 AM

Ok, I found an open brace here, i simply deleted it and fixed the void problem:

	if (start == 's')
	{
		system("cls");
		gboard();



Now im getting an error:

error C2059: syntax error : 'return'

This post has been edited by lukeyboyuk: 14 January 2009 - 09:04 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Naughts and Crosses game program, void error

Posted 14 January 2009 - 09:52 AM

Go through your *entire* code and match up all your braces (i.e., { and }). Print it out and use pen and paper if necessary.
Was This Post Helpful? 0
  • +
  • -

#5 matthew180  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 07-January 09

Re: Naughts and Crosses game program, void error

Posted 14 January 2009 - 10:41 AM

The time() function returns its result as a time_t value, which you are turning around and feeding to srand() which takes an unsigned int. You are doing this without a case, so you get an error. You can see more clearly if you separate the calls:

time_t t;

t = time(NULL);
srand(t);




If you don't care about the loss of data, then explicitly make a cast so the compiler knows you realize you are losing data:

time_t t;
unsigned int seed;

t = time(NULL);
seed = (unsigned int)t;
srand(seed);



Matthew
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1