| | |
Input Validation for an array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 22
Reputation:
Solved Threads: 0
I'm currently writing code for a soon to be simple tic tac toe program. Currently i'm just testing out methods of how to run it, so it'll be more "modular" in the end but for now I'm just testing things out. My question is, how would I go about validating that part a 2d array(or in this case a tic tac toe board) has already been given a value. This is my testing code and it stops when I try to validate with a if statement.
What's bolded is what I thought would work, but doesn't.
#include <iostream>
#include <iomanip>
using namespace std;
const gRow = 3;
const gCol = 3;
void showGrid(char grid[][gCol], int);
int main()
{ char grid[gRow][gCol] = {{'*', '*', '*'},
{'*', '*', '*'},
{'*', '*', '*'}};
int rows, cols;
int el = 1;
cout << " TIC TAC TOE" << endl;
cout << "Player One: X Player Two: O\n" << endl;
do {
showGrid(grid, gRow);
cout << "Player 1, Enter the row and column:";
cin >> rows >> cols;
if ( grid[rows][cols] = 'O')
{ "This space has been taken, enter another row and column";
cin >> rows >> cols; }
else
grid[rows][cols] = 'X';
showGrid(grid, gRow);
cout << "Player 2, Enter the row and column:";
cin >> rows >> cols;
grid[rows][cols] = 'O';
}
while(el = 1);
showGrid(grid, gRow);
return 0;
}
void showGrid(char array[][gCol], int numRows)
{
for (int row = 0; row < numRows; row++)
{ for (int col = 0; col < gCol; col++)
{
cout << setw(2) << array[row][col] << " ";
}
cout << endl;
}
}What's bolded is what I thought would work, but doesn't.
![]() |
Similar Threads
- Array gone awry (Java)
- C++ array input validation help. (C++)
- html/php form for .htaccess validation (PHP)
- preg_match validation (PHP)
- Validation Program (C++)
- seg fault when accessing the array (C)
- C++ char array parsing-Help (C++)
- Problem with validation in c (C++)
Other Threads in the C++ Forum
- Previous Thread: "Poking"
- Next Thread: Linked List Help
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






