Thread: Jumping into c++ chapter 14 problem 5 (Collect 4)

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    11

    Jumping into c++ chapter 14 problem 5 (Collect 4)

    Hi guys I m trying to do a couple of functions which enable to get a board of a certain width and length. Once the user choose how much broad the board will be, I want to make appear on each empty slot this "-", by using system ("cls") (because is the only way I ve learnt so far).
    This is the code I ve written:

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    void DisplayBoard (char **p_p_board, int length, int width);
    
    void Board (char **p_p_board, int length, int width)
    {
      p_p_board = new char* [length];
      for (int i=0; i<length; i++)
      {
         p_p_board [i] = new char [width];
      }
      DisplayBoard (p_p_board,  length,  width);
    }
    
    void DisplayBoard (char **p_p_board, int length, int width)
    {
        cout<<"COLLECT FOUR"<<endl;
        system("cls");
        for (int x=1; x<length+1; x++)
        {
            cout<<x<<" ";
        }
        cout<<endl;
        for (int i =0; i<length; i++)
        {
            cout<<endl;
            for(int j=0; j<width; j++)
            {
               cout<< p_p_board [i][j] = "-"<< " "; // error
            }
        }
    }
    I know it doesn't give anything yet, cause I haven t put main, but when I try to build it it tells me:
    "|30|error: invalid operands of types 'const char [2]' and 'const char [2]' to binary 'operator<<'"
    This is referred to the line of code where I added the comment "//error"
    Could anyone tell me what to change?
    (P.S. this chapter is supposed to treat multidimentional arrays and pointers to pointers)
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,621
    Perhaps start with.
    cout<< p_p_board [i][j];
    Then add to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,101
    It's called "connect 4" (not "collect").

    The assignment:
    Write a two-player game of "connect 4" where the user can set the width and height of the board and each player gets a turn to drop a token into the slot. Display the board using + for one side, x for the other, and _ to indicate blank spaces.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    using namespace std;
     
    void DisplayBoard(char **p_p_board, int length, int width);
     
    char **Board(int length, int width) {
        char **p_p_board = new char*[length];
        for (int i = 0; i < length; i++) {
            p_p_board[i] = new char[width];
            for (int j = 0; j < width; j++)
                p_p_board[i][j] = '_';
        }
        return p_p_board;
    }
    
    void DisplayBoard(char **p_p_board, int length, int width) {
        //system("cls");
        cout << setw(3*width/2+7) << "CONNECT FOUR\n";
        for (int x = 1; x <= width; x++)
            cout << setw(2) << x << ' ';
        for (int i = 0; i < length; i++) {
            cout << "\n ";
            for (int j = 0; j < width; j++)
               cout << p_p_board[i][j] << "  ";
        }
        cout << '\n';
    }
    
    int main() {
        int length = 0, width = 0;
        cout << "Length: ";
        cin >> length;
        cout << "Width: ";
        cin >> width;
    
        char **p_p_board = Board(length, width);
    
        DisplayBoard(p_p_board, length, width);
    
        for (int i = 0; i < length; i++)
            delete [] p_p_board[i];
        delete [] p_p_board;
    }
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  4. #4
    Registered User
    Join Date
    May 2018
    Posts
    11
    Thanks a lot, i just didnt get wht setw(2) mean (you ve written it at the 20th and 22nd line)… I ve probably not arrived so far in my book yet. could you explain me what it is please?

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,101
    setw(n) sets the "width" of the next field to n.
    So
    Code:
    cout << setw(3*width/2+7) << "CONNECT FOUR\n";
    sets the field width to what is required to center the title.

    And
    Code:
    cout << setw(2) << x << ' ';
    sets the field width to 2 so that single digit numbers will be printed with a space in front of them.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 07-09-2016, 08:13 AM
  2. Jumping Into C++ Chapter 14 Problem 4
    By mbartholomew in forum C++ Programming
    Replies: 3
    Last Post: 12-28-2014, 03:10 PM
  3. jumping into c++ chapter 5 problem 7
    By etricity in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2014, 11:23 PM
  4. Jumping Into C++ - Chapter 8 Problem[3]
    By Mohamed Adel in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2013, 09:14 AM
  5. Jumping To C++ - Chapter 8 Problem
    By Mohamed Adel in forum C++ Programming
    Replies: 4
    Last Post: 08-27-2013, 01:02 PM

Tags for this Thread