void to void to out of void jump

This is a discussion on void to void to out of void jump within the Game Programming forums, part of the General Programming Boards category; In my BotK game, i have a soundtrack. On the main menu, you can access 3 things 1: Play game ...

  1. #1
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    void to void to out of void jump

    In my BotK game, i have a soundtrack. On the main menu, you can access 3 things

    1: Play game
    2: Bgm test
    3: Exit

    I can get everything to work fine. When in the Bgm test, if you want to go to the main menu, you hit the button, then it closes.

    Look at my code and tell me how i can fix it. I've trieds everything, and all i can think is a Goto.

    But that wont work either. It wont compile.
    Attached Files Attached Files
    This war, like the next war, is a war to end war.

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    while loops are your friend. Avoid using a goto at all costs.
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    If you must resort to a goto I hardly believe that you've tried hard enough.
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  4. #4
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Trust me, abrege, i have tried. I even copied the entire game code and put it into the function. it still didn't work.
    If i use goto, i use code like this:
    Code:
    void test()
    {
    cout << "Testing\n";
    goto top;
    };
    
    top:
    cout << "top";
    and i get this error:

    line 4: top not defined, but declared
    This war, like the next war, is a war to end war.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I took a quick look at your code, and... BOY, does it need improvement. First of all, your indentation sucks. In fact, it doesn't exist at all. Indentation is good because it's easier to see at which depth (scope) the code is at. Take a look at these snippets:
    Code:
    int main()
    {
    switch(MyChoice)
    {
    case 'a':
    printf("You pressed A");
    break;
    default:
    printf("I don't know what you pressed, but it wasn't A");
    break;
    }
    }
    
    
    int main()
    {
       switch(MyChoice)
       {
          case 'a':
             printf("You pressed A");
             break;
    
          default:
             printf("I don't know what you pressed, but it wasn't A");
             break;
       }
    }
    Which one is easiest to read? The latter of course. As mentioned, it's easier to see at what depth the code is at and it's easier to detect missing brackets and such.

    The second thing I noticed in your code was your endlessly recursive function calls. You have your loop in this manner:
    Code:
    void Func1()
    {
       PlaySound(...);
       Func2();
    }
    
    void Func2()
    {
       //Notice, this is not valid code:
       Choice =  Wanna play again? 
    
       if(Choice == 'y')
       {
          Func1();
       }
    }
    This leads to endless recursion. If you don't play the sound too many times you will probably not notice it, but this structure is plain wrong. If run enough times, the stack will overflow and your program will crash.
    A while loop is a better choice:
    Code:
    while(Choise = 'y')
    {
       PlaySound();
    }
    Or somethinglike that.

    Also, in your post before this one, you're using goto to jump out of a function. This is also wrong, and will most liely lead to the same error as above.

    The third thing I could comment on is your usage of global variables. I'm not saying that they are wrong. I use globals too sometimes, like when storing game stats. However, I'd recommend that you make a structure like this:
    Code:
    typedef struct
    {
       int Health;
       int Xpos;
       int Ypos;
    }ENEMY;
    Then defining your objects like this:
    Code:
    ENEMY Zombie;
    ENEMY Lich;
    ENEMY Mum;
    To reduce the millions of global variables you have


    I hope you take this as a helping gesture rather than a complaint of your program
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    actually magos, magos.... MAGos... MaGoS... that just sounds cool..
    I was using typedef structs, regular structs and all that for my Mech text game. I had 200 enemies defined with it, 5 allied mechs types, 30 mech allies, 40 non mech allies, aircraft, artillery, comm channels, 2 weapons, rocket launcher and chaingun, and they would be change outside or inside the battles, and that was the only weapon you had to fight with, until you change it. Duh...

    Anyway, every thing stayed at ZERO, NULL, EINS - EINS, int STRUCTSTAT = 0.

    Well anyway, i had to undo 400 structures, and im back to int. The only problem is, the battle isn't working yet, and i dont understand why, i even INDENTED it. I see no errors, nor does the compiler.

    it will input, but then restart the loop. Its annoying, and im going off topic.

    the goto jump:
    A book i have said that it can jump into and out of voids, switch case, if, else if, while, you name it. A earlier program that used it i made worked fine. My first loop. then i got into while loops. For loops are still annoying me, as i get a lot of errors with them.

    I'll try structs again on my recoding of Mech Abttle 2050, and i cant think of a good title for it........ ill work on that...

    Ooh! I know! Magos the Mech! LOL.
    This war, like the next war, is a war to end war.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Eh, are you high on something?
    I didn't get a word of that reply...

    What do you mean by "everything stayed at zero"? Structures are only a way of managing huge amounts of data. Apart from that they are the same as normal variables.

    You know the notation to use them, right?:
    Code:
    typedef enum
    {
       Enemy_Tank,
       Enemy_Missile,
       Enemy_Infantry
    }ENEMYTYPE;
    
    typedef struct
    {
       ENEMYTYPE Type;
       int Xpos;
       int Ypos;
    }ENEMY;
    
    int main()
    {
       ENEMY Enemy1;
    
       //Set the data
       Enemy1.Xpos = 12;
       Enemy1.Ypos = 27;
       Enemy1.Type = Enemy_Tank;
    
       //Get some data
       cout << "The X position is " << Enemy1.Xpos <<< endl;
       
       return 0;
    }
    Simple!

    PS: Magos is a shortening of my 'real' name (what, do we have names too ). I also believe it's a greek word for something. Dunno what though.

    EDIT: Checked it out. Obviously it's plural for Wizards/Conjurers/Magicians
    Last edited by Magos; 03-10-2003 at 10:28 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Well, this isn't your problem, but it is kind of odd.
    Code:
    case '2' : PlaySound("Battle.wav", NULL, SND_FILENAME | SND_ASYNC);
    	musmenu(); //this one will get called
    	break;
    	musmenu(); //this one won't...EVER
    I don't think it matters if you remove the second musmenu() or not, but it's not helping.

    To answer your question, you can fix it with a while loop.
    Code:
    while(1)
    {
    	mainmen();
    	cin >> menu;
    	if (menu == '1')
    		//play the game
    	if (menu == '2')
    	{
    		//music menu
    	}
    	if (menu == '3')
    	{
    		//quit the game
    	}
    }
    It will help a lot if you move all of your game playing code to another function. Having it all in main() is pretty messy.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Magos: I thought the name was latin for Magician. Hehe. As for a Abr. of you whole name? I dont think i ever seen a name begining with Mago, 'cept magot, which i REEEEAAAALLLLYYYYY doubt thats your real name. But if it is, well, its the 21st century.

    Am i high? Whyy do yoi asssk? Oooff coousre iiim noot hiiiiggh. eh heh heh, he a hehehe hahahahHEHEHEHEHHAHAHEHAHEHAHE!
    LOL

    Im recoding the game in this way
    Code:
    /player mech
    typedef struct
    {
    char name[size];
    int y;
    int x;
    int gun;
    int hp;
    int maxhp;
    int armor;
    int ammo1;
    int ammo2;
    int ammo3;
    int maxammo1;
    int maxammo2;
    int maxammo3;
    } player;
    
    player user =
    {
     {'Dark Cobra', 0, 0, 1, 450, 450, 30, 500, 10, 30, 500, 10, 30,}
    }
    
    //friend mechs
    typedef struct
    {
     char name[size];
     int gun;
     int hp;
     int maxhp;
     int armor;
     int y;
     int x;
    } fmech;
    
    fmech cobra[1] =
    {
     {'Cobra', 1, 400, 400, 30, 0, 1}
    }
    and getting a buttload of errors. I will give the typedef enum a try... BTW, do mi need to include any special headers or Libs for that?

    Im asking because i think might have a typedef undefined error...

    Pianorain: A while loop will fix it? A WHILE LOOP????? Shoot, here i was, thinking i HAD a while loop on there... oh, and don't take my 'screamin' wrong..... not trying to be rude......

    Fordy: Thanks
    This war, like the next war, is a war to end war.

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    No, Using Enumerations won't require you to include any special
    headers or library's.

  11. #11
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    i meant typedef
    This war, like the next war, is a war to end war.

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by Blizzarddog
    and getting a buttload of errors. I will give the typedef enum a try... BTW, do mi need to include any special headers or Libs for that?


    No, you don't need them for Typedef's as well.

  13. #13
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    okay
    This war, like the next war, is a war to end war.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21