ugh...

My console application keeps getting shut down by something. I don't know what. I am rather sure that the problem is confined to this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void ArrayOut(bool cursor, char cursor_character, int NumberOfRows, int NumberOfColumns, int locx, int locy, int locz, char level[][50][50]){
     int h;
     int x;
     int y;
     do{
          if(cursor == true){
               h = cursor_character;
          }
          if(x != locx || y != locy || cursor == false){
               h = level[y][x][locz];
          }
          std::cout << h;
          x++;
          if(x >= NumberOfRows){
               x = 0;
               y++;
               std::cout << std::endl;
          }
     }
     while(x != NumberOfRows && y != NumberOfColumns);
     return;
}


I am using a rather old version of gcc (from before C++11) because I'm on a school machine, and can't get it home yet (forgot my flash drive).
Here's the rest of 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <basicP.h>

using namespace std;
char h;
char in;
int inpt;
int x = 0;
int y = 0;
int locx = 1;
int locy = 1;
int locz = 1;
char Level[31][50][50]={
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
     "101010101010101010101010101010",
};

void move(){
     in = GetKeystroke();
     inpt = in - '0';
     switch(inpt){
               case -3:
                    locz --;
                    break;
               case -5:
                    locz ++;
                    break;
               case 8:
                    locy --;
                    break;
               case 2:
                    locy ++;
                    break;
               case 4:
                    locx --;
                    break;
               case 6:
                    locx ++;
                    break;
               case 0:
                    return;
                    break;
          }
}

int main(){
     do{
         ScreenSize(32, 33);
          ClearScreen();
          //cout << locx << ", " << locy << ", " << locz << endl;
          ArrayOut(true, 'Q', 30, 30, locx, locy, locz, Level);
          //move();
     }
     while(inpt != 0);
     return 0;
}
Last edited on
Well is it an error? You need to give more details instead of just "shutting down"


you also are passing a three dimensional array into two dimensions char level[][50]. And then you proceed to attempt to access the third dim of Level[][].

Unless this is a school project that is required, using just two dimensional rays is bad practice, the same goes for three. You should think of different ways to accomplish your project without having to use shady c++ techniques, look at graphics libraries for example.
Last edited on
Have you read this thread:
http://www.cplusplus.com/forum/beginner/1988/

I don't see anything in your code that would prevent your console window from closing when you exit the while loop at line 32.

Line 79: You're passing a three dimensional array, but ArrayOut is expecting a two dimensional array. Line 10 of ArrayOut, you're trying to reference as a three dimensional array. This shouldn't even compile.

Need4Sleep: I posted this sort of out of the blue. I had compiled last just before I changed it into a 3 dimensional array in the declaration. I apolagize for not posting the compiled version, but that's the only difference. I am going to edit it out.It is an error, but it only brings up the error code "-1073741510 " which I looked into and apparently it's the error code that is brought up when you close the program. I get a notification box that tells me that it has stopped working. It is a school thing (hence the reason I'm working on it at school) and the teacher has only taught 2 dimensional arrays, and last time I did something that she hadn't taught yet (I used SetConsoleTextAttributes(handle, FOREGROUND_RED); instead of system(color 4f); (not that I actually know the number for red)) she marked me wrong.

AbstarctAnon: I thumbed through the thread, and I don't really see the relevance. Some thing in the code is causing it to shut down to the extent that I'm getting the error up there ^^^. And I'm trying to exit at the end of the loop.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void ArrayOut(bool cursor, char cursor_character, int NumberOfRows, int NumberOfColumns, int locx, int locy, int locz, char level[][50][50]){
     int h;
     int x;
     int y;
     do{
          if(cursor == true){
               h = cursor_character;
          }
          if(x != locx || y != locy || cursor == false){
               h = level[y][x][locz];
          }
          std::cout << h;
          x++;
          if(x >= NumberOfRows){
               x = 0;
               y++;
               std::cout << std::endl;
          }
     }
     while(x != NumberOfRows && x != NumberOfColumns);   // <- First condition will always be true.
     return;
}


As noted in the comment above, the do-while condition is effectively while (x != NumberOfColumns), because of lines 14 and 15. You do not initialize the variable y in the function so it's value may be anything, but you use it to index your array which is a recipe for undefined behavior.
cire: I can't believe I didn't notice that. The variable 'h' should also have been declared as a character not an integer. I initialized x and y and changed h to a char. It's not closing anymore, thank you!
Topic archived. No new replies allowed.