-
Looping Probrems...
I have tried awhile and cannot find out why this won't work. :confused:
Here it is:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <dos.h>
int col = 0, row = 0 , x = 0, y = 0;
void cls()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}
char t[3][3];
void DisplayTable() {
cls();
cout << "Table:\n";
for (col = 1; col <= 3; col++) {
for (row = 1; row <= 3; row++) {
if (t[row][col] == '1') {
cout << "X";
}
if (t[row][col] == '2') {
cout << "O";
}
if (t[row][col] == NULL) {
cout << "-";
}
}
cout << endl;
}
}
void NewTable() {
for (col = 1; col <= 3; col++) {
for (row = 1; row <= 3; row++) {
t[row][col] = NULL;
row++;
}
col++;
}
}
void main() {
NewTable();
cout << "Tic Tac Toe\nBy: Scott A. Hand\nEnter X: ";
cin >> x;
cout << "Enter Y: ";
cin >> y;
t[x][y] = '1';
DisplayTable();
}
It's supposed to look like this if I say 2, 2:
---
-X-
---,
but it looks like this:
---
-X-
--
I can't figure it out! :( Thanks in advance.
-
big hint on how to fix your code.....
C++ counts from zero in arrays..... so your arrays should loop from 0 to 2 not 1 to 3!
-
Tried it...
:confused:It didn't work!!:confused:
-
Are you sure you modified your for loops to take this into account. Also, if the user enters 2,2 because arrays start from zero you will have to translate this to 1,1 before passing the values to your functions.
Code:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <dos.h>
int col = 0, row = 0 , x = 0, y = 0;
void cls()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}
char t[3][3];
void DisplayTable() {
cls();
cout << "Table:\n";
for (col = 0; col < 3; col++) {
for (row = 0; row < 3; row++) {
if (t[row][col] == '1') {
cout << "X";
}
if (t[row][col] == '2') {
cout << "O";
}
if (t[row][col] == NULL) {
cout << "-";
}
}
cout << endl;
}
}
void NewTable() {
for (col = 0; col < 3; col++) {
for (row = 0; row < 3; row++) {
t[row][col] = NULL;
row++;
}
col++;
}
}
int main() {
NewTable();
cout << "Tic Tac Toe\nBy: Scott A. Hand\nEnter X: ";
cin >> x;
cout << "Enter Y: ";
cin >> y;
t[x-1][y-1] = '1';
DisplayTable();
return 0;
}