440,812 Members | 863 Online
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

# Help with code (parse error)

 P: n/a Basically I've just started making a game. So far it makes an array 25 by 20 and tries to make five rooms within it. In scr_make_room() there's parse errors: 20 C:\c\Rooms\Untitled1.c parse error before "top_x" 28 C:\c\Rooms\Untitled1.c parse error before "first_square_x" 35 C:\c\Rooms\Untitled1.c parse error before '}' token Why are these happening? int room_square[25][20], selected = 1, squares = 1, first_square_x = -1, first_square_y = 1; void scr_make_room(void) //Make a room { int top_x, roomx, top_y, roomy, xplus, yplus; top_x = 8; //floor(random(21))+1 //Do this later when roomx = 5; //floor(random(8))+1 //I can use random() top_y = 12; //floor(random(16))+1 // and floor() roomy = 5; //floor(random(8))+1 //Turn all the wall squares within the room to floor for (xplus = 0; xplus < roomx; xplus++) { for (yplus = 0; yplus < roomy; xplus++) { // make sure it's in the room 20: if top_x + xplus < 24 && top_y + yplus < 19 //***PARSE ERROR { if room_square[top_x+xplus][top_y+yplus] == 0 //if there is wall { room_square[top_x+xplus][top_y+yplus] = 1; //make it floor squares+=1; //increase the number of squares made //record the first square made(ever) 28: if first_square_x == -1 //***PARSE ERROR { first_square_x = top_x+xplus; first_square_y = top_y+yplus; }//endi }//endi wall }//endi in room 35: }//end for y //***PARSE ERROR }//end for x }//end scr_make_room() int main() { int i, j; for (i = 0; i < 25; i++) { for (j = 0; j < 20; i++) { room_square[i][j]=0; } } for (i=0; i<5; i+=1) //make 5 rooms { scr_make_room(); } return 0; } Sep 30 '06 #1
