Jessycat,I tried doing that but it got complicated,also trying to put the game into separate functions it got complicated.
Jessycat,I tried doing that but it got complicated,also trying to put the game into separate functions it got complicated.
Do the following 4 things:
1) rename int main(void) to int play_game(void)
2) add the main() function I posted to your program file
3) move the system("pause"); from the play_game() function to the main function right before the return 0;
4) put the play_game() function prototype (int play_game();) right after the #includes
That's the easiest way for right now, and it separates your game from the rest of the program. If you make any changes to how your game works, the main() function doesn't have to change at all, and changes to the main() function won't affect the play_game() function either. Keeping things separated like this makes it easier to make changes without messing things up.
For example, if you want to put up a title screen before you actually get into the game, you could write a function to do that and have your main() call it before going into the while loop that calls play_game().
There are probably a lot of other changes you might want to make, but I am only addressing "going back to the top without goto" here.
Programming can get very complicated. Don't let that discourage you though.
abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "
Thanks alot,I think I know what you mean ,I wasnt sure to use a void function or a return function.
That's actually one of the changes I was talking about. play_game() doesn't need to return anything, so it should probably be void play_game().
abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "
scanf("%c", &yn);
leaves \n in the input stream, so next time it will read it, instead of waiting of user input...
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
How can I remove the \n in the buffer.
getchar()
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler