943,832 Members | Top Members by Rank

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

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

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



C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Foe89 is offline Offline
22 posts
since Oct 2008
Dec 8th, 2008
0

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

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).
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Dec 8th, 2008
1

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

Sorry but your errors include :
else {player = 'O' && num++;}
And that is equivilent to
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 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: Parsing a char
Next Thread in C++ Forum Timeline: USB Dismount not turning off my USB Drive?





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


Follow us on Twitter


© 2011 DaniWeb® LLC