21 Replies

 P: n/a BWIGLEY wrote: Basically I've just started making a game. So far it makes an array 25 by 20 and tries to make five rooms within it. In scr_make_room() there's parse errors: 20 C:\c\Rooms\Untitled1.c parse error before "top_x" 28 C:\c\Rooms\Untitled1.c parse error before "first_square_x" 35 C:\c\Rooms\Untitled1.c parse error before '}' token Why are these happening? Because you have omitted the parentheses in the if(...) construct. They are not optional. if (x < y) /* pass "Go," collect \$200 */ if x < y /* go to jail. go directly to jail. */ -- Eric Sosman es*****@acm-dot-org.invalid Sep 30 '06 #2

 P: n/a BWIGLEY wrote: Basically I've just started making a game. So far it makes an array 25 by 20 and tries to make five rooms within it. In scr_make_room() there's parse errors: 20 C:\c\Rooms\Untitled1.c parse error before "top_x" 28 C:\c\Rooms\Untitled1.c parse error before "first_square_x" 35 C:\c\Rooms\Untitled1.c parse error before '}' token Why are these happening? int room_square[25][20], selected = 1, squares = 1, first_square_x = -1, first_square_y = 1; void scr_make_room(void) //Make a room { int top_x, roomx, top_y, roomy, xplus, yplus; top_x = 8; //floor(random(21))+1 //Do this later when roomx = 5; //floor(random(8))+1 //I can use random() top_y = 12; //floor(random(16))+1 // and floor() roomy = 5; //floor(random(8))+1 //Turn all the wall squares within the room to floor for (xplus = 0; xplus < roomx; xplus++) { for (yplus = 0; yplus < roomy; xplus++) { // make sure it's in the room 20: if top_x + xplus < 24 && top_y + yplus < 19 //***PARSE ERROR if (top_x + xplus < 24 && top_y + yplus < 19) /* You need the parenthesis */ Robert Gamble Sep 30 '06 #3

 P: n/a "Eric Sosman"

 P: n/a "BWIGLEY" Why are these happening? Because you have omitted the parentheses in the if(...) construct. They are not optional. if (x < y) /* pass "Go," collect \$200 */ if x < y /* go to jail. go directly to jail. */ Thanks. Anyone know why it crashes when I try to run it? OK, it's crashing because it's trying to put "0" in lots of memory in: for (i = 0; i < 25; i++) { for (j = 0; j < 20; i++) { room_square[i][j]=0; } } i just keeps going up. What's wrong with it. Sep 30 '06 #5

 P: n/a BWIGLEY wrote: Thanks. Anyone know why it crashes when I try to run it? You are incrementing i when you mean to increment j. Sep 30 '06 #6

 P: n/a "jmcgill"

 P: n/a BWIGLEY wrote: Thanks. Anyone know why it crashes when I try to run it? Fixed as far as I could understand it, attached. Sep 30 '06 #8

 P: n/a "jmcgill" Thanks, this is very much appreciated. From now on I think I'll format my code like this. I don't know if anyone can be bothered, but if someone could please tell me how to use random() and floor() ( what to #include etc.) so I can actually randomly generate a level (I've finished everything else and it works nicely). I'll probably be able to find this, but they aren't in the tutorials I've downloaded so far... Sep 30 '06 #9

 P: n/a BWIGLEY wrote: >>Thanks. Anyone know why it crashes when I try to run it? Fixed as far as I could understand it, attached. Thanks, this is very much appreciated. From now on I think I'll format my code like this. I don't know if anyone can be bothered, but if someone could please tell me how to use random() random() is not standard C. In standard C you get RAND_MAX which is at least 32767 (but probably is the maximum int for your system), you get rand() which gives a random int between [0 and RAND_MAX] inclusive, and a seed function, void srand(unsigned int seed); To get a double precision random number, you can do this, no guarantees about the distribution or anything. double r; r=( ((double) rand()) / (double) (RAND_MAX) + (double)(1.0) ); (Personally, I've been using the random number generator from a crypto library, not the standard function). and floor() floor() is in Sep 30 '06 #10

 P: n/a "jmcgill" Thanks. Anyone know why it crashes when I try to run it? Fixed as far as I could understand it, attached. Thanks, this is very much appreciated. From now on I think I'll format my code like this. I don't know if anyone can be bothered, but if someone could please tell me how to use random() random() is not standard C. In standard C you get RAND_MAX which is at least 32767 (but probably is the maximum int for your system), you get rand() which gives a random int between [0 and RAND_MAX] inclusive, and a seed function, void srand(unsigned int seed); To get a double precision random number, you can do this, no guarantees about the distribution or anything. double r; r=( ((double) rand()) / (double) (RAND_MAX) + (double)(1.0) ); (Personally, I've been using the random number generator from a crypto library, not the standard function). and floor() floor() is in Thanks, I've got it working perfectly now. How should I print the array to the screen? Sep 30 '06 #11

 P: n/a "BWIGLEY" Thanks, I've got it working perfectly now. How should I print the array to the screen? BTW, here's the source so far: /*Program outline Make 5 rooms, randomly placed Try to select all squares that touch the first square (and select ones near them etc.) If all not all squares are selected make 2 more rooms retry selecting (only ones that don't have selected squares all around them) repeat room creation, selection trying etc. If at any time it's close to having all squares selected delete all unselected squares Convert selected squares to floor (1) */ #include

 P: n/a BWIGLEY wrote: Basically I've just started making a game. So far it makes an array 25 by 20 and tries to make five rooms within it. By the way, since you seem to be making a 2d look-down-on-the-action game, you might be interested in the much more topical rec.games.roguelike.development. That group is devoted to programming this sort of game, and in particular the amount of literature on generating 2d maps in an array is staggering. Sep 30 '06 #13

 P: n/a On Fri, 29 Sep 2006 22:52:16 -0700, jmcgill BWIGLEY wrote: >>>Thanks. Anyone know why it crashes when I try to run it?Fixed as far as I could understand it, attached. Thanks, this is very much appreciated. From now on I think I'llformat my code like this.I don't know if anyone can be bothered, but if someone could pleasetell me how to use random() random() is not standard C.In standard C you get RAND_MAX which is at least 32767 (but probably isthe maximum int for your system), you get rand() which gives a randomint between [0 and RAND_MAX] inclusive, and a seed function,void srand(unsigned int seed);To get a double precision random number, you can do this, no guaranteesabout the distribution or anything.double r;r=( ((double) rand()) / (double) (RAND_MAX) + (double)(1.0) ); You don't need any of the casts. 1.0 is already a double. The + operator will force RAND_MAX to be converted to double. That will make the denominator a double. The / operator will force the numerator to be converted to double. That will make the quotient a double. Why did you add +1? Your code will give a value in the range [0,1). It can produce 0 but can never produce 1. Was there something in the OP's concept that would make 1 undesirable? >(Personally, I've been using the random number generator from a cryptolibrary, not the standard function). >and floor() floor() is in Remove del for email Sep 30 '06 #14

 P: n/a On Sat, 30 Sep 2006 23:25:30 +1200, "BWIGLEY" wrote: >"BWIGLEY" "jmcgill" >Thanks, I've got it working perfectly now. How should I print thearray to the screen? BTW, here's the source so far:/*Program outlineMake 5 rooms, randomly placedTry to select all squares that touch the first square (and select onesnear them etc.)If all not all squares are selected make 2 more roomsretry selecting (only ones that don't have selected squares all aroundthem)repeat room creation, selection trying etc.If at any time it's close to having all squares selected delete allunselected squaresConvert selected squares to floor (1)*/#include } //end scr_make_room()void scr_select_squares(int square_x, int square_y) { int amnt = 0; if ((room_square[square_x-1][square_y] == -1 ||room_square[square_x-1][square_y] == -2) && //if all 'round it'sselected be done You should not use // comments for usenet. They don't survive line wraps and prevent others from compiling your code to help. snip rest of function >}int main(int argc, char** argv) { snip //lets try printing it: for (i=0; i<25; i++) { printf("\n"); for (j=0; j<25; j+=1) { switch (room_square[i][j]){ case 0:{ printf("0"); };break; The braces, {}, are superfluous. The semicolon just after the right brace is a null statement. Did you really mean that? If so, why? If you are going to use braces, you should move the break statement inside. case 1:{ printf("."); };break; case -1:{ printf("s"); };break; case -2:{ printf("-"); };break; } //printf("%d", room_square[i][j]); } } printf("\n"); getch(); This is a non-standard function. How does the user know that he should press a key? snip more code //make selected spaces floor for (i=1; i<24; i++) { for (j=1; j<24; j++) { if (room_square[i][j] == -1 || room_square[i][j] == -2) { room_square[i][j] = 1; } } } //lets try printing it: for (i=0; i<25; i++) { printf("\n"); for (j=0; j<25; j+=1) { Variety may be the spice of life but a consistent code style is a virtue. Think about coming back to this program after several months. snip rest of function >} Remove del for email Sep 30 '06 #15

 P: n/a Barry Schwarz wrote: On Fri, 29 Sep 2006 22:52:16 -0700, jmcgill >To get a double precision random number, you can do this, no guaranteesabout the distribution or anything.double r;r=( ((double) rand()) / (double) (RAND_MAX) + (double)(1.0) ); You don't need any of the casts. Debatable, considering "no guarantees." The code as it stands sets r to a value somewhere between 1.0 and 2.0, with both extremes included. Remove the casts and it will set r to 1.0 most of the time and 2.0 rarely, never anything in between. 1.0 is already a double. Right. The + operator will force RAND_MAX to be converted to double. Wrong: division "binds more tightly" than addition. That will make the denominator a double. The / operator will force the numerator to be converted to double. That will make the quotient a double. Why did you add +1? Your code will give a value in the range [0,1). No: the range is 1.0 <= r <= 2.0. It seems likely that this was not jmcgill's intention, but that he garbled the usual formula r = rand() / (RAND_MAX + 1.0); .... for a [0,1) uniform value. (Speculation: In his attempt to make it look fancier by adding unnecessary casts and way too many parentheses, he lost track of which parens went with which. If so, jmcgill should probably avoid Lisp!) But since he said "no guarantees" we'd be in a false position trying to tut-tut him. His code produces a pseudo-random value, and although the probability distribution it simulates is a bit peculiar it can't be called "wrong" without a specification. -- Eric Sosman es*****@acm-dot-org.invalid Sep 30 '06 #16

 P: n/a Eric Sosman wrote: No: the range is 1.0 <= r <= 2.0. It seems likely that this was not jmcgill's intention, but that he garbled the usual formula r = rand() / (RAND_MAX + 1.0); ... for a [0,1) uniform value. (Speculation: In his attempt to make it look fancier by adding unnecessary casts and way too many parentheses, he lost track of which parens went with which. If so, jmcgill should probably avoid Lisp!) Help the OP with the questions, don't tear apart my attempt to help. Sep 30 '06 #17

 P: n/a jmcgill said: Eric Sosman wrote: > No: the range is 1.0 <= r <= 2.0. It seems likely thatthis was not jmcgill's intention, but that he garbled theusual formula r = rand() / (RAND_MAX + 1.0);... for a [0,1) uniform value. (Speculation: In his attemptto make it look fancier by adding unnecessary casts and way toomany parentheses, he lost track of which parens went with which.If so, jmcgill should probably avoid Lisp!) Help the OP with the questions, don't tear apart my attempt to help. Why not? Don't *you* want to learn something new? Most of my misconceptions about C were only sorted out when I tried to help others, here in clc, and my attempts to help got ripped apart by the regulars. Yes, I suppose I could have moaned about it, but to what end? I'm *glad* I was corrected, because it meant I was learning more about the language. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at above domain (but drop the www, obviously) Oct 1 '06 #18

 P: n/a "Barry Schwarz" wrote: "BWIGLEY" Thanks, I've got it working perfectly now. How should I print the array to the screen? BTW, here's the source so far: //printf("%f\n",(rand()/(RAND_MAX + 1)) * 21); If you uncomment this, it will invoke undefined behavior. The second argument is an int, whose value will always be zero, and the %f says printf can expect a double. Yeah, I was trying to figure it out, and this was one of the ways I tried. > snip rest of function } //end scr_make_room() void scr_select_squares(int square_x, int square_y) { int amnt = 0; if ((room_square[square_x-1][square_y] == -1 || room_square[square_x-1][square_y] == -2) && //if all 'round it's selected be done You should not use // comments for usenet. They don't survive line wraps and prevent others from compiling your code to help. Yeah, I sort of realized that after I posted this. I'll do this from now on. snip rest of function } int main(int argc, char** argv) { snip //lets try printing it: for (i=0; i<25; i++) { printf("\n"); for (j=0; j<25; j+=1) { switch (room_square[i][j]){ case 0:{ printf("0"); };break; The braces, {}, are superfluous. The semicolon just after the right brace is a null statement. Did you really mean that? If so, why? If you are going to use braces, you should move the break statement inside. I didn't mean that, I'll but the break statement inside my brackets from now on. } printf("\n"); getch(); This is a non-standard function. How does the user know that he should press a key? I was just doing this to see how well the room generation worked, so this won't be in the actual game. > snip more code //lets try printing it: for (i=0; i<25; i++) { printf("\n"); for (j=0; j<25; j+=1) { Variety may be the spice of life but a consistent code style is a virtue. Think about coming back to this program after several months. Oops, I just lapsed into +=1 for a moment there. Anyway I still don't know how to move the cursor to print text to the screen at places, so if anyone can help me with this... I'll check out Rec.games.roguelike.development too, I didn't realize there was such a group so hopefully they'll have good information there. Oct 1 '06 #19

 P: n/a In article , jmcgill Eric Sosman wrote: > No: the range is 1.0 <= r <= 2.0. It seems likely thatthis was not jmcgill's intention, but that he garbled theusual formula r = rand() / (RAND_MAX + 1.0);... for a [0,1) uniform value. (Speculation: In his attemptto make it look fancier by adding unnecessary casts and way toomany parentheses, he lost track of which parens went with which.If so, jmcgill should probably avoid Lisp!) Help the OP with the questions, don't tear apart my attempt to help. You're new here, aren't you? Oct 1 '06 #20

 P: n/a On Sat, 30 Sep 2006 13:18:26 -0400, Eric Sosman Barry Schwarz wrote: >On Fri, 29 Sep 2006 22:52:16 -0700, jmcgill>>To get a double precision random number, you can do this, no guaranteesabout the distribution or anything.double r;r=( ((double) rand()) / (double) (RAND_MAX) + (double)(1.0) ); You don't need any of the casts. Debatable, considering "no guarantees." The code as itstands sets r to a value somewhere between 1.0 and 2.0, withboth extremes included. Remove the casts and it will set rto 1.0 most of the time and 2.0 rarely, never anything inbetween. >1.0 is already a double. Right. >The +operator will force RAND_MAX to be converted to double. Wrong: division "binds more tightly" than addition. All too true. I must remember to where my glasses when the parentheses nesting level exceeds 1. Remove del for email Oct 1 '06 #21

 P: n/a On Sun, 1 Oct 2006 21:42:15 +1300, "BWIGLEY" wrote: snip >Anyway I still don't know how to move the cursor to print text to thescreen at places, so if anyone can help me with this... I'll checkout Rec.games.roguelike.development too, I didn't realize there wassuch a group so hopefully they'll have good information there. Unfortunately, that is system specific. You will need to ask in a forum that discusses your system (hardware, OS, and run-time library). Remove del for email Oct 1 '06 #22

### This discussion thread is closed

Replies have been disabled for this discussion.