Program that prints out array is not working

Started by
6 comments, last by Serapth 11 years, 6 months ago

#include <iostream>
using namespace std;
int map[5][5] =
{
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1
};
//other stuff
int mapn = 0;
int mapn2 = 0;
int number = 0;
int number2 = 0;
int x, y = map[x][y];
int main()
{
x = map[2][y];
y = map[x][2];
while (mapn < 5, mapn2 < 5)
{
cout << map[number][number2];
mapn++;
number++;
if (mapn == 5)
{
mapn2++;
number2++;
mapn = 0;
cout << endl;
}
}
cout << endl;
system("PAUSE");
}


This program is supposed to print out an array. To do so, it prints out a number, adds onto that number and once that number equals five, number2 has 1 added onto it, and number is reset. Why isn't it working?
It compiles fine, but the output is a lot of random numbers. :(
Advertisement
number isn't reset. Is there any particular reason you declared number and number2 instead of using mapn and mapn2? Also, you should probably be using nested for loops for going through a 2D array, and "mapn < 5, mapn2 < 5" probably doesn't do what you think it does (the comma operator is simply equal to the second expression, so that code is equivalent to mapn2 < 5)
[spoiler][s]Ah thanks, number isn't reset! That solved the problem! Thanks for pointing that out.

Well, now I'm trying to create a movement script for an ingame character:

prev = map[x][y];
cout << "Where would you like to go?" << endl;
cin >> input;
if (input == 8)
{
y--;
x, y == 2;
prev == 0;
goto map;
mapn = 0;
mapn2 = 0;
number = 0;
number2 = 0;
}


But it seems to just automatically skip the loop again, despite it being reset...
Is there a way to fix this?

Sorry, but I'm just really new to C++ so my code probably looks extremely bad.[/spoiler][/s]

Nvm
Just an FYI "goto" is typically frowned upon by programmers and usually considered bad practice and harmful, just search "Edsger Dijkstra on goto".

Example for loop for cycling this kind of 2d array:

for (int x = 0; x < 5; x++) {
// In here you can do something with just x in the map
// int somethingElse = map[x][0];
for (int y = 0; y < 5; y++) {
int something = map[x][y];
}
}
Seriously... where are new developers even being exposed to goto anymore?!?!?

I mean, it should have been relegated to complete niche status within the language by this point. I dont think it is even featured in most C++ books, so how the heck people even learn about it.

int x, y = map[x][y];

I'm really curious what happens here. x is declared as an int without being defined, and y is declared as an int defined as map[x][y]...that's a recursive definition on top of an undefined index? It's been a while since I mucked with C++.
I guess it doesn't matter since x and y are unused after their first assignment in main. But I don't see how that's being resolved by the compiler.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)


[quote name='Youbar' timestamp='1350359411' post='4990604']
int x, y = map[x][y];

I'm really curious what happens here. x is declared as an int without being defined, and y is declared as an int defined as map[x][y]...that's a recursive definition on top of an undefined index? It's been a while since I mucked with C++.
I guess it doesn't matter since x and y are unused after their first assignment in main. But I don't see how that's being resolved by the compiler.
[/quote]
Unless I am missing something, the array is improperly populated.
Both X and Y are declared as NULL
After MAIN they are both declared as map[2][2], which are both NULL
Also as it's written the values of X,Y will not update when the map is updated.

Corrected 2D array declaration.

int map[5][5] =
{
{1, 1, 1, 1, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 1, 1, 1, 1}
};

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson


[quote name='BCullis' timestamp='1350405737' post='4990791']
[quote name='Youbar' timestamp='1350359411' post='4990604']
int x, y = map[x][y];

I'm really curious what happens here. x is declared as an int without being defined, and y is declared as an int defined as map[x][y]...that's a recursive definition on top of an undefined index? It's been a while since I mucked with C++.
I guess it doesn't matter since x and y are unused after their first assignment in main. But I don't see how that's being resolved by the compiler.
[/quote]
Unless I am missing something, the array is improperly populated.
Both X and Y are declared as NULL
After MAIN they are both declared as map[2][2], which are both NULL
Also as it's written the values of X,Y will not update when the map is updated.

Corrected 2D array declaration.

int map[5][5] =
{
{1, 1, 1, 1, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 1, 1, 1, 1}
};

[/quote]

Actually, you can populate a 2D array that way. Both are functionally equivalent.

I am rather surprised that the code does compile though... there is a lot going on there that would require good knowledge of the spec to figure out what would happen.

Interesting lines are the aforementioned:

int x, y = map[x][y];


Which my brain is strugging to grok. I am kinda surprised that compiles. I believed C++ evaluated right to left, so it would encounter the y and x indexes that are undeclared.

The other fun line is:
while (mapn < 5, mapn2 < 5)

Which if my understand of C++ is correct, actually evaluates to
while(mapn2 < 5)

In that it would evaluate the first expression, throw it away, then evaluate the second expression as the condition of the loop.

This topic is closed to new replies.

Advertisement