Can anyone please explain to me why in the hell this isn't working... I want it to start at the beginning of the do loop if there is more than 1 character in the string, or if the character entered cannot be evaluated to a number from 1-9, but at the moment, if I enter something like:
1saisagiusgqje2132t1ge
It will register as 1.
I honestly can't see ANYTHING wrong with what I have, but I might just point out it's 3:00AM, and I'm tired, and I've probably got some ridiculously easy completely stupid mistake that I just cannot see.
I'm compiling with g++ on a Unix system. It's going to be a Tick Tack Toe game (I'm extremely bored)...
Here is the problematic snippet of code:
Code:
do {
cout << "Enter a board position number: ";
cin >> strpos;
pos = atoi(strpos.c_str());
if (strpos.size() > 1 || !pos)
continue;
} while (!CheckSquare(pos, board, 'x'));
And here is the rest of what I have so far if you're interested:
Code:
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int CheckSquare(int square, char ** board, char player);
int main() {
string strpos;
int pos;
char ** board;
board = new char*[3];
for (int i = 0; i < 3; i++)
board[i] = new char[3];
for (int i = 0; i < 3; i++)
for (int y = 0; y < 3; y++)
board[i][y] = '-';
do {
cout << "Enter a board position number: ";
cin >> strpos;
pos = atoi(strpos.c_str());
if (strpos.size() > 1 || !pos)
continue;
} while (!CheckSquare(pos, board, 'x'));
for (int i = 0; i < 3; i++)
for (int y = 0; y < 3; y++)
cout << board[i][y] << " ";
for (int i = 0; i < 3; i++)
delete [] board[i];
delete [] board;
}
int CheckSquare(int square, char ** board, char player) {
int down;
int side;
if (square >= 7) {
down = 2;
side = square - 7;
}
else if (square >= 4) {
down = 1;
side = square - 4;
}
else {
down = 0;
side = square - 1;
}
if (board[down][side] == '-')
board[down][side] = player;
else
return 0;
}
Thanks.
P.S I'm probably going to end up feeling completely stupid, but oh well... I have no idea why I can't figure this out HAHA!