Getting happy face value instead of O for tic tac toe prog.

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Getting happy face value instead of O for tic tac toe prog.

 
0
  #1
Dec 8th, 2008
Yes, I have the do while set to go on forever until I get on to the solving part, but while trying to shrink my program down so I can make it more modular, the O's that player 2 have are showing up as a face?

I know I have a logic error with the player 1 and 2 alternating, I'll get that fixed, but first I want to know why my O's are showning up as faces?



  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. const gRow = 3;
  6. const gCol = 3;
  7.  
  8. void showGrid(char grid[][gCol], int);
  9. int changeValue(int, int);
  10.  
  11. int main()
  12. { char grid[gRow][gCol] = {{'*', '*', '*'},
  13. {'*', '*', '*'},
  14. {'*', '*', '*'}};
  15. int rows, cols;
  16. int el = 1;
  17. int turn = 1;
  18. int num = 1;
  19. char player;
  20.  
  21. cout << " TIC TAC TOE" << endl;
  22. cout << "Player One: X Player Two: O\n" << endl;
  23.  
  24. do {
  25.  
  26. showGrid(grid, gRow);
  27.  
  28. if (turn % 2)
  29. {player = 'X';}
  30. else {player = 'O' && num++;}
  31.  
  32. cout << "Player " << num << ", Enter the row and column:";
  33. cin >> rows >> cols;
  34. if (grid[rows-1][cols-1] != '*')
  35. {cout << "This space is taken... Enter the row and column:\n";
  36. cin >> rows >> cols; }
  37.  
  38. grid[rows - 1][cols - 1] = player;
  39.  
  40. turn++;
  41.  
  42. }
  43. while(el = 1);
  44.  
  45.  
  46. return 0;
  47.  
  48. }
  49.  
  50.  
  51. void showGrid(char array[][gCol], int numRows)
  52. {
  53. for (int row = 0; row < numRows; row++)
  54. { for (int col = 0; col < gCol; col++)
  55. {
  56. cout << setw(2) << array[row][col] << " ";
  57. }
  58. cout << endl;
  59. }
  60. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: Getting happy face value instead of O for tic tac toe prog.

 
0
  #2
Dec 8th, 2008
Hmm not exactly sure what that would be happening, but there are still a couple things you need to change. At the top you should declare gRow and gCol as const int - I'm not sure how this even compiles with no type given. Also you should make a loop for getting input rather than an if statement in case the player tries to place a piece in an invalid spot more than once. Another thing to consider is making sure the player enters a valid row and column (between 1 and 3).
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 397
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Getting happy face value instead of O for tic tac toe prog.

 
1
  #3
Dec 8th, 2008
Sorry but your errors include :
else {player = 'O' && num++;}
And that is equivilent to
  1.  
  2. player = ('O' && num);
  3. num++;

That means that player == 1 or 0, and both of those are interesting ascii characters to print

hope that helps.

Overall, laying out the code cleanly will greatly reduce errors like this.
Last edited by StuXYZ; Dec 8th, 2008 at 5:19 pm.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 302 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC