These are my instructions:

- create a class containing a 10x10 two-dimensional array of characters.

- class must have a constructor that puts the '.' character in each element.

- In main, I must allow the user to enter an 'X' into six different spaces in the 2D array.

(we're eventually making a simple minesweeper game, but this is the first assignment)

I have no idea how to set up a 2D array in a class. I've attempted different things, but I'm not sure where in the constructor I would initialize the array or how it is suppose to look. I don't even know if the private variables are suppose to be an array or values for an array.

Are there any examples of this type of set up? We haven't learned pointers yet, so I need a simple explanation. Here is my code so far:

sweeper.h

class Sweeper
{
public:

    const static int row=10; 
    const static int col=10;
    
    Sweeper(char b); //constructors
    Sweeper();
    
    void set(char b); //mutators
    void set_box(char b);
    
    char get_box() const; //accessors
    
    friend ostream& operator<<(ostream& os, const Sweeper& aSweeper);

private:
    char m_box[row][col];
};

sweeper.cpp

Sweeper::Sweeper(char b) //constructor
{
    set(b);
}
Sweeper::Sweeper() //default constructor
{
    set('.');
}
   
void Sweeper::set(char b) //mutators
{
    set_box(b);
}

void Sweeper::set_box(char box)
{
    m_box[row][col] = box;
}

char Sweeper::get_box() const //accessors
{
    return(m_box[row][col]);
}

I'm a bit lost. I'm using a model of a class that the professor gave us, but it's not an array.

Nick Evan commented: good first post +25
VernonDozier commented: Code tags on first post. +23

Presumably you want something like this for your constructors:

Sweeper::Sweeper() //default constructor
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
            m_box[i][j] = '.';
    }
}


Sweeper::Sweeper(char b) //constructor
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
            m_box[i][j] = 'b';
    }
}

You might want to change row to NUMROWS and col to NUMCOLS to be a little more descriptive.

- In main, I must allow the user to enter an 'X' into six different spaces in the 2D array.

Presumably that means you need to have a set function that specifies a row and column number (another reason to change your constant variable names to NUMROWS and NUMCOLS. "row" can be confusing and the reader would expect it to vary). Or you may be calling it "set_box"? What's the difference between set and set_box? Is one supposed to set all the boxes and one supposed to set a single box?

Change "set" below to "set_box" if that's what you want.

void Sweeper::set (char b, int rowNum, int colNum)
{
    m_box[rowNum][colNum] = b;
}

Thank you so much! That was a tremendous help. I haven't had programming in a while and couldn't remember bits and pieces.

And yes, one set function is for setting all the boxes and one is for a single box. Now I'm having trouble returning in my get function. It works when I use 'cout' instead of 'return', but my professor warned against using 'cout' in a class. The 'return' doesn't work though because it can only return one character.

Here is my new code:

//.cpp file

//Accessors

char Sweeper::get_box()const
{
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
            cout<< m_box[i][j] << " "; //i want to use return here but I   only get a single char return.
             cout<< endl;
    }   
}

//Mutators
void Sweeper::set_all(char boxes)
{
    for(int i = 0; i<row; i++)
    {
        for(int j = 0; j<col; j++)
            m_box[i][j]=boxes;
    }    
}
void Sweeper::set_abox(char x, int rowNum, int colNum)
{
    m_box[rowNum][colNum] = x;
}
Sweeper::Sweeper(char b) //constructor
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
            m_box[i][j] = 'b';
    }
}

Typo on my last post. Get rid of the quotes around 'b' above.

Thank you so much!

You're welcome.

And yes, one set function is for setting all the boxes and one is for a single box. Now I'm having trouble returning in my get function. It works when I use 'cout' instead of 'return', but my professor warned against using 'cout' in a class. The 'return' doesn't work though because it can only return one character.

Here is my new code:

char Sweeper::get_box()const
{
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
            cout<< m_box[i][j] << " "; //i want to use return here but I   only get a single char return.
             cout<< endl;
    }   
}

