Jump to content
  • Advertisement
Sign in to follow this  

can't get output

This topic is 5399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can't get the output to show up on screen

using namespace std;

int main()
    const int rows = 3;
    const int columns = 3;
    char wall[rows][columns] = { {'x', 'x', 'x'},
                                 {'x', 'x', 'x'},
                                 {'x', ' ', 'x'}};
                                 cin>>wall [rows][columns];
    return 0;

Share this post

Link to post
Share on other sites
You need to use cout << yourArray[][] in a loop that cycles thru each piece of the array.


for(x = 0; x < rows; x++)
for(y = 0; y < columns; y++)
cout << yourArray[x][y];

Well I think thats right. Im not very experianced with using for loops like this.

EDIT: Opps teh '>' symbols beffore where wrong. Changed them to '<'

Share this post

Link to post
Share on other sites
1) You want to output stuff, and aren't seeing any output.

Oddly enough, cin is used for input; cout is used for output. Oh, and with cout you will need the little angle brackets to point the other way. This is easy to remember: the angle brackets point towards the thing that is receiving data.

cin>>foo; // data goes from standard input to foo
cout<<foo; // data goes from foo to standard output

2) I'm seeing a lot of this recently and I have no idea why. But.

When you declare a variable "char wall[rows][columns]", the stuff inside the square brackets is part of the variable's type. It is not part of the variable's name. "wall" is a 2-dimensional array of char's; its elements are chars. So to refer to the array, you don't put any []'s afterward. To refer to a specific element, you put the subscripts that refer to the element you want.

3) When you declare it that way, you say that there are rows-many 'positions' in one dimension, and columns-many in the other, which specify elements of wall. The rows are numbered, not from 1 to rows, but from 0 to (rows - 1). Trust me, this is much much more convenient in the long run. Anyway. When you say wall[rows][columns], well, that's wall[3][3], and that doesn't exist:

Conceptual drawing of "wall".
Real data in memory doesn't look anything like this, of course.

row -> 0 1 2
column 0 x x x
1 x x x
2 x x
# Oops.
You try to grab something here, but there isn't anything there.
Chances are good that that piece of memory doesn't even belong
to your program. And That's Bad. Segfault city.

However, you *are* on the right track, because...

4) Just outputting "an array" doesn't do what you expect. C++ arrays are not objects, they're just undecorated bits of memory that the compiler divides up into element-sized chunks. So when you refer to an array by name (without the [][]'s at the end), what you actually get is a pointer to the beginning of that undecorated bit of memory. This can be quite useful once you are comfortable with the concept, but is probably just going to confuse you for now. Anyway.

5) Synthesizing all of that: what you need to do, apparently, is print out every element of the array, one at a time. You probably want some formatting on it, too. It sounds like we need to generate all nine possible combinations of row/column values to access array elements, and use each combination to print an element. And after each row, maybe we should go to the next line or something.

Here are the tools you need:

a) The for loop. You've probably seen this before, but here's a quick refresher, using the most common form:

int counter;
for (counter = initial_value; counter < limit_value; ++counter) {
// Do stuff here. Counter takes on each value from
// initial_value up to limit_value-1, because of the strict
// inequality ('<'). Hmm, that -1 stuff sounds familiar yes?
// Anyway. Don't forget that "stuff" to do can include another
// loop.

b) As mentioned, the 'cout' stream. Recall that you can "cout << endl;" to output a new line and flush the buffer (make sure your stuff appears on screen).

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!