Thread: Beginner Help

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Jessycat,I tried doing that but it got complicated,also trying to put the game into separate functions it got complicated.

  2. #17
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    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 "

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Thanks alot,I think I know what you mean ,I wasnt sure to use a void function or a return function.

  4. #19
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    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 "

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    How can I remove the \n in the buffer.

  7. #22
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    getchar()

  8. #23
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Books, Beginner, MustHave
    By Zeusbwr in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2004, 05:14 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM