473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

trouble with c++ while loop ?

2
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;
}
May 2 '06 #1
2 6980
Banfa
9,065 Expert Mod 8TB
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
May 3 '06 #2
apec
2
Thank you, Banfa, for your help :D

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
May 18 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Alistair | last post by:
Hi there, New to PHP. I have always used ASP. I am having trouble trying to do the things in PHP the way (or similar) i did it in ASP. In ASP I used an Access database. I now use a mySQL...
9
by: Alexander Stippler | last post by:
Hi, I've got trouble with some well known issue. Iterator invalidation. My situation: for (it=v.begin(); it!=v.end(); ++it) { f(*it); } Under some circumstances, f may alter the container...
6
by: rh0dium | last post by:
Hi all, Basically I have a bunch of pluggins in a directory (METDIR). For each one of these templated pluggins I want to do a specific routine. Let's start with a basic template file...
1
by: ferraro.joseph | last post by:
Hi, I'm querying Salesforce.com via their AJAX toolkit and outputting query results into a table. Currently, their toolkit does not possess the ability to do table joins via their structured...
1
by: teddarr | last post by:
I'm having trouble reading the first 2 lines of data in an external file. I am supposed to use a while loop to read the first 2 lines of an external file that contains several random integers. I...
10
by: Hendri Adriaens | last post by:
Hi, I'm trying to automate the creation of an excel file via COM. I copied my code below. I read many articles about how to release the COM objects that I create. The code below runs just fine...
9
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return...
0
by: bmerlover | last post by:
This code makes sense to me, I'm just having trouble trying to understand why it doesn't work correctly. This is a GUI APP. When the Play button is Clicked, the play_Click(System::Object * sender,...
11
by: inihility | last post by:
This is actaully a really simple recursion (for-loop), but I'm having some trouble optimizing it so that it would run faster. for (int i = 0; i < 50; i++) { a = getNumber(i); } Right now it...
2
by: msridhar87 | last post by:
hi, am doing a multi server chat program using select()..in the client side i need to multiplex thei/p from server and stdin. #include <stdio.h> #include <sys/types.h> #include <sys/socket.h>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.