Connecting Tech Pros Worldwide Help | Site Map

trouble with c++ while loop ?

Newbie
 
Join Date: May 2006
Posts: 2
#1: May 2 '06
Dear All,

I'm having trouble getting my c++ code to work (below). Any suggestions as to what could be the problem? I think it has to do with the while loop, since the program ends before command[0] is a 'Q'.

Thank you in advance.


#include <iostream>

#define MAX 8

using namespace std;

bool validCoord(char col, int row);
void processCoord(string command,char player,string board[MAX][MAX]);
int translateColumn(char letter);
void printboard(string board[MAX][MAX]);


int main(int argc, char *argv[])
{
std::cout << "************************************" << std::endl;
std::cout << "* Welcome to Reversi (Othello) *" << std::endl;
std::cout << "************************************" << std::endl;
std::cout << "" << std::endl;
std::cout << "Player 1 will be X and Player 2 will be O" << std::endl;
std::cout << "Enter ? for a list of commands" << std::endl;

std::cout << "c333333333333333333333333333333333333333333333333 33333333333cc" << std::endl;

std::string command;
std::cin >> command;

std::cout << command << std::endl;

int rows = 8;
int cols = 8;
string board[8][8] = {0};
char player = 'X';

while (command[0] != 'Q') {
if (command[0] == '?') {
std::cout << "? - display list of commands" << std::endl;
std::cout << "<Col><Row> - move to given coordinates" << std::endl;
std::cout << " where <Col> is a column letter (a-h)" << std::endl;
std::cout << " and <Row> is a row number (1-8)" << std::endl;
std::cout << "N - play new game" << std::endl;
std::cout << "P - display board" << std::endl;
std::cout << "Q - quit game" << std::endl;
}

if (command[0] == 'P')
printboard(board);
if (validCoord(command[0],command[1]))
processCoord(command, player, board);

std::cin >> command;


if (player == 'X')
player = 'O';
else
player = 'X';


std::cin >> command;
}
std::cout << "Enter ? for a list of commands" << std::endl;

std::cout << "c333333333333333333333333333333333333333333333333 33333333333cc" << std::endl;

std::string command2;
std::cin >> command2;
std::cout << command2 << std::endl;

return 0;
}

bool validCoord(char col, int row)
{
if (col == 'a' || col == 'b' || col == 'c' || col == 'd' || col == 'e' || col == 'f' || col == 'g' || col == 'h' || col == 'A' || col == 'B' || col == 'C' || col == 'D' || col == 'E' || col == 'F' || col == 'G' || col == 'H') {
if (row == '1' || row == '2' || row == '3' || row == '4' || row == '5' || row == '6' || row == '7' || row == '8')

return true;
else
cout << "Invalid Coordinates (" << col << "," << row << ")." << std::endl;
return false;
}
else
cout << "Invalid Coordinates (" << col << "," << row << ")." << std::endl;
return false;
}

void processCoord(string command,char player, string board[MAX][MAX]) {
if (board[translateColumn(command[0])][command[1]] != "0")
std::cout << "Position (" << command[0] << "," << command[1] << ") is occupied." << std::endl;
else {
if (player == 'X')
board[translateColumn(command[0])][command[1]] = 'X';
else
board[translateColumn(command[0])][command[1]] = 'O';
}
}

int translateColumn(char letter)
{
int col;

if (letter == 'a' || letter == 'A')
col = 1;
if (letter == 'b' || letter == 'B')
col = 2;
if (letter == 'c' || letter == 'C')
col = 3;
if (letter == 'd' || letter == 'D')
col = 4;
if (letter == 'e' || letter == 'E')
col = 5;
if (letter == 'f' || letter == 'F')
col = 6;
if (letter == 'g' || letter == 'G')
col = 7;
if (letter == 'h' || letter == 'H')
col = 8;

return col;
}

void printboard(string board[MAX][MAX])
{
int t;
int w;

cout << " +---+---+---+---+---+---+---+---+" << endl;
for (t = 0;t < MAX; t++)
cout << " " << 8 - t << " " << endl;
for (w = 0;w < MAX; w++)
cout << board[w][8 - t] << " | " << endl;
cout << " +---+---+---+---+---+---+---+---+" << endl;
cout << " a b c d e f g h " << endl;
}
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,162
#2: May 3 '06

re: trouble with c++ while loop ?


Expand|Select|Wrap|Line Numbers
  1. std::cout << "************************************" << std::endl;
Since you have included
Expand|Select|Wrap|Line Numbers
  1. using namespace std;
this can be simplified to
Expand|Select|Wrap|Line Numbers
  1. cout << "************************************" << endl;
This

