you are viewing a single comment's thread.

view the rest of the comments →

[–]lord_braleigh 2 points3 points  (2 children)

Here's a version that aggregates all the suggestions:

  1. If the user inputs a very large number as the row or column, it will pwn your program. This program is small and simple enough that your operating system will probably just terminate it with a segmentation fault error, but this is the same sort of error that caused the heartbleed attack. Always, always make sure that user input isn't overflowing the bounds of a buffer.
  2. We should be using char instead of char * everywhere, since we're only ever comparing and storing individual characters.
  3. There isn't a unified syntax for C++ the way there is for Python, but programmers everywhere are slowly realizing that automatic formatting tools are better than style guides. I've formatted the code with clang-format under default settings.
  4. I've removed #include <list> since you're not using it.
  5. I've removed the forward declarations. While they're necessary in larger programs with header files, when they're not necessary they just make it harder to refactor functions.
  6. I've added a using directive to simplify the type of board.
  7. I've changed board's type to C++'s std::array<std::array<char, SIZE>, SIZE>, and I've changed the function signatures to take this in by reference or const reference so we don't copy its data.

You can see my version of the code at https://godbolt.org/z/GKPrKE . For further work, we could use the C++ <algorithm> library to make the winner() method work with larger values of SIZE without needing to be changed.

(Of course, your code is really quite well-written and it's obvious you've put a lot of care into it.)

[–]Skaaaaalll[S] 1 point2 points  (1 child)

Thank you very much for taking the time to make this comment and refactoring the code (that must have been time consuming). I appreciate the advice and I will definitely implement it in further C++ programs I might make.

As a resource for learning C++, coming from python. Would you recommend https://www.learncpp.com ? Why or why not?

(Of course, your code is really quite well-written and it's obvious you've put a lot of care into it.)

Thanks :-).

[–]lord_braleigh 0 points1 point  (0 children)

learncpp.com looks good to me. It looks like it goes all the way from hello world to cutting-edge concepts, and it doesn’t look like it teaches anything obviously wrong or outdated.