Program doesn't work, but gives no errors.

I'm trying to get this program to display rows of '.'s (as a blank map in a roguelike game), but it doesn't show anything. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

using namespace std;

void map();

int main()
{
    void map();

    return 0;
}

void map()
{

int fullMap[20];
int x = 0;
int y = 0;
int px = 0;
int py = 0;

for (x = 0; x < 21; x++)
{
    for (y = 0; y < 21; y++)
    {
        fullMap[x,y] = 1;
    }
}

for (px >= 0; px < 21; px++)
{
    for (py >= 0; py < 21; py++)
    {
        if (fullMap[px,py] = 1)
        {
            cout << ".";
        }
    }
}

}


I know that at the end, it won't show what I'm trying to get it to show, but for now I'd be thrilled if it showed anything. No errors pop up when it's compiled, but their are some warnings, including 'statement has no effect' for the last two for loops. Anybody see any horribly stupid mistakes?
Lines 27 and 35 make no sense. A) that's no way to specify two-dimensional array access; and B) fullMap is not a two-dimensional array.
Oh. What would I do instead of an array then?
You could declare a two-dimensional array:

int fullMap[20][20];

and then access it appropriately:

fullMap[x][y];
Thanks, but it still doesn't show anything. It still says 'statement has no effect' for lines 33 and 35.
Try replacing line 35 with
if (fullMap[px][py] == 1)

and when you use for(...), the counter should at first has a certain value (in your case, the counter's py and it should look like
1
2
for (py=certain_int; py<21; py++) 
{...}
Last edited on
// This may be wrong, but does the fact that the period in cout << "."; is surrounded by double quotes rather than single quotes? I'm not sure if it's an issue, but I just remember seeing anything with a single character surrounded by single quotes.

// (>'.')>
Okay, here's the corrected code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

using namespace std;

void map();

int main()
{
    void map();
    return 0;
}

void map()
{

int fullMap[20][20];
int x = 0;
int y = 0;
int px = 0;
int py = 0;

for (x = 0; x < 21; x++)
{
    for (y = 0; y < 21; y++)
    {
        fullMap[x][y] = 1;
    }
}

for (px = 0; px < 21; px++)
{
    for (py = 0; py < 21; py++)
    {
        if (fullMap[px][py] == 1)
        {
            cout << ".";
        }
    }
}

}


No errors, no warnings, and yet it still doesn't show anything. Is there some problem with if (fullMap[px][py] == 1) not being true?
I hate to break this to ya...

This program is flawed, unfortunately, and you may get the classic, one and only mother and Agent Smith of all C++ runtime errors, the segmentation fault. Replacing all the 21s with 20s should prevent it.

Oh. Also. Why is nothing printing? You can fix it by getting rid of the the void in line 9.

(In short, fill it in with whitespace... no, but seriously, ditch that void).

-Albatross
Last edited on
HUZZAH! It works! Though I didn't mean for it to fill up five lines all the way across the screen, but I sorta expected that.

@ Albatross
Why exactly does switching 21's for 20's make it work? I would have thought that having their be a less-than sign would have made it stop at 20.
That's exactly the point, Urist. Your array has 20 elements, not 20 + (0) = 21. You need it to stop at 19.

-Albatross
Ah, I see.
Final question (maybe): how would I clear the screen without using system("CLS")? Everywhere I look people say "Don't use it, it reduces portability", but I need to have the computer redraw the map as it changes.
You could print 128 newlines to the scree- ah, actually, I'll link to an article that has a lot of info on clearing the console. It should help!
http://www.cplusplus.com/forum/articles/10515/

-Albatross
Also, when you call a function, you don't need to include the return type...
Topic archived. No new replies allowed.