News:

11 March 2016 - Forum Rules

Main Menu

Need help. [SOLVED!]

Started by InfamousKnight, December 19, 2012, 05:36:08 PM

Previous topic - Next topic

InfamousKnight

Why doesn't this output anything?
#include<iostream>
using namespace std;
char map[30][30];
int cx = 4;
int cxm = 7;
int cy = 4;
int cym = 7;
int x = 5;
int y = 5;
char input;
main() {
for(int gx = 0; gx <= 30; gx++) {
for(int gy = 0; gy <= 30; gy++)
map[gx][gy] = 'X';
}
for(int gx = 5; gx <= 15; x++) {
for(int gy = 5; gy <= 15; gy++)
map[gx][gy] = ' ';
}
while(true) {
for(int i = 0; i <= 55; i++) {
cout << endl;
}
for(int vy = cy; vy <= cym; vy++) {
for(int vx = cx; vx <= cxm; vx++)
cout << map[vy][vx];
cout << endl;
}
cin >> input;
switch(input) {
case 'd':
if(map[y+1][x] != ' ') break;
map[y][x] = ' ';
y++;
map[y][x] = '@';
cy++;
cym++;
break;
case 'a':
if(map[y-1][x] != ' ') break;
map[y][x] = ' ';
y--;
map[y][x] = '@';
cy--;
cym--;
break;
case 'w':
if(map[y][x-1] != ' ') break;
map[y][x] = ' ';
x--;
map[y][x] = '@';
cx--;
cxm--;
break;
case 's':
if(map[y][x+1] != ' ') break;
map[y][x] = ' ';
x++;
map[y][x] = '@';
cx++;
cxm++;
break;
}
}
}

I'm trying to recreate a map system I made when I was 16. With clean code that is. You do not want to see my old code. It has like unused variables and sloppy indents due to copy and pasting old code off a forum. Which was my code. I pasted it into dev-cpp which wasn't a very good idea.

Garoth Moulinoski

#1
I think you have an extra }

Do you get any error message at all?

Edit: Nevermind. I missed one { in the loop.

I suggest doing this to make looking up brackets easier:


class main
{

   //code

   while (...)
   {

       //code

       for(...)
       {
             //code
       }

       //code

   }

   //code

}



Edit: Maybe this can help?
Who will quote me next?
Disclaimer: If it sounds wrong, I may have been posting while asleep.

BRPXQZME


for(int gx = 5; gx <= 15; x++) {
we are in a horrible and deadly danger

Garoth Moulinoski

Quote from: BRPXQZME on December 19, 2012, 05:54:20 PM

for(int gx = 5; gx <= 15; x++) {


Or that. That'll definitely create some sort of infinite loop or bug otherwise. :P So much for for loops being safer to use than while loops :P (Not saying that for loops are useless; I use them far more than while loops)
Who will quote me next?
Disclaimer: If it sounds wrong, I may have been posting while asleep.

InfamousKnight

#4
I think I have to switch around the directions too. Thanks for your help! I honestly didn't see that.

That's strange. I thought array coordinates were the complete opposite of the algebra coordinates.. Like instead of xy it would be yx. But they're the same. Like they're not reversed.

BRPXQZME

They are not the same at all! Algebra coordinates stretch off to infinity and array coordinates are limited, for one. In fact, you exceed these limits in your first set of for loops. The next difference is why you don't crash anything: the location after map[r][29] is map[r+1][0] for r<29.

Also, these nested for loops are very inefficient ways to handle character arrays and the only reason it's not causing trouble is that these are not very big arrays.
we are in a horrible and deadly danger

InfamousKnight

Quote from: BRPXQZME on December 19, 2012, 06:42:20 PM
They are not the same at all! Algebra coordinates stretch off to infinity and array coordinates are limited, for one. In fact, you exceed these limits in your first set of for loops. The next difference is why you don't crash anything: the location after map[r][29] is map[r+1][0] for r<29.

Also, these nested for loops are very inefficient ways to handle character arrays and the only reason it's not causing trouble is that these are not very big arrays.
That's not what I meant. I mean the ordered pairs. Like xy in the algebra system. I didn't mean that arrays and algebra coordinates were the same. I meant the ordered pairs are the same.

Garoth Moulinoski

You shouldn't dismiss BRP's warning though:

Quote from: BRPXQZME on December 19, 2012, 06:42:20 PM
Also, these nested for loops are very inefficient ways to handle character arrays and the only reason it's not causing trouble is that these are not very big arrays.

It might not be a big a deal in this situation, but realize that every time you have a loop, you're essentially telling the computer to do a certain amount of instructions any number of times. Although each instruction takes constant time, each loop adds n to that time. So, your main takes at least a bit more than O(n^2). I wasn't very good at calculating Big O, but I can at least tell by just looking at it that it's at minimum that much (which it isn't, it's a little more than that). O(n^2) isn't very efficient, with the best you can hope is probably O(log n) and O(n) (and O(1) but chances are, you're not doing anything interesting in that situation).

Edit: 2n + n(2n), so your thing takes O(n^2), I think. Someone who remembers that may be able to provide a more accurate answer if mine is not.
Who will quote me next?
Disclaimer: If it sounds wrong, I may have been posting while asleep.

BRPXQZME

Quote from: InfamousKnight on December 19, 2012, 08:58:04 PM
That's not what I meant. I mean the ordered pairs. Like xy in the algebra system. I didn't mean that arrays and algebra coordinates were the same. I meant the ordered pairs are the same.
That's wrong, too. Algebraic coordinates are continuous.

Quote from: Garoth Moulinoski on December 19, 2012, 09:14:53 PM
[snip]
It is indeed O(n2), which is indeed no good. You can't avoid that for the second loop (technically), but in both cases more efficient code would use memset, which will be faster and less error-prone.
we are in a horrible and deadly danger

Klarth

Quote from: InfamousKnight on December 19, 2012, 05:36:08 PM
Why doesn't this output anything?
I know this is solved, but here's some advice: Learn to use the debugger and breakpoints. You can step through your code, view variables, etc as your program is running.

snarfblam

BRPXQZME, I'm not having a very hard time understanding what he means here. Why are you?

Quote from: InfamousKnight on December 19, 2012, 06:22:35 PMLike instead of xy it would be yx. But they're the same. Like they're not reversed.

In fact, InfamousKnight, you can order them however you like (as long as you're consistent), but there's usually no reason not to stick with [ x ][ y ].

Garoth Moulinoski

Quote from: BRPXQZME on December 19, 2012, 09:19:19 PM
It is indeed O(n2), which is indeed no good. You can't avoid that for the second loop (technically), but in both cases more efficient code would use memset, which will be faster and less error-prone.

Ah, thanks. I was afraid I was rusty...

Quote from: Klarth on December 19, 2012, 09:57:58 PM
I know this is solved, but here's some advice: Learn to use the debugger and breakpoints. You can step through your code, view variables, etc as your program is running.

I wanted to give him the benefit of the doubt and hoped he'd done this. But then again, had he used a debugger, he probably would have caught this easily. It's that kind of error... But this is indispensable advice for him. Hopefully, he takes it to heart and starts using a debugger...

(And since you didn't explain what breakpoints are: IK, They're like "flags" where you can tell the debugger to pause the program while it's running; So, you want to check the state of your program at line 150, where something interesting just happened, you put a breakpoint there, run the program in debug and it runs until it reaches line 150 and then it waits for you take a step or hit run again.)
Who will quote me next?
Disclaimer: If it sounds wrong, I may have been posting while asleep.

BRPXQZME

Quote from: snarfblam on December 19, 2012, 10:02:49 PM
BRPXQZME, I'm not having a very hard time understanding what he means here. Why are you?
I understand perfectly well. I also have seen plenty of classmates—most of them 16–19 years old at the time—get cut down due to bad assumptions in all too many years of math classes.
we are in a horrible and deadly danger

relminator

Hi, I know this is solved but aren't you accessing 1 more element than you should in your first for loop accessing char map[30][30];?