Thread: GoTo's?

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    GoTo's?

    I've wanted to make a text based C++ game for a while now, and i've slowly started planning the basics for it now.

    One of the things i'd like is some kind of save/load function. So every once in a while, the program will ask the user if he/she wants to save the game, if yes, the program will output a code to a text file.

    At the start of the game, the user will then have a choice of loading the last game, and the program will fetch the code and compare it to some hard coded codes that represent the players progress in the game, this is where i got stuck.

    My plan was to make my own branch table and then use GoTo statements to jump to the saved location in the game. Something along the lines of this:

    Code:
    switch(savedgame)
    {
       case LEVEL1:
       goto lvl1;
       break;
       
       case LEVEL2:
       goto lvl2;
       break;
    ...
    And so on...

    My question is then: Would this be considered poor programming? I've heard alot about GOTO, and how bad it is. But in this case, it doesn't really produce spaghetti code, actually, i kind of like my save/load function implementation, and i have a hard time figuring out another way of doing it.

    One way could be to base the entire game on a linked list, so the first lvl would be in the first node of the list, and so on. And then just traversing through the list until the codes in the save game files matched a certain variable in my list, but i don't like this solution as much as the other one.

    What else could i do? And do you think this qualifies as a good place to use GOTO?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I'd use external files as well as a naming convention like level1.lvl, level2.lvl ... And then simply read that file for loading the level...

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    I'm not sure how you think goto will help you. i certainly can't see any reason to use it based on your description of the problem.

    How about wrapping the code for each level inside its own function, then you could have a switch statement which looks like this
    Code:
    switch (savedgame)
    {
        case LEVEL1:
        lvl1();
        break;
    
        case LEVEL2:
        lvl2();
        break;
    }

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Personally I would make a generic load function, that accepts a level name or number as a parameter. Doing that you could avoid using goto's and probably cut out some repeating code.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Neo1 View Post
    Snip - <Irrelevant background information>

    My plan was to make my own branch table and then use GoTo statements to jump to the saved location in the game. Something along the lines of this:

    Snip - <Evil code that used the banished 'goto' word>
    And so on...

    Snip - <Attempts at justifying use of the banished 'goto' word>

    What else could i do? And do you think this qualifies as a good place to use GOTO?
    Use function calls instead.

    The best place to use a 'goto' is to the left of the cursor, when the backspace key is being held down.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gotos
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 10-21-2007, 10:28 PM
  2. Time limit on cin
    By Queatrix in forum C++ Programming
    Replies: 11
    Last Post: 04-13-2005, 01:56 PM
  3. C++ vs Java
    By Moni in forum C++ Programming
    Replies: 19
    Last Post: 03-23-2003, 04:19 PM
  4. Hehe, look at this code. (Not looking for help)
    By SinAmerica in forum C++ Programming
    Replies: 22
    Last Post: 05-04-2002, 09:30 PM
  5. if, then, and gotos
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2002, 05:56 AM