Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
  • Try not to use using namespace std Try not to use using namespace std.

  • <stdlib.h> is a C library. Use <cstdlib> for C++.

  • These shouldn't be global:

     int xRan;
     int choicei = 12;
     int choicej = 12;
    

    As you're using xRan in another function, initialize it in main() only:

     int xRan = rand() % 15 + 1;
    

    and pass it to that function as an argument.

    I also don't see why choicei and choicej are initialized to 12. If there's a reason for that, there should be a comment specifying that. Since it looks needless, just declare them in main() closely in scope with the input:

     cout << "Enter The Row Number Less Than 10!" << endl;
     int choicei; // declare here
     cin >> choicei;
    
     cout << "Enter The Column Number Less Than 10!" << endl;
     int choicej; // declare here
     cin >> choicej;
    

    On another note, choicei and choicej are not good names. What are i and j? One would assume they have to do with simple loop counters, but this doesn't seem to be the case here. Consider something like rowNumberChoice and colNumberChoice.

  • I don't know how high your compiler warnings are turned up, but your std::srand() may produce warnings corresponding to "possible loss of data." This is usually remedied by casting the std::time() return to an unsigned int.

    If these warnings become present, call std::srand() like this:

     // if you're using C++11, use nullptr instead of NULL
     std::srand(static_cast<unsigned int>(std::time(NULL));
    
  • In C++, prefer to use standard containers over C-style arrays. You especially shouldn't pass one to a function as it would decay into a pointer. It's best to avoid this in C++.

    I recommend using std::array, if you're using C++11. As 2D, it would be initialized like this:

     std::array<std::array<char>, 10>, 10> gameBoard;
    

    As @nvuono mentioned, you could have row and column constants so that minimal updating is needed. You could then have this:

     const unsigned int rows = 10;
     const unsigned int cols = 12;
     std::array<std::array<char>, rows>, cols> gameBoard;
    

    To pass this 2D array to functions:

     // you may use a typedef to "rename" this type for less typing
     // name should be capitalized as it is a type
     typedef std::array<std::array<char>, rows>, cols> Board;
     // pass as const-ref as the board shouldn't be modified
     void displayBoard(Board const& gameBoard) { }
    
  • Try not to use using namespace std.

  • <stdlib.h> is a C library. Use <cstdlib> for C++.

  • These shouldn't be global:

     int xRan;
     int choicei = 12;
     int choicej = 12;
    

    As you're using xRan in another function, initialize it in main() only:

     int xRan = rand() % 15 + 1;
    

    and pass it to that function as an argument.

    I also don't see why choicei and choicej are initialized to 12. If there's a reason for that, there should be a comment specifying that. Since it looks needless, just declare them in main() closely in scope with the input:

     cout << "Enter The Row Number Less Than 10!" << endl;
     int choicei; // declare here
     cin >> choicei;
    
     cout << "Enter The Column Number Less Than 10!" << endl;
     int choicej; // declare here
     cin >> choicej;
    

    On another note, choicei and choicej are not good names. What are i and j? One would assume they have to do with simple loop counters, but this doesn't seem to be the case here. Consider something like rowNumberChoice and colNumberChoice.

  • I don't know how high your compiler warnings are turned up, but your std::srand() may produce warnings corresponding to "possible loss of data." This is usually remedied by casting the std::time() return to an unsigned int.

    If these warnings become present, call std::srand() like this:

     // if you're using C++11, use nullptr instead of NULL
     std::srand(static_cast<unsigned int>(std::time(NULL));
    
  • In C++, prefer to use standard containers over C-style arrays. You especially shouldn't pass one to a function as it would decay into a pointer. It's best to avoid this in C++.

    I recommend using std::array, if you're using C++11. As 2D, it would be initialized like this:

     std::array<std::array<char>, 10>, 10> gameBoard;
    

    As @nvuono mentioned, you could have row and column constants so that minimal updating is needed. You could then have this:

     const unsigned int rows = 10;
     const unsigned int cols = 12;
     std::array<std::array<char>, rows>, cols> gameBoard;
    

    To pass this 2D array to functions:

     // you may use a typedef to "rename" this type for less typing
     // name should be capitalized as it is a type
     typedef std::array<std::array<char>, rows>, cols> Board;
     // pass as const-ref as the board shouldn't be modified
     void displayBoard(Board const& gameBoard) { }
    
  • Try not to use using namespace std.

  • <stdlib.h> is a C library. Use <cstdlib> for C++.

  • These shouldn't be global:

     int xRan;
     int choicei = 12;
     int choicej = 12;
    

    As you're using xRan in another function, initialize it in main() only:

     int xRan = rand() % 15 + 1;
    

    and pass it to that function as an argument.

    I also don't see why choicei and choicej are initialized to 12. If there's a reason for that, there should be a comment specifying that. Since it looks needless, just declare them in main() closely in scope with the input:

     cout << "Enter The Row Number Less Than 10!" << endl;
     int choicei; // declare here
     cin >> choicei;
    
     cout << "Enter The Column Number Less Than 10!" << endl;
     int choicej; // declare here
     cin >> choicej;
    

    On another note, choicei and choicej are not good names. What are i and j? One would assume they have to do with simple loop counters, but this doesn't seem to be the case here. Consider something like rowNumberChoice and colNumberChoice.

  • I don't know how high your compiler warnings are turned up, but your std::srand() may produce warnings corresponding to "possible loss of data." This is usually remedied by casting the std::time() return to an unsigned int.

    If these warnings become present, call std::srand() like this:

     // if you're using C++11, use nullptr instead of NULL
     std::srand(static_cast<unsigned int>(std::time(NULL));
    
  • In C++, prefer to use standard containers over C-style arrays. You especially shouldn't pass one to a function as it would decay into a pointer. It's best to avoid this in C++.

    I recommend using std::array, if you're using C++11. As 2D, it would be initialized like this:

     std::array<std::array<char>, 10>, 10> gameBoard;
    

    As @nvuono mentioned, you could have row and column constants so that minimal updating is needed. You could then have this:

     const unsigned int rows = 10;
     const unsigned int cols = 12;
     std::array<std::array<char>, rows>, cols> gameBoard;
    

    To pass this 2D array to functions:

     // you may use a typedef to "rename" this type for less typing
     // name should be capitalized as it is a type
     typedef std::array<std::array<char>, rows>, cols> Board;
     // pass as const-ref as the board shouldn't be modified
     void displayBoard(Board const& gameBoard) { }
    
deleted 8 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
  • Try not to use using namespace std.

  • <stdlib.h> is a C library. Use <cstdlib> for C++.

  • These shouldn't be global:

     int xRan;
     int choicei = 12;
     int choicej = 12;
    

    As you're using xRan in another function, initialize it in main() only:

     int xRan = rand() % 15 + 1;
    

    and pass it to that function as an argument.

    I also don't see why choicei and choicej are initialized to 12. If there's a reason for that, there should be a comment specifying that. Since it looks needless, just declare them in main() closely in scope with the input:

     cout << "Enter The Row Number Less Than 10!" << endl;
     int choicei; // declare here
     cin >> choicei;
    
     cout << "Enter The Column Number Less Than 10!" << endl;
     int choicej; // declare here
     cin >> choicej;
    

    On another note, choicei and choicej are not good names. What are i and j? One would assume they have to do with simple loop counters, but this doesn't seem to be the case here. Consider something like rowNumberChoice and colNumberChoice.

  • I don't know how high your compiler warnings are turned up, but your std::srand() may produce warnings corresponding to possible data"possible loss of data." This is usually remedied by casting the std::time() return to an unsigned int.

    If these warnings become present, call std::srand() like this:

     // if you're using C++11, use nullptr instead of NULL
     std::srand(static_cast<unsigned int>(std::time(NULL));
    
  • In C++, prefer to use standard containers over C-style arrays. You especially shouldn't pass themone to functionsa function as thatit would deprecate them to pointersdecay into a pointer. It's best to avoid this in C++.

    I recommend using std::array, if you're using C++11. As 2D, it would be initialized like this:

     std::array<std::array<char>, 10>, 10> gameBoard;
    

    As @nvuono mentioned, you could have row and column constants so that minimal updating is needed. You could then have this:

     const unsigned int rows = 10;
     const unsigned int cols = 12;
     std::array<std::array<char>, rows>, cols> gameBoard;
    

    To pass this 2D array to functions:

     // you may use a typedef to "rename" this type for less typing
     // name should be capitalized as it is a type
     typedef std::array<std::array<char>, rows>, cols> Board;
     // pass as const-ref as the board shouldn't be modified
     void displayBoard(Board const& gameBoard) { }
    
  • Try not to use using namespace std.

  • <stdlib.h> is a C library. Use <cstdlib> for C++.

  • These shouldn't be global:

     int xRan;
     int choicei = 12;
     int choicej = 12;
    

    As you're using xRan in another function, initialize it in main() only:

     int xRan = rand() % 15 + 1;
    

    and pass it to that function as an argument.

    I also don't see why choicei and choicej are initialized to 12. If there's a reason for that, there should be a comment specifying that. Since it looks needless, just declare them in main() closely in scope with the input:

     cout << "Enter The Row Number Less Than 10!" << endl;
     int choicei; // declare here
     cin >> choicei;
    
     cout << "Enter The Column Number Less Than 10!" << endl;
     int choicej; // declare here
     cin >> choicej;
    

    On another note, choicei and choicej are not good names. What are i and j? One would assume they have to do with simple loop counters, but this doesn't seem to be the case. Consider something like rowNumberChoice and colNumberChoice.

  • I don't know how high your compiler warnings are turned up, but your std::srand() may produce warnings corresponding to possible data loss.

    If these warnings become present, call std::srand() like this:

     // if you're using C++11, use nullptr instead of NULL
     std::srand(static_cast<unsigned int>(std::time(NULL));
    
  • In C++, prefer to use standard containers over C-style arrays. You especially shouldn't pass them to functions as that would deprecate them to pointers. It's best to avoid this in C++.

    I recommend using std::array, if you're using C++11. As 2D, it would be initialized like this:

     std::array<std::array<char>, 10>, 10> gameBoard;
    

    As @nvuono mentioned, you could have row and column constants so that minimal updating is needed. You could then have this:

     const unsigned int rows = 10;
     const unsigned int cols = 12;
     std::array<std::array<char>, rows>, cols> gameBoard;
    

    To pass this 2D array to functions:

     // you may use a typedef to "rename" this type for less typing
     // name should be capitalized as it is a type
     typedef std::array<std::array<char>, rows>, cols> Board;
     // pass as const-ref as the board shouldn't be modified
     void displayBoard(Board const& gameBoard) { }
    
  • Try not to use using namespace std.

  • <stdlib.h> is a C library. Use <cstdlib> for C++.

  • These shouldn't be global:

     int xRan;
     int choicei = 12;
     int choicej = 12;
    

    As you're using xRan in another function, initialize it in main() only:

     int xRan = rand() % 15 + 1;
    

    and pass it to that function as an argument.

    I also don't see why choicei and choicej are initialized to 12. If there's a reason for that, there should be a comment specifying that. Since it looks needless, just declare them in main() closely in scope with the input:

     cout << "Enter The Row Number Less Than 10!" << endl;
     int choicei; // declare here
     cin >> choicei;
    
     cout << "Enter The Column Number Less Than 10!" << endl;
     int choicej; // declare here
     cin >> choicej;
    

    On another note, choicei and choicej are not good names. What are i and j? One would assume they have to do with simple loop counters, but this doesn't seem to be the case here. Consider something like rowNumberChoice and colNumberChoice.

  • I don't know how high your compiler warnings are turned up, but your std::srand() may produce warnings corresponding to "possible loss of data." This is usually remedied by casting the std::time() return to an unsigned int.

    If these warnings become present, call std::srand() like this:

     // if you're using C++11, use nullptr instead of NULL
     std::srand(static_cast<unsigned int>(std::time(NULL));
    
  • In C++, prefer to use standard containers over C-style arrays. You especially shouldn't pass one to a function as it would decay into a pointer. It's best to avoid this in C++.

    I recommend using std::array, if you're using C++11. As 2D, it would be initialized like this:

     std::array<std::array<char>, 10>, 10> gameBoard;
    

    As @nvuono mentioned, you could have row and column constants so that minimal updating is needed. You could then have this:

     const unsigned int rows = 10;
     const unsigned int cols = 12;
     std::array<std::array<char>, rows>, cols> gameBoard;
    

    To pass this 2D array to functions:

     // you may use a typedef to "rename" this type for less typing
     // name should be capitalized as it is a type
     typedef std::array<std::array<char>, rows>, cols> Board;
     // pass as const-ref as the board shouldn't be modified
     void displayBoard(Board const& gameBoard) { }
    
added 286 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
  • Try not to use using namespace std.

  • <stdlib.h> is a C library. Use <cstdlib> for C++.

  • These shouldn't be global:

     int xRan;
     int choicei = 12;
     int choicej = 12;
    

    As you're using xRan in another function, initialize it in main() only:

     int xRan = rand() % 15 + 1;
    

    and pass it to that function as an argument.

    I also don't see why choicei and choicej are initialized to 12. If there's a reason for that, there should be a comment specifying that. Since it looks needless, just declare them in main() closely in scope with the input:

     cout << "Enter The Row Number Less Than 10!" << endl;
     int choicei; // declare here
     cin >> choicei;
    
     cout << "Enter The Column Number Less Than 10!" << endl;
     int choicej; // declare here
     cin >> choicej;
    

    On another note, choicei and choicej are not good names. What are i and j? One would assume they have to do with simple loop counters, but this doesn't seem to be the case. Consider something like rowNumberChoice and colNumberChoice.

  • I don't know how high your compiler warnings are turned up, but your std::srand() may produce errors from the lack of casting. Justwarnings corresponding to be on the safe sidepossible data loss.

    If these warnings become present, call std::srand() like this:

     // if you're using C++11, use nullptr instead of NULL
     std::srand(static_cast<unsigned int>(std::time(NULL));
    
  • In C++, prefer to use standard containers over C-style arrays. You especially shouldn't pass them to functions as that would deprecate them to pointers. It's best to avoid this in C++.

    I recommend using std::array, if you're using C++11. As 2D, it would be initialized like this:

     std::array<std::array<char>, 10>, 10> gameBoard;
    

    As @nvuono mentioned, you could have row and column constants so that minimal updating is needed. You could then have this:

     const unsigned int rows = 10;
     const unsigned int cols = 12;
     std::array<std::array<char>, rows>, cols> gameBoard;
    

    To pass this 2D array to functions:

     // you may use a typedef to "rename" this type for less typing
     // name should be capitalized as it is a type
     typedef std::array<std::array<char>, rows>, cols> Board;
     // pass as const-ref as the board shouldn't be modified
     void displayBoard(Board const& gameBoard) { }
    
  • Try not to use using namespace std.

  • <stdlib.h> is a C library. Use <cstdlib> for C++.

  • These shouldn't be global:

     int xRan;
     int choicei = 12;
     int choicej = 12;
    

    As you're using xRan in another function, initialize it in main() and pass it to that function as an argument.

    I also don't see why choicei and choicej are initialized to 12. If there's a reason for that, there should be a comment specifying that. Since it looks needless, just declare them in main() closely in scope with the input:

     cout << "Enter The Row Number Less Than 10!" << endl;
     int choicei; // declare here
     cin >> choicei;
    
     cout << "Enter The Column Number Less Than 10!" << endl;
     int choicej; // declare here
     cin >> choicej;
    
  • I don't know how high your warnings are turned up, but your std::srand() may produce errors from the lack of casting. Just to be on the safe side, call std::srand() like this:

     // if you're using C++11, use nullptr instead of NULL
     std::srand(static_cast<unsigned int>(std::time(NULL));
    
  • In C++, prefer to use standard containers over C-style arrays. You especially shouldn't pass them to functions as that would deprecate them to pointers. It's best to avoid this in C++.

    I recommend using std::array, if you're using C++11. As 2D, it would be initialized like this:

     std::array<std::array<char>, 10>, 10> gameBoard;
    

    As @nvuono mentioned, you could have row and column constants so that minimal updating is needed. You could then have this:

     const unsigned int rows = 10;
     const unsigned int cols = 12;
     std::array<std::array<char>, rows>, cols> gameBoard;
    

    To pass this 2D array to functions:

     // you may use a typedef to "rename" this type for less typing
     // name should be capitalized as it is a type
     typedef std::array<std::array<char>, rows>, cols> Board;
     // pass as const-ref as the board shouldn't be modified
     void displayBoard(Board const& gameBoard) { }
    
  • Try not to use using namespace std.

  • <stdlib.h> is a C library. Use <cstdlib> for C++.

  • These shouldn't be global:

     int xRan;
     int choicei = 12;
     int choicej = 12;
    

    As you're using xRan in another function, initialize it in main() only:

     int xRan = rand() % 15 + 1;
    

    and pass it to that function as an argument.

    I also don't see why choicei and choicej are initialized to 12. If there's a reason for that, there should be a comment specifying that. Since it looks needless, just declare them in main() closely in scope with the input:

     cout << "Enter The Row Number Less Than 10!" << endl;
     int choicei; // declare here
     cin >> choicei;
    
     cout << "Enter The Column Number Less Than 10!" << endl;
     int choicej; // declare here
     cin >> choicej;
    

    On another note, choicei and choicej are not good names. What are i and j? One would assume they have to do with simple loop counters, but this doesn't seem to be the case. Consider something like rowNumberChoice and colNumberChoice.

  • I don't know how high your compiler warnings are turned up, but your std::srand() may produce warnings corresponding to possible data loss.

    If these warnings become present, call std::srand() like this:

     // if you're using C++11, use nullptr instead of NULL
     std::srand(static_cast<unsigned int>(std::time(NULL));
    
  • In C++, prefer to use standard containers over C-style arrays. You especially shouldn't pass them to functions as that would deprecate them to pointers. It's best to avoid this in C++.

    I recommend using std::array, if you're using C++11. As 2D, it would be initialized like this:

     std::array<std::array<char>, 10>, 10> gameBoard;
    

    As @nvuono mentioned, you could have row and column constants so that minimal updating is needed. You could then have this:

     const unsigned int rows = 10;
     const unsigned int cols = 12;
     std::array<std::array<char>, rows>, cols> gameBoard;
    

    To pass this 2D array to functions:

     // you may use a typedef to "rename" this type for less typing
     // name should be capitalized as it is a type
     typedef std::array<std::array<char>, rows>, cols> Board;
     // pass as const-ref as the board shouldn't be modified
     void displayBoard(Board const& gameBoard) { }
    
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /