1

First of all let me tell you, I've studied C and C++, but my knowledge of OOP is very limited. I basically want that as soon as I create an object to class output, my whole array outpt be initialized to blank spaces (char no. 32). MAXROWS and MAXCOLS are defined as const int, currently 25 and 80 but I may change them.

class output{
private:
int score;
char outpt[MAXROWS][MAXCOLS];

void rand_platform()
{
    int platform_start = rand() % (MAXCOLS-20);
    int platform_length = rand() % 10 + 10;
    for (int i=0; i<platform_length; i++) {
        outpt[MAXROWS-1][platform_start+i]=219;
    }
}

void bring_screen_down()
{                             //this part brings whole screen 1 row up
    score++;
    for (int i=1;i<MAXROWS;i++) {
        for (int j=0;j<MAXCOLS;j++) {
            outpt[i-1][j] = outpt[i][j];
        }
    }
    for (int j=0;j<MAXCOLS;j++) {
        outpt[MAXROWS-1][j] = 0;
    }
    if (!(score%10))
        rand_platform();
}

public:
void print()
{
    system("CLS");
    for (int i=0; i<MAXCOLS/2-2;i++)
        cout << ' ';
    printf("%04d\n",score);
    for (int i=0;i<MAXROWS;i++) {
        for (int j=0;j<MAXCOLS;j++) {
            cout << outpt[i][j];
        }
    cout << endl;
    }
bring_screen_down();
Sleep(200);   // alternately    for(int i=0; i<3500000;i++);
}

void output()
{
    score=0;
    fill_n(outpt, num_space_req, ' ');
}
};
  • this is the error message – some_nerdy_guy Mar 27 '16 at 05:51
  • 66 C:\Users\gm pd\Desktop\C++\Game2.cpp `num_space_req' undeclared (first use this function) – some_nerdy_guy Mar 27 '16 at 05:51
  • I'd suggest make sure your code compiles first before posting it here asking for help. Then see below one possible way to initialize your entire 2d char[][] array to ' ' spaces. – jrsmolley Mar 27 '16 at 05:57
  • sorry yws my constructor was type void, type shouldnt have been mentioned. Its compiling when you correct that constructor and comment the fill statement. – some_nerdy_guy Mar 27 '16 at 06:01
  • and no, since i named my class output, i just wanted a different identifier for the char array, so i made it outpt intentionally – some_nerdy_guy Mar 27 '16 at 06:02
  • The function 'void output()' at the bottom... is it supposed to be your constructor? If so, remove the 'void' before it, to avoid confusion. – jrsmolley Mar 27 '16 at 06:05

2 Answers2

1
class output {
    //...
    output() {
        memset( &outpt[0][0], ' ', sizeof(char) * MAXROWS * MAXCOLS );
    }
}

This will set (MAXROWS * MAXCOLS) chars, starting at the address of output[0][0], to the value of ' ' (32). Since you're setting every element to the same value, this is a quick way to do it.

You can clear the array in your constructor, for starters.

jrsmolley
  • 399
  • 1
  • 7
  • why not use `std::fill_n` instead of `memset` ? – Maikel Mar 27 '16 at 08:14
  • Is it guaranteed that `char outpt[MAXROWS][MAXCOLS];` is a continuous block of memory (I don't know)? If we allocated an array like this dynamically, it wouldn't be. And actually, if I write `output[0]`, I should get a pointer, not a space, shouldn't I? – FreeNickname Mar 27 '16 at 08:49
  • That should be `memset(..., sizeof outpt)`. Why compute the size of something if you can use `sizeof` instead? Especially, if there's the risk of errors when calculating it... That said, `sizeof (char)` is exactly 1, by definition, since `sizeof` gives you the size in multiples of the size of a `char`. – Ulrich Eckhardt Mar 27 '16 at 08:54
  • @UlrichEckhardt I didn't know if that was always true, but I see you're right. Looked at http://stackoverflow.com/questions/2215445/are-there-machines-where-sizeofchar-1-or-at-least-char-bit-8 – jrsmolley Mar 28 '16 at 03:39
  • Clearly I should've paid more attention to the "C++" tag, not "C" :) At any rate `sizeof outpt` will not return the size of the array if it is dynamically allocated with either `malloc()` or `new`. I agree with the other posters here though: a boost::multi_array would be my choice for C++; it is a bit involved though. – jrsmolley Mar 28 '16 at 03:43
1

To initialze a class you have to use a so called constructor function. For example like this

#include <cassert>
#include <algorithm>

const std::ptrdiff_t max_rows;
const std::ptrdiff_t max_cols;

class output {
private:
    char data_[max_rows][max_cols];
public:
    output()
    {
        assert(max_rows > 0 && max_cols > 0);
        std::fill_n(&data_[0][0], max_rows*max_cols, ' ');
    }

    void print() { /* your output */ }
};

For this dimension size handling, it would be even better to use a boost::multi_array container or a std::array. These C-style arrays are very error-prone. Even this can be more safe

class output {
private:
    std::array<char,max_rows*max_cols> data_;
public:
    output()
    {
        for (char& x : data_)  // can not be out of bounds
            x = ' ';
        // std::fill(data_.begin(), data_.end(), ' ');
    }

    void print() { /* your output */ }
};
Maikel
  • 1,204
  • 7
  • 19
  • Is it guaranteed that `char outpt[MAXROWS][MAXCOLS];` is a continuous block of memory (I don't know)? If we allocated an array like this dynamically, it wouldn't be. And actually, if I write `output[0]`, I should get a pointer, not a space, shouldn't I? – FreeNickname Mar 27 '16 at 08:50
  • Yes the memory is [guaranteed](http://stackoverflow.com/questions/7269099/may-i-treat-a-2d-array-as-a-contiguous-1d-array) to be contiguous. And yes, vector> might not be (but probably will be for small arrays). That is why one should use library containers for these things which will optimize memory layout. – Maikel Mar 27 '16 at 08:57
  • `vector>` would almost certainly not have contiguous container memory. But yeah I agree containers are a wiser choice for C++. – jrsmolley Mar 28 '16 at 04:18