0

I'm having issues with initializing my array of char[][][] and displaying it of which i have never really used but must use now so I'm not very familiar with it. the array is used in a booking system where a user e asks for say a cabin on deck 1 column 1 row 1 which should be an "i"

Header File

#include <iostream>
#include <string>

using namespace std;

class Ship
{
public:

Ship()
{
}

char cabins[13][4][1];
void setArray();
void showArray();
};
1
  • 3
    Are you just making up syntax? Where did you come up with this stuff? Commented Sep 6, 2013 at 22:34

3 Answers 3

2

You wrote this

for (int k = 0; k < 13; k++)
{

    for (int j = 0; j< 1; j++)
    {
        for (int i = 0; i< 4; i++)
        {
    cabins[13][4][1] =  (("b" "i" "i" "b") 

Which would iterate across all the characters in the array, and then try to assign the entire array that slot, if it worked, or was valid C++.

I'm not sure what "(("b" "i" "i" "b")" is supposed to be, but you seem to have missed some C++ fundamentals. What you actually want is something more like

char cabins[13][4]; // only needs to be 2d.

void Ship::setArray()
{
    cabins = {
       { 'b', 'i', 'i', 'b' },
       { 'b', 'i', 'i', 'b' },
       ...
    };
}

[edit: I hit return early, working on the laptop, sorry]

This too would not work. If the array definition here is to be persistent, you'll need to store it somewhere.

Here's a complete single-compilation-unit example of how you might solve it:

#include <iostream>
#include <cstring> // for memcpy

class Ship {
public:
    Ship() {}
    char m_cabins[4][4];
    void setArray();
};

void Ship::setArray() {
    static const char defaultCabinLayout[4][4] = {
        { 'b', 'i', 'i', 'b' },
        { 'b', 'i', 'i', 'b' },
        { 'w', 'i', 'i', 'w' },
        { 'w', 'i', 'i', 'w' },
    };
    static_assert(sizeof(m_cabins) == sizeof(defaultCabinLayout), "defaultCabinLayout does not match m_cabins");
    memcpy(m_cabins, defaultCabinLayout, sizeof(m_cabins));
}

int main() {
    Ship s;
    s.setArray();
    std::cout << "cabins[0][0] = " << s.m_cabins[0][0] << std::endl;    
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use curly braces { } around values in initialization, not parentheses. Add commas in between too! And also, what is the array's definition doing inside your loop? Oh, my...

2 Comments

and characters not strings.
Yes, there's a lot that can be missed in this program... :-)
0

It should be this...

cabins[k][j][i] 

In you're loop if you are trying to initialize it at certain indexes.

for instance.

cabins[1][1][1] = 1; 

This is setting the value at deck 1 column 1 and row 1 equal to 1.

When you are looping through those 3 for loops you are incrementing through i, j, k

I don't really understand how you are assigning values per index so.... if you elaborate more on that I could help you a bit more.

1 Comment

There's no reason for this to be a 3-dimensioned array - it's a 2d array of chars. The 3rd dimension is from his imagination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.