fork download
  1. #include <iostream>
  2. //#include <conio.h>
  3.  
  4. char square[10] = ["0","1","2","3","4","5","6","7","8","9"];
  5. int checkwin();
  6. void board();
  7.  
  8. int main()
  9. {
  10. int player = 1,i,choice;
  11. char mark;
  12. clrscr();
  13. do
  14. {
  15. board();
  16. player=(player%2)?1:2;
  17. cout << "Player " << player << ", enter a number: ";
  18. cin >> choice;
  19. mark=(player == 1) ? "X" : "O";
  20. if (choice == 1 && square[1] == "1")
  21. square[1] = mark;
  22. else if (choice == 2 && square[2] == "2")
  23. square[2] = mark;
  24. else if (choice == 3 && square[3] == "3")
  25. square[3] = mark;
  26. else if (choice == 4 && square[4] == "4")
  27. square[4] = mark;
  28. else if (choice == 5 && square[5] == "5")
  29. square[5] = mark;
  30. else if (choice == 6 && square[6] == "6")
  31. square[6] = mark;
  32. else if (choice == 7 && square[7] == "7")
  33. square[7] = mark;
  34. else if (choice == 8 && square[8] == "8")
  35. square[8] = mark;
  36. else if (choice == 9 && square[9] == "9")
  37. square[9] = mark;
  38. else
  39. {
  40. cout<<"Invalid move ";
  41. player--;
  42. getch();
  43. }
  44. i=checkwin();
  45. player++;
  46. }while(i==-1);
  47. board();
  48. if(i==1)
  49. cout<<"==>\aPlayer "<<--player<<" win ";
  50. else
  51. cout<<"==>\aGame draw";
  52. getch();
  53. return 0;
  54. }
  55. /*********************************************
  56. FUNCTION TO RETURN GAME STATUS
  57. 1 FOR GAME IS OVER WITH RESULT
  58. -1 FOR GAME IS IN PROGRESS
  59. O GAME IS OVER AND NO RESULT
  60. **********************************************/
  61.  
  62. int checkwin()
  63. {
  64. if (square[1] == square[2] && square[2] == square[3])
  65. return 1;
  66. else if (square[4] == square[5] && square[5] == square[6])
  67. return 1;
  68. else if (square[7] == square[8] && square[8] == square[9])
  69. return 1;
  70. else if (square[1] == square[4] && square[4] == square[7])
  71. return 1;
  72. else if (square[2] == square[5] && square[5] == square[8])
  73. return 1;
  74. else if (square[3] == square[6] && square[6] == square[9])
  75. return 1;
  76. else if (square[1] == square[5] && square[5] == square[9])
  77. return 1;
  78. else if (square[3] == square[5] && square[5] == square[7])
  79. return 1;
  80. else if (square[1] != "1" && square[2] != "2" && square[3] != "3" &&
  81. square[4] != "4" && square[5] != "5" && square[6] != "6" &&
  82. square[7] != "7" && square[8] != "8" && square[9] != "9")
  83. return 0;
  84. else
  85. return -1;
  86. }
  87.  
  88.  
  89. /*******************************************************************
  90.   FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
  91. ********************************************************************/
  92.  
  93.  
  94. void board()
  95. {
  96. clrscr();
  97. cout << "\n\n\tTic Tac Toe\n\n";
  98. cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
  99. cout << endl;
  100. cout << " | | " << endl;
  101. cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
  102. cout << "_____|_____|_____" << endl;
  103. cout << " | | " << endl;
  104. cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
  105. cout << "_____|_____|_____" << endl;
  106. cout << " | | " << endl;
  107. cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
  108. cout << " | | " << endl << endl;
  109. }
  110.  
  111. /*******************************************************************
  112. END OF PROJECT
  113. ********************************************************************/
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:20: error: expected identifier before string constant
 char square[10] = ["0","1","2","3","4","5","6","7","8","9"];
                    ^
prog.cpp:4:23: error: expected ‘]’ before ‘,’ token
 char square[10] = ["0","1","2","3","4","5","6","7","8","9"];
                       ^
