943,660 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 521
  • C++ RSS
Dec 8th, 2008
0

Input Validation for an array

Expand Post »
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.

#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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Foe89 is offline Offline
22 posts
since Oct 2008
Dec 8th, 2008
0

Re: Input Validation for an array

Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 8th, 2008
0

Re: Input Validation for an array

Yeah, my bad, I marked as solved so it'll drop.
and to note

if ( grid[rows][cols] = 'O')
{ cout << "This space has been taken, enter another row and column";
cin >> rows >> cols; }

is what the bold was suppsed to show, I didn't type in the cout <<
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Foe89 is offline Offline
22 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: "Poking"
Next Thread in C++ Forum Timeline: Linked List Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC