Tic tac toe, but no 'x' is being printed

'x' fails to go onto the board after inputing desired position

Page 1 of 1

4 Replies - 901 Views - Last Post: 24 November 2008 - 08:30 AM Rate Topic: -----

#1 justin2021  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-December 07

Tic tac toe, but no 'x' is being printed

Post icon  Posted 23 November 2008 - 07:05 PM

Hi, I'm having a bit of a problem having this tic-tac-toe program to output an 'x' onto the board
The program compiles but nothing seems to show up. This is what I have so far:

#include <iostream>
#include <string>
#include <cstdlib>
#include "graphics.h"
#include <iomanip>

using namespace std;

#define ROW 3
#define COL 3

void clearBoard(char [][3]);
void printBoard(char [][3]);


int main()
{
	string userinput;
	system("cls");
	setrgb(2);
	gotoxy(4,1);
	cout << "Random Tic Tac Toe!\n\n";
	char board[3][3];
	clearBoard(board);
	printBoard(board);	
	gotoxy(20, 20);
	cout << "TYPE IN YOUR COORDINATES WITH A SPACE INBETWEEN";
	getline(cin, userinput);
	char r = userinput[0];
	char c = userinput[2];
	board[r][c] = 'x';
	printBoard(board);

}
void clearBoard(char board[][3])
{
	int i,j;
	for(i = 0; i < ROW; i++)
	{
		for(j = 0; j < COL; j++)
		{
			board[i][j] = ' ';
		}
	}
}

void printBoard(char board[][3])
{
	gotoxy(25, 12);
	cout << " " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;
	gotoxy(25, 13);
	cout << "---+---+---" << endl;
	gotoxy(25, 14);
	cout << " " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;
	gotoxy(25, 15);
	cout << "---+---+---" << endl;
	gotoxy(25, 16);
	cout << " " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;
}


is there anything in there that could be preventing the 'x' from showing onto the board?
(BTW, graphics.h is just used for gotoxy() which puts the output in a certain position and setrgb which changes the text color in cmd prompt)

Is This A Good Question/Topic? 0
  • +

Replies To: Tic tac toe, but no 'x' is being printed

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Tic tac toe, but no 'x' is being printed

Posted 23 November 2008 - 09:29 PM

[edit]
My original answer was so dumb it needed to be removed in case it mislead anyone.
See N8wxs's answer for the real issue.
[/edit]

This post has been edited by janotte: 23 November 2008 - 10:10 PM

Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Tic tac toe, but no 'x' is being printed

Posted 23 November 2008 - 09:36 PM

You are inputing characters, not integers. You need to convert the ASCII character codes to integers. Change
char r = userinput[0];
char c = userinput[2];


to
int r = atoi(&userinput[0]);
int c = atoi(&userinput[2]);



See http://www.cplusplus...tdlib/atoi.html

This post has been edited by n8wxs: 23 November 2008 - 09:36 PM

Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5757
  • View blog
  • Posts: 12,571
  • Joined: 16-October 07

Re: Tic tac toe, but no 'x' is being printed

Posted 24 November 2008 - 06:55 AM

Might as well keep it C++...

Maybe something like:
int row, col;
cout << "TYPE IN YOUR COORDINATES WITH A SPACE INBETWEEN";
cin >> row;
cin >> col;



There, all the heavy lifting done. You might want to add some validation:

void processUserInput(char ch) {
   int row, col;
   bool valid = false;
   while (!valid)
      cout << "TYPE IN YOUR COORDINATES WITH A SPACE INBETWEEN";
      cin >> row;
      cin >> col;
      valid = (row<ROW && col<COL); // not allowing for negatives, btw.
      if (!valid) { cout << "Invalid values, try again."; }
   }
   board[row][col] = ch;
}



Hope this helps.

This post has been edited by baavgai: 24 November 2008 - 06:56 AM

Was This Post Helpful? 0
  • +
  • -

#5 justin2021  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-December 07

Re: Tic tac toe, but no 'x' is being printed

Posted 24 November 2008 - 08:30 AM

Thanks for the help, guys. Got it going just fine now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1