Well, do you want to get the entire 2-D array or just one box? I'm guessing you're just wanting to get one box since you're returning a char and you say you haven't worked with pointers yet (and presumably you haven't worked with addresses either)?

But the fact that you are using a loop in your function, as well as the comment inside of your function suggests you want the entire array. Or possibly that you're not 100% sure what you want? Regardless, if you're returning an entire array, you can't have the function return a char, and if you're returning a single char, you shouldn't have a loop.

I'm assuming you want one box, so that means your function prototype needs to change. Somehow your function needs to know WHICH of the 100 boxes to return and the way to do that is to pass it some parameters:

char Sweeper::get_box(int rowNum, int colNum) const
{
    return m_box[rowNum][colNum];
}

I realized it was a typo. It works beautifully.

I guess my problem is that I'm not sure whether or not I should return the entire array. I guess I could make two accessors and do both, but then when I declare my class, I'm not sure if I should declare a 2D array object in main or if the object is the 2D array.

Thanks again for all your help. You explain things so crystal clear. I hope I have a better understanding of it all one day.

Thank you for submitting this topic! It helps a lot bc I am trying to do a similar project. Is it bad to cout from within a class? Its the only way I have been able to get the entire array to print to console. Return, of course, only prints out a single char.

Thank you for submitting this topic! It helps a lot bc I am trying to do a similar project. Is it bad to cout from within a class? Its the only way I have been able to get the entire array to print to console. Return, of course, only prints out a single char.

Nico, you should probably create a new thread. This one has been marked Solved and people tend not to look at solved threads. You can make a hyperlink to this thread if you think it'll add to your thread.

It's not bad to use cout inside of a class. It's done all the time. It completely depends on what the function is designed to do. You might have a function whose entire job is to display an array, in which case using "cout" in that function would be fine.

You can't really return an array from a function. You can, however return a pointer to the first element of an array, but that involves using pointers.

Your best bet is to start a new thread and add a bit more detail as to what you are trying to do.

Thank you for submitting this topic! It helps a lot bc I am trying to do a similar project. Is it bad to cout from within a class? Its the only way I have been able to get the entire array to print to console. Return, of course, only prints out a single char.

I'm having the same problem returning single char. cout is the only thing that works for me right now. I was thinking about returning a single char in the accessor and using a loop in the ostream operator to display the entire array.

I can't make up my mind. Plus I'm not sure if in the main routine I am suppose to declare the object of my class as an array (Classname objectName[row][col]) or if the object itself is suppose to be an array.

I'm having the same problem returning single char. cout is the only thing that works for me right now. I was thinking about returning a single char in the accessor and using a loop in the ostream operator to display the entire array.

I can't make up my mind. Plus I'm not sure if in the main routine I am suppose to declare the object of my class as an array (Classname objectName[row][col]) or if the object itself is suppose to be an array.

Here's a line from your original spec:

- create a class containing a 10x10 two-dimensional array of characters.

The key word is "containing". You almost definitely are supposed to create ONE class object in main. That object contains the array inside of it (red below):

class Sweeper
{
public:

    const static int row=10; 
    const static int col=10;
    
    Sweeper(char b); //constructors
    Sweeper();
    
    void set(char b); //mutators
    void set_box(char b);
    
    char get_box() const; //accessors
    
    friend ostream& operator<<(ostream& os, const Sweeper& aSweeper);

private:
    char m_box[row][col];
};

My guess is the following. You shouldn't have cout statements anywhere in your class. Your accessor functions and your mutator functions definitely should not have cout statements in them. Displaying to the console happens in your << friend function (red below) and cout is PASSED to that as a parameter from main.

class Sweeper
{
public:

    const static int row=10; 
    const static int col=10;
    
    Sweeper(char b); //constructors
    Sweeper();
    
    void set(char b); //mutators
    void set_box(char b, int rowNum, int colNum);    

    char get_box(int rowNum, int colNum) const; //accessors - no cout statements
    
    friend ostream& operator<<(ostream& os, const Sweeper& aSweeper);  // Display happens here
private:
    char m_box[row][col];
};

In main:

Sweeper aSweeper ('#');
aSweeper.set_box ('X', 2, 3);
aSweeper.set_box ('X', 2, 5);
aSweeper.set_box ('X', 3, 3);
aSweeper.set_box ('X', 4, 7);
aSweeper.set_box ('X', 7, 2);
aSweeper.set_box ('X', 1, 9);
cout << aSweeper; // calls << function and passes it the parameter cout
commented: Another newbie costumer satisfied! +26

Thank you so much for all the help. I plan to complete the game this week, and am sure it will go smoother now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.