Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Game Loop - Need some help looping..

loop

  • Please log in to reply
33 replies to this topic

#1 Donovan

Donovan

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 633 posts
  • Location:Nevada
  • Learning:Java, Objective-C, PHP, JavaScript, Perl, PL/SQL, ActionScript, VBScript, Others

Posted 12 December 2008 - 11:15 AM

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.
  • 0

#2 MeTh0Dz

MeTh0Dz

    CC Leader

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1726 posts

Posted 12 December 2008 - 11:27 AM

A switch is not a loop.
  • 0

#3 Lance

Lance

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 270 posts

Posted 12 December 2008 - 12:10 PM


// 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;



}


  • 0

#4 Donovan

Donovan

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 633 posts
  • Location:Nevada
  • Learning:Java, Objective-C, PHP, JavaScript, Perl, PL/SQL, ActionScript, VBScript, Others

Posted 12 December 2008 - 12:29 PM

Thank You lance going to see if that works:)
  • 0

#5 MeTh0Dz

MeTh0Dz

    CC Leader

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1726 posts

Posted 12 December 2008 - 02:34 PM

Do not ever and I repeat ever use a goto in a situation such as that, in code like that.

Just NO.
  • 0

#6 Donovan

Donovan

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 633 posts
  • Location:Nevada
  • Learning:Java, Objective-C, PHP, JavaScript, Perl, PL/SQL, ActionScript, VBScript, Others

Posted 12 December 2008 - 03:13 PM

Thanks Lance the code works wonders:)
  • 0

#7 Lance

Lance

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 270 posts

Posted 12 December 2008 - 07:29 PM

You are welcome :)
  • 0

#8 MeTh0Dz

MeTh0Dz

    CC Leader

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1726 posts

Posted 12 December 2008 - 07:33 PM

Also Bjarne explicitly says to not use do-while loops whenever possible.
  • 0

#9 Lance

Lance

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 270 posts

Posted 13 December 2008 - 06:36 AM

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.
  • 1

#10 MeTh0Dz

MeTh0Dz

    CC Leader

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1726 posts

Posted 13 December 2008 - 06:45 AM

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.

  • -1

#11 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 13 December 2008 - 11:55 AM

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.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#12 MeTh0Dz

MeTh0Dz

    CC Leader

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1726 posts

Posted 13 December 2008 - 12:14 PM

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





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download