Expand|Select|Wrap|Line Numbers
  1. if (col == 'a' || col == 'b' || col == 'c' || col == 'd' || col == 'e' || col == 'f' || col == 'g' || col == 'h' || col == 'A' || col == 'B' || col == 'C' || col == 'D' || col == 'E' || col == 'F' || col == 'G' || col == 'H') {
  2.     if (row == '1' || row == '2' || row == '3' || row == '4' || row == '5' || row == '6' || row == '7' || row == '8')
  3.         return true;
can be simplified to
Expand|Select|Wrap|Line Numbers
  1. if (tolower(col) >= 'a' && tolower(col) <= 'h' && row >= '1' && row <= '8')
  2.     return true;
Here is your problem

Expand|Select|Wrap|Line Numbers
  1. if (board[translateColumn(command[0])][command[1]] != "0")
You translate the first character of column from letters to integers but you do not translate the second character, you use it directly. However command[1] does not have the value in the range 0 - 7 which is what is required to be in range for the variable board, it has the range '1' - '8' which is decimal is 49 - 56, well out of range. You need a translateRow function in the same way you have a translate column.

This error appears multiple times in the function processCoord

and finally this
Expand|Select|Wrap|Line Numbers
  1. int translateColumn(char letter)
  2. {
  3. int col;
  4.  
  5. if (letter == 'a' || letter == 'A')
  6. col = 1;
  7. if (letter == 'b' || letter == 'B')
  8. col = 2;
  9. if (letter == 'c' || letter == 'C')
  10. col = 3;
  11. if (letter == 'd' || letter == 'D')
  12. col = 4;
  13. if (letter == 'e' || letter == 'E')
  14. col = 5;
  15. if (letter == 'f' || letter == 'F')
  16. col = 6;
  17. if (letter == 'g' || letter == 'G')
  18. col = 7;
  19. if (letter == 'h' || letter == 'H')
  20. col = 8;
  21.  
  22. return col;
  23. }
also has an error in it, it returns a value between 1 and 8 however this is used as an index into the board array. Since this array is declared string board[8][8] the index needs to be in the range 0 - 7. Additionally this could be written as
Expand|Select|Wrap|Line Numbers
  1. int translateColumn(char letter)
  2. {
  3.     return tolower(letter) - 'a';
  4. }
To use the function tolower you need to include the header ctype.h
Newbie
 
Join Date: May 2006
Posts: 2
#3: May 18 '06

re: trouble with c++ while loop ?


Thank you, Banfa, for your help :D

Quote:

Originally Posted by Banfa

Expand|Select|Wrap|Line Numbers
  1. std::cout << "************************************" << std::endl;
Since you have included
Expand|Select|Wrap|Line Numbers
  1. using namespace std;
this can be simplified to
Expand|Select|Wrap|Line Numbers
  1. cout << "************************************" << endl;
This

Expand|Select|Wrap|Line Numbers
  1. if (col == 'a' || col == 'b' || col == 'c' || col == 'd' || col == 'e' || col == 'f' || col == 'g' || col == 'h' || col == 'A' || col == 'B' || col == 'C' || col == 'D' || col == 'E' || col == 'F' || col == 'G' || col == 'H') {
  2.     if (row == '1' || row == '2' || row == '3' || row == '4' || row == '5' || row == '6' || row == '7' || row == '8')
  3.         return true;
can be simplified to
Expand|Select|Wrap|Line Numbers
  1. if (tolower(col) >= 'a' && tolower(col) <= 'h' && row >= '1' && row <= '8')
  2.     return true;
Here is your problem

Expand|Select|Wrap|Line Numbers
  1. if (board[translateColumn(command[0])][command[1]] != "0")
You translate the first character of column from letters to integers but you do not translate the second character, you use it directly. However command[1] does not have the value in the range 0 - 7 which is what is required to be in range for the variable board, it has the range '1' - '8' which is decimal is 49 - 56, well out of range. You need a translateRow function in the same way you have a translate column.

This error appears multiple times in the function processCoord

and finally this
Expand|Select|Wrap|Line Numbers
  1. int translateColumn(char letter)
  2. {
  3. int col;
  4.  
  5. if (letter == 'a' || letter == 'A')
  6. col = 1;
  7. if (letter == 'b' || letter == 'B')
  8. col = 2;
  9. if (letter == 'c' || letter == 'C')
  10. col = 3;
  11. if (letter == 'd' || letter == 'D')
  12. col = 4;
  13. if (letter == 'e' || letter == 'E')
  14. col = 5;
  15. if (letter == 'f' || letter == 'F')
  16. col = 6;
  17. if (letter == 'g' || letter == 'G')
  18. col = 7;
  19. if (letter == 'h' || letter == 'H')
  20. col = 8;
  21.  
  22. return col;
  23. }
also has an error in it, it returns a value between 1 and 8 however this is used as an index into the board array. Since this array is declared string board[8][8] the index needs to be in the range 0 - 7. Additionally this could be written as
Expand|Select|Wrap|Line Numbers
  1. int translateColumn(char letter)
  2. {
  3.     return tolower(letter) - 'a';
  4. }
To use the function tolower you need to include the header ctype.h

Reply