theres no diffence i just added the \nCode:char menu(void){ char x; printf("\nyou want to do this y, n:"); scanf("%c",&x); return x; } int main (void) { char play; while(1) { play=menu(); if(play=='n') break; } return 0; }
theres no diffence i just added the \nCode:char menu(void){ char x; printf("\nyou want to do this y, n:"); scanf("%c",&x); return x; } int main (void) { char play; while(1) { play=menu(); if(play=='n') break; } return 0; }
Don't you have at least #include <stdio.h> , etc., at the top of your program?
ya thats in there just missed coping it.Code:#include <stdio.h> #include <stdlib.h> char menu(void){ char x; printf("\nyou want to do this y, n:"); scanf("%c",&x); return x; } int main (void) { char play; while(1) { play=menu(); if(play=='n') break; } return 0; }
This is what you want for the menu logic, I believe. Note that it will not play a game until it gets the game playing function call added into the menu function.
Code:#include <stdio.h> char menu(void) { char yesno; printf("\nyou want to do this? (y/n): "); scanf("%c", &yesno); return yesno; } int main (void) { char play; printf("\n\n\n\n\n\n"); while(1) { play=menu(); getchar(); /* removes newline from input buffer This is a problem with scanf(), which is why it's not used much. Next time you look at this program, ask about fgets() - it is the only good way to get input from a user in C, but slightly too complicated for right this second. */ if(play =='n') break; /* else new_game(); game playing function call, goes here */ } printf("\n\n Program Complete - Press Enter "); getchar(); /* wait for the key press */ return 0; }
Last edited by Adak; 11-06-2007 at 12:45 AM.
nice it worked thanks, now if i put it all together
Code:#include <stdio.h> #include <stdlib.h> void script (int room_num){ if (room_num==0){ printf("\nroom0"); } if (room_num==1){ printf("\nroom1"); } if (room_num==2){ printf("\nroom2"); } if (room_num==3){ printf("\nroom3"); } if(room_num==-1){ printf("\nthe walls are acid"); return 0; } } int next_room(unsigned int a, unsigned int b) { static const int map[4][4] = { {1, 2,-1,-1}, {2, 0,-1,-1}, {0, 1,-1, 3}, {-1,-1,2,-1} }; return map[a][b]; } int conversion (char letter){ switch(letter) { case 'n': return 0; case 's': return 1; case 'e': return 2; case 'w': return 3; default: printf("No!, After wandering for hours you figure out %c is not a actual direction and end up walking into the slabbering fangs of a lurking Grue.\n", letter); return 4; } } char menu(void){ char x; printf("\nyou want to do this y, n:"); scanf("%c",&x); return x; } \\\so i would be adding another function here? like.. int game(int start _room);{ int start_room=current_room; return current_room } int main (void) { char letter; char play; int current_room; while(1) { play=menu(); getchar(); if(play=='n') break; \\\then i add int game here like so: the int game function updates the current room from the first loop to the second. i hope i am doign it right. int game(current_room); \\\or do i add another while loop within the first one? printf("direction:"); scanf("%c",&letter); int letter_num=conversion(letter); int room_num=(next_room(current_room,letter_num)); if(letter_num==4) break; script (room_num); } return 0; }
Usually, you have an outer "menu & game loop" that you go through once only, for each complete game (or until the player presses the Q to quit).
Then, inside the functions that the outer loop calls, will be other loops that might address the player's movements from room A to room B, then back to room A, then room C, whatever.
These secondary loops should be in the next level down from the main "menu & game" loop. You want to distribute the loops so there's no big headache to debug them, later.
Two considerations:
1. Using the "number of choices + 1" as indication of failure is not a particularly good plan - especially if it's a "magic number" [just a number, rather that a constant declaration]. Using either an "impossible number" or a "hugely outside the range" number is a much better plan. So don't use 4 as your indication of "you pressed the wrong key", but perhaps either -1 or say 1000. What happens if you feel like implementing further directions, such as up, down, NorthWest, SouthEast, etc, etc.
2. The getchar() to get rid of the newline should really be part of "menu" rather than stuffed after menu() - this is because it fixes a side-effect of the actual reading of the choice using scanf() - if at some future point, you change it to use for example fgets(), then you don't need the getchar(). It is also bad style to rely on some other part of the code to clean up after a particular function - if it's at all possible, the function should make sure that it exits in a "clean" state.
3. You probably also want this:
as it suffers from the same thing as your "menu()".Code:printf("direction:"); scanf("%c",&letter); getchar();
4. This can end up "out of range":
It letter_num is 4 in the above line, next_room will be called with a 4, which is out of bounds for the array, and there is no checking for that. The red parenthesis are un-necessary. I'm all for extra parenthesis when they clarify the code, but I don't see these doing that.Code:int room_num=(next_room(current_room,letter_num)); if(letter_num==4) break;
5. Declaring local variables "as you need them" [as in #4 above] is a C++ technique and non-standard in "plain" C. It's better style to declare the variables in the beginning of the function.
Please don't take this criticism as "you are crap", but rather as "this is things you can improve on, and learn from, so that you become a better programmer".
--
Mats
Last edited by matsp; 11-06-2007 at 04:42 AM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
i am not sure what this means, dose it mean there should be loops in my functions?
thanks matsp i think i did most of what you said, i think?
but this is how i have it now and it comes with an parse error befor ";" in the for loop. i dident know how to update the while loop so i am trying a for loop.
Code:#include <stdio.h> #include <stdlib.h> void script (int room_num){ if (room_num==0){ printf("\nroom0\n"); } if (room_num==1){ printf("\nroom1\n"); } if (room_num==2){ printf("\nroom2\n"); } if (room_num==3){ printf("\nroom3\n"); } if(room_num==-1){ printf("\nthe walls are acid\n"); return 0; } } int next_room(unsigned int a, unsigned int b) { static const int map[4][4] = { {1, 2,-1,-1}, {2, 0,-1,-1}, {0, 1,-1, 3}, {-1,-1,2,-1} }; return map[a][b]; } int conversion (char letter){ switch(letter) { case 'n': return 0; case 's': return 1; case 'e': return 2; case 'w': return 3; default: printf("No!, After wandering for hours you figure out %c is not a actual direction and end up walking into the slabbering fangs of a lurking Grue.\n", letter); return -2; } } char menu(void){ char x; printf("\nyou want to do this y, n:"); scanf("%c",&x); getchar(); return x; } int main (void) { char letter; char play; int current_room; int room_num; int letter_num; /*while(1) { play=menu(); if(play=='n') break; } */ i commented the menu out because i did not whant to deal with it.but it works just fine. while(1){ printf("direction:"); scanf("%c",&letter); getchar(); for( letter_num=conversion(letter), room_num=next_room(current_room,letter_num);1; current_room=room_num;) { if(letter_num==-2) break; } script (room_num); } return 0; }
Last edited by deadherorising; 11-09-2007 at 01:43 AM.
This code is all wrong:
You don't need a for-loop here - and this particular loop definitely doesn't work - it spins around forever doing very little. The check for "letter_num == -2" is after you call next_room [not to mention that it breaks only the for-loop - which will only happen if an invalid direction was given before the loop was entered].Code:for( letter_num=conversion(letter), room_num=next_room(current_room,letter_num);1; current_room=room_num;) { if(letter_num==-2) break; }
I would remove the for-loop, and move the check for -2 to before the next_room call. [Calling next_room with -2 as letter_num will cause bad index in your variable, and you'll get some "random" room number from that].
The variable room_num can be replaced with current_room - no need for the two variables in this case.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
it works i got nothing to say exsept thanks. and are there any problems with it.
Code:#include <stdio.h> #include <stdlib.h> void script (int current_room){ if (current_room==0){ printf("\nroom 0\n"); } if (current_room==1){ printf("\nroom 1\n"); } if (current_room==2){ printf("\nroom 2\n"); } if (current_room==3){ printf("\nroom 3\n"); } if(current_room==-1){ } } int next_room(unsigned int a, unsigned int b) { static const int map[4][4] = { {1, 2,-1,-1}, {2, 0,-1,-1}, {0, 1,-1, 3}, {-1,-1,2,-1} }; return map[a][b]; } int conversion (char letter){ switch(letter) { case 'n': return 0; case 's': return 1; case 'e': return 2; case 'w': return 3; default: printf("No!, After wandering for hours you figure out %c is not a actual direction and end up walking into the slabbering fangs of a lurking Grue.\n", letter); return -2; } } char menu(void){ char x; printf("\nYou sure you want to do this y, n:"); scanf("%c",&x); getchar(); return x; } int main (void) { char letter; char play; int current_room; int letter_num; int last_room; while(1) { play=menu(); if(play=='n') break; else{ printf("to exit press any key exsept n,s,e,w. you start in room 0\n"); } while(1){ printf("direction:"); scanf("%c",&letter); getchar(); letter_num=conversion(letter); if(letter_num==-2) break; last_room=current_room; current_room=next_room(current_room,letter_num); if (current_room==-1){ printf("\nthe walls are acid\n"); current_room=last_room; } script (current_room); } } return 0; }
Wandering "into the slabbering fangs of a lurking Grue" and exiting is a little harsh just for pressing the wrong key, don't you think? Why not print an error message for every key except q or something? (Or perhaps, because 'q' is right next to 'w' for west, some other key.)
If you pass in the string to print, and rename it, that function could be used in at least one other place.Code:char menu(void){ char x; printf("\nYou sure you want to do this y, n:"); scanf("%c",&x); getchar(); return x; }
In script(), it looks like you could use a switch statement. Or a lookup table, implemented as an array of strings for the descriptions.
Let's see . . . your indentation could also be improved.Code:const char *message[] = { "room 1", "room 2", /* ... */ }; if(current_room >= 0) puts(message[current_room]);
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.