Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

In need of some clarity...

StephenCStephenC Posts: 7Member
Hey everyone. Okay, I've been digging through a multiplicity of books, watching videos, and reading beginner tutorials on websites. I think that perhaps one of the most confusing aspects of C++ for me is one man's definition of "beginner" when compared to another's. Also, when people say "you can do this but it isn't really used anymore" and then they fail to give an example of what WOULD be used to substitute for what can be done but "isn't anymore," it leaves much to be desired. I'm approaching learning C++ from the perspective of game development, by the way. I'm starting a game dev. program at a local community college in January, so I'm trying to get into this stuff before I HAVE to.

In my simple little "game" below, the "player" has to guess a number less than 100. You can read the logic and see exactly which numbers yield success and which do not. My question is, if the person gets it wrong, I've got it set up for the program to take them back to a spot I've labeled "beginning". Of all I've read and watched, I've yet to see anyone note anything about goto other than "this isn't used much anymore." If that's the case, then if I were to use it, would it be "looked down upon" as something ancient? If so, what the heck should I use instead of goto? I've tried creating a more complex application -- a virtual soda machine -- and in the application, I have a lot of switches with cases that end with a goto to send the person back to a specific point to re-enter some type of data. Is there a more "correct" or beneficial way of achieving goto?

Example:

#include

using namespace std;

int main()
{
int number = 0;

beginning:

cout << "Please enter an integer under 100. If you guess the right range,"
<< " you win: " ;

cin >> number;

if ((number >= 56) && (number <= 78))
{
system("COLOR 1f");
cout << "
You Win!!!
" << endl;
}

else
{
cout << "
You LOSE!!! Try again...
" << endl;
system("pause");
system("cls");
goto beginning;
}

system("pause");

return 0;
}

One more quick question... how would I go about making the console change colors every second while sitting on a cout of something like "YOU WIN!!"?

Thanks for your time.

Comments

  • MT2002MT2002 Posts: 1,444Member

    : In my simple little "game" below, the "player" has to guess a number
    : less than 100. You can read the logic and see exactly which numbers
    : yield success and which do not. My question is, if the person gets
    : it wrong, I've got it set up for the program to take them back to a
    : spot I've labeled "beginning". Of all I've read and watched, I've
    : yet to see anyone note anything about goto other than "this isn't
    : used much anymore." If that's the case, then if I were to use it,
    : would it be "looked down upon" as something ancient? If so, what the
    : heck should I use instead of goto? I've tried creating a more
    : complex application -- a virtual soda machine -- and in the
    : application, I have a lot of switches with cases that end with a
    : goto to send the person back to a specific point to re-enter some
    : type of data. Is there a more "correct" or beneficial way of
    : achieving goto?
    :
    : Example:
    :
    : #include
    :
    : using namespace std;
    :
    : int main()
    : {
    : int number = 0;
    :
    : beginning:
    :
    : cout << "Please enter an integer under 100. If you guess the
    : right range,"
    : << " you win: " ;
    :
    : cin >> number;
    :
    : if ((number >= 56) && (number <= 78))
    : {
    : system("COLOR 1f");
    : cout << "
    You Win!!!
    " << endl;
    : }
    :
    : else
    : {
    : cout << "
    You LOSE!!! Try again...
    " << endl;
    : system("pause");
    : system("cls");
    : goto beginning;
    : }
    :
    : system("pause");
    :
    : return 0;
    : }
    :
    : One more quick question... how would I go about making the console
    : change colors every second while sitting on a cout of something like
    : "YOU WIN!!"?
    :
    : Thanks for your time.

    Im writing a large game (at 25000+ lines), and have yet to use a goto :)

    Useually if you design your structure around routines and classes,
    there is no need for goto.

    The reason goto is looked down upon , is that it produces Spaghetti code.
    This makes programs *much* harder to maintain, and increases complexity
    alot.

    As for changing colors, there are alot of ways to do it.
    As you are already using system(), Im assuming portability
    isnt a concern.

    If so, just use the command console's color command:
    [code]
    Sets the default console foreground and background colors.

    COLOR [attr]

    attr Specifies color attribute of console output

    Color attributes are specified by TWO hex digits -- the first
    corresponds to the background; the second the foreground. Each digit
    can be any of the following values:

    0 = Black 8 = Gray
    1 = Blue 9 = Light Blue
    2 = Green A = Light Green
    3 = Aqua B = Light Aqua
    4 = Red C = Light Red
    5 = Purple D = Light Purple
    6 = Yellow E = Light Yellow
    7 = White F = Bright White

    If no argument is given, this command restores the color to what it was
    when CMD.EXE started. This value either comes from the current console
    window, the /T command line switch or from the DefaultColor registry
    value.
    [/code]

    So...[code]
    system ("color fc");
    [/code]
    Will produce red text on a white background.

    With some compiliers, you can also use [b]settextcolor()[/b]
    from [b]conio.h[/b], Or the System API.

    Whatever you choose, note there is no ANSI compatible way;
    ie it is system dependent.
  • MT2002MT2002 Posts: 1,444Member

    Heres a new version of your code. It doesnt have goto, and
    flashes the colors when the player wins :-)

    [code]
    #include
    #include // NON PORTABLE

    using namespace std;

    int main()
    {
    int number = 0;
    bool bDone=false;

    //beginning:

    do {

    cout << "Please enter an integer under 100. If you guess the right range,"
    << " you win: " ;

    cin >> number;

    if ((number >= 56) && (number <= 78))
    {

    cout << "
    You Win!!!
    " << endl;

    // Cheap method :) -- blink text until player presses a key. NON PORTABLE
    while (!kbhit()) {
    for (int i=0;i<5;i++) system("COLOR 1f");
    for (int i=0;i<5;i++) system("COLOR f1");
    }

    bDone=true;
    }

    else
    {
    cout << "
    You LOSE!!! Try again...
    " << endl;
    system("pause");
    system("cls");
    }
    }while (!bDone);

    system("pause");

    return 0;
    }
    [/code]
  • StephenCStephenC Posts: 7Member
    Gotcha. Thanks for the information!
Sign In or Register to comment.