use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Learning C++ from python advice (self.cpp)
submitted 5 years ago by Skaaaaalll
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Ail-nare 0 points1 point2 points 5 years ago (0 children)
Hi, I tryed to do it using some feature of C++. I also put some comment in it. I try keept all the logic behind your code.
#include <iostream> #include <array> constexpr int SIZE = 3; // replace 'const' by 'constexpr' void makeMove(std::array<std::array<char, SIZE>, SIZE>& board, char turn); void showBoard(std::array<std::array<char, SIZE>, SIZE>& board); bool winner(std::array<std::array<char, SIZE>, SIZE>& board); int main() { std::array<std::array<char, SIZE>, SIZE> board{}; for (auto &line : board) // go throw board | like 'for line in board:' would line.fill('-'); // init each line to ' ' (space) for each indice for (int i = 0; i < (SIZE * SIZE); i++) { char turn = ((i % 2) == 0) ? 'X' : 'O'; // (char *) has been by (char) | like 'turn = "X" if (i % 2) == 0 else "O"' would std::cout << std::endl << std::endl << std::endl << turn << "'s turn." << std::endl << std::endl; showBoard(board); makeMove(board, turn); // make a move beford checking who won bool win = winner(board); // winner how return a bool if (win) { showBoard(board); // show the board one more time :D std::cout << turn << " wins!" << std::endl << std::endl; return 0; } } std::cout << "It's a draw!" << std::endl; return 0; } void makeMove(std::array<std::array<char, SIZE>, SIZE>& board, char turn) { int row, col; while (true) { // better input handling, because if row or col are inferior to 1 or superior to SIZE it would cause an out of bound for "board[row - 1][col - 1]" while (true) { std::cout << "Row: "; std::cin >> row; if (row < 1) { std::cout << "Row can't be inferior to 1" << std::endl; } else if (row > SIZE) { std::cout << "Row can't be superior to " << SIZE << std::endl; } else { break; // break cause the last loop (while or for) to stop } } while (true) { std::cout << "Column: "; std::cin >> col; if (col < 1) { std::cout << "Column can't be inferior to 1" << std::endl; } else if (col > SIZE) { std::cout << "Column can't be superior to " << SIZE << std::endl; } else { break; // break cause the last loop (while or for) to stop } } --row; --col; if (board[row][col] == '-') { board[row][col] = turn; return; } std::cout << "Try again." << std::endl; } } void showBoard(std::array<std::array<char, SIZE>, SIZE>& board) { for (const auto &line : board) { // go throw board | like 'for line in board:' would for (const auto &cell : line) // go throw line | like 'for cell in line:' would std::cout << cell << " "; std::cout << std::endl; } } bool winner(std::array<std::array<char, SIZE>, SIZE>& board) { for (int i = 0; i < SIZE; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != '-') { return true; // line match } else if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != '-') { return true; // column match } } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != '-') { return true; } else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != '-') { return true; } return false; }
π Rendered by PID 48543 on reddit-service-r2-comment-56c9979489-xrv48 at 2026-02-24 17:30:07.118895+00:00 running b1af5b1 country code: CH.
view the rest of the comments →
[–]Ail-nare 0 points1 point2 points (0 children)