prog.cpp:4:23: error: type ‘__lambda0’ with no linkage used to declare function ‘auto __lambda0::operator()() const’ with linkage [-fpermissive]
prog.cpp: In lambda function:
prog.cpp:4:23: error: expected ‘{’ before ‘,’ token
prog.cpp: At global scope:
prog.cpp:4:23: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
prog.cpp:4:23: error: array must be initialized with a brace-enclosed initializer
prog.cpp:4:24: error: expected unqualified-id before string constant
 char square[10] = ["0","1","2","3","4","5","6","7","8","9"];
                        ^
prog.cpp: In function ‘int main()’:
prog.cpp:12:9: error: ‘clrscr’ was not declared in this scope
  clrscr();
         ^
prog.cpp:17:3: error: ‘cout’ was not declared in this scope
   cout << "Player " << player << ", enter a number:  ";
   ^
prog.cpp:17:3: note: suggested alternative:
In file included from prog.cpp:1:0:
/usr/include/c++/4.8/iostream:61:18: note:   ‘std::cout’
   extern ostream cout;  /// Linked to standard output
                  ^
prog.cpp:18:3: error: ‘cin’ was not declared in this scope
   cin >> choice;
   ^
prog.cpp:18:3: note: suggested alternative:
In file included from prog.cpp:1:0:
/usr/include/c++/4.8/iostream:60:18: note:   ‘std::cin’
   extern istream cin;  /// Linked to standard input
                  ^
prog.cpp:19:7: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
   mark=(player == 1) ? "X" : "O";
       ^
prog.cpp:20:35: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   if (choice == 1 && square[1] == "1")
                                   ^
prog.cpp:20:35: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:22:40: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   else if (choice == 2 && square[2] == "2")
                                        ^
prog.cpp:22:40: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:24:40: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   else if (choice == 3 && square[3] == "3")
                                        ^
prog.cpp:24:40: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:26:40: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   else if (choice == 4 && square[4] == "4")
                                        ^
prog.cpp:26:40: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:28:40: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   else if (choice == 5 && square[5] == "5")
                                        ^
prog.cpp:28:40: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:30:40: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   else if (choice == 6 && square[6] == "6")
                                        ^
prog.cpp:30:40: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:32:40: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   else if (choice == 7 && square[7] == "7")
                                        ^
prog.cpp:32:40: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:34:40: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   else if (choice == 8 && square[8] == "8")
                                        ^
prog.cpp:34:40: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:36:40: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   else if (choice == 9 && square[9] == "9")
                                        ^
prog.cpp:36:40: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:42:10: error: ‘getch’ was not declared in this scope
    getch();
          ^
prog.cpp:49:3: error: ‘cout’ was not declared in this scope
   cout<<"==>\aPlayer "<<--player<<" win ";
   ^
prog.cpp:49:3: note: suggested alternative:
In file included from prog.cpp:1:0:
/usr/include/c++/4.8/iostream:61:18: note:   ‘std::cout’
   extern ostream cout;  /// Linked to standard output
                  ^
prog.cpp:51:3: error: ‘cout’ was not declared in this scope
   cout<<"==>\aGame draw";
   ^
prog.cpp:51:3: note: suggested alternative:
In file included from prog.cpp:1:0:
/usr/include/c++/4.8/iostream:61:18: note:   ‘std::cout’
   extern ostream cout;  /// Linked to standard output
                  ^
prog.cpp:52:8: error: ‘getch’ was not declared in this scope
  getch();
        ^
prog.cpp: In function ‘int checkwin()’:
prog.cpp:80:24: warning: comparison with string literal results in unspecified behaviour [-Waddress]
  else if (square[1] != "1" && square[2] != "2" && square[3] != "3" &&
                        ^
prog.cpp:80:24: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:80:44: warning: comparison with string literal results in unspecified behaviour [-Waddress]
  else if (square[1] != "1" && square[2] != "2" && square[3] != "3" &&
                                            ^
prog.cpp:80:44: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:80:64: warning: comparison with string literal results in unspecified behaviour [-Waddress]
  else if (square[1] != "1" && square[2] != "2" && square[3] != "3" &&
                                                                ^
prog.cpp:80:64: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:81:24: warning: comparison with string literal results in unspecified behaviour [-Waddress]
           square[4] != "4" && square[5] != "5" && square[6] != "6" &&
                        ^
prog.cpp:81:24: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:81:44: warning: comparison with string literal results in unspecified behaviour [-Waddress]
           square[4] != "4" && square[5] != "5" && square[6] != "6" &&
                                            ^
prog.cpp:81:44: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:81:64: warning: comparison with string literal results in unspecified behaviour [-Waddress]
           square[4] != "4" && square[5] != "5" && square[6] != "6" &&
                                                                ^
prog.cpp:81:64: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:82:26: warning: comparison with string literal results in unspecified behaviour [-Waddress]
             square[7] != "7" && square[8] != "8" && square[9] != "9")
                          ^
prog.cpp:82:26: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:82:46: warning: comparison with string literal results in unspecified behaviour [-Waddress]
             square[7] != "7" && square[8] != "8" && square[9] != "9")
                                              ^
prog.cpp:82:46: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:82:66: warning: comparison with string literal results in unspecified behaviour [-Waddress]
             square[7] != "7" && square[8] != "8" && square[9] != "9")
                                                                  ^
prog.cpp:82:66: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp: In function ‘void board()’:
prog.cpp:96:9: error: ‘clrscr’ was not declared in this scope
  clrscr();
         ^
prog.cpp:97:2: error: ‘cout’ was not declared in this scope
  cout << "\n\n\tTic Tac Toe\n\n";
  ^
prog.cpp:97:2: note: suggested alternative:
In file included from prog.cpp:1:0:
/usr/include/c++/4.8/iostream:61:18: note:   ‘std::cout’
   extern ostream cout;  /// Linked to standard output
                  ^
prog.cpp:98:45: error: ‘endl’ was not declared in this scope
  cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
                                             ^
prog.cpp:98:45: note: suggested alternative:
In file included from /usr/include/c++/4.8/iostream:39:0,
                 from prog.cpp:1:
/usr/include/c++/4.8/ostream:564:5: note:   ‘std::endl’
     endl(basic_ostream<_CharT, _Traits>& __os)
     ^
stdout
Standard output is empty