Jump to content

Game Loop - Need some help looping..

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
33 replies to this topic

#1
Donovan

Donovan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 798 posts
The c++ book I have is "Beginning C++ Through Game Programming Second Edition" and it has explaint various looping ways like do, while, and switch.

I have even looked up the goto statement but that will not do what I need the program to do.


So here is the Program / Source code I am working on.



// Menu Chooser


#include <iostream>

using namespace std;


int main()

{

    cout << "Difficulty Levels\n\n";

    cout << "1 - Easy\n";

    cout << "2 - Normal\n";

    cout << "3 - Hard\n";


    int choice;

    cout << "Choice: ";

    cin >> choice;


    switch (choice)

    {

    case 0:

                cout << "Good Bye!";

                system("PAUSE");

                break;

    case 1:

                cout << "You picked Easy.\n";

                system("PAUSE");

                break;

    case 2:  

                cout << "You picked Normal.\n";

                system("PAUSE");

                break;

    case 3:

                cout << "You picked Hard.\n";

                system("PAUSE");

                break;

    default:

               cout << "Please choose between Easy, Normal and Hard only.\n";

               system("PAUSE");

               break;

    }

    

    return 0;


}  



Alright, Now if you are already a coder you know that this will not loop after pressing either of the options.

What I want to program to do is the following.


Console Display:

Difficulty Levels

0 - Exit
1 - Easy
2 - Normal
3 - Hard

Choice: 1

Press any key to continue...
// program clears screen then loops to the top.


Difficulty Levels

0 - Exit
1 - Easy
2 - Normal
3 - Hard

Choice: |

// At this point of the program it does a system("PAUSE"); then a system("CLS") then loops back at the top. So 1, 2, 3 should display what level difficulty they picked 0 should exit the program. There also needs to be an if statement that says if (choice > 3) display "Please choose between Easy, Normal, and Hard only.
Posted Image
+Friend Me | My Graphics | Forum Rules | Help Forum | Forum FAQ

#2
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
A switch is not a loop.

#3
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
// Menu Chooser

#include <iostream>
using namespace std;

int main()
{

    int choice; // this line is moved out even it's not strictly necessary.

    /* code inserted by lance*/
    do{

        cout << "Difficulty Levels\n\n";
        cout << "1 - Easy\n";
        cout << "2 - Normal\n";
        cout << "3 - Hard\n";

        cout << "Choice: ";
        cin >> choice;

        switch (choice)
        {
        case 0:
                    cout << "Good Bye!";
                    system("PAUSE");
                    goto _done;
        case 1:
                    cout << "You picked Easy.\n";
                    system("PAUSE");
                    break;
        case 2:
                    cout << "You picked Normal.\n";
                    system("PAUSE");
                    break;
        case 3:
                    cout << "You picked Hard.\n";
                    system("PAUSE");
                    break;
        default:
                   cout << "Please choose between Easy, Normal and Hard only.\n";
                   system("PAUSE");
                   break;
        }
    }while(choice!=0);
_done:
    return 0;

}


#4
Donovan

Donovan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 798 posts
Thank You lance going to see if that works:)
Posted Image
+Friend Me | My Graphics | Forum Rules | Help Forum | Forum FAQ

#5
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Do not ever and I repeat ever use a goto in a situation such as that, in code like that.

Just NO.

#6
Donovan

Donovan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 798 posts
Thanks Lance the code works wonders:)
Posted Image
+Friend Me | My Graphics | Forum Rules | Help Forum | Forum FAQ

#7
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
You are welcome :)

#8
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Also Bjarne explicitly says to not use do-while loops whenever possible.

#9
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
And use it whenever it's logical. Why else it's there in the language at all?

But seriouly, can you elaborate on the bads of this language feature?

BTW, I wouldn't hesitate to use goto when I see reasonable.

#10
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Lance it's obvious that you don't know much about the language through a paradigm standpoint so saying that you wouldn't hesitate to do something doesn't mean much.

Where it's logical? It makes much more sense to just use a while loop and eliminate your do-while and jmp. Also stop acting like it's cool to disagree with Bjarne. It's annoying.

Also that is terrible reasoning. There are tons of things in C++ you shouldn't use. Macros, C style strings (usually), C syle [File]IO, void pointers, etc.

Edited by xXHalfSliceXx, 13 December 2008 - 04:09 PM.


#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
NEVER use goto! If you can give me a concrete of where goto is the best solution, I'd love to see it, and then I'd tell you "NEVER use goto!" Goto is a holdover from the bad old days of BASIC, and only has any use in a language where structured programming is not possible. There is never a situation where goto is necessary (mathematically proven). Don't do it, don't encourage it, don't make it sound acceptable.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#12
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
The only places that goto[s] are considered acceptable (generally) are in deeply embedded expressions where they allow easy access to an exit routine.