Thread: cin.get() Pausing Help

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    26

    Post cin.get() Pausing Help

    I recently finished a tutorial for the creation of a CRAPS game. However, the finished game was hard to follow because the text in the console got longer and longer, and no matter how well formatted, it just didn't look good.

    I decided to try to clear the screen at the end of the main loop, so that it would have a clear screen for the next pass. (Sorry if any of this is hard to follow, I'm not really sure about some of the terminology) Obviously, for this to work I needed to pause the game at the end of the loop (before clearing the screen), otherwise the player would never see the outcome of his actions.

    I did a bit of googling, and the simplest answer (although not necessarily the best, from what I've read) was to use cin.get() to pause.

    Unfortunately, this has the irritating side effect of letting the player type random junk into the console at the end of each loop. Normally this would just be an aesthetic issue, but apparently passing more than the single enter key seems to use the extra entries to fill the next few "cin >> variable_name" calls.

    What would be the best way to freeze the screen until the user presses [enter]? If possible, I'd like to be able to detect the key they press, so I can exit on [esc]...

    Any references or built in functions for me to look into would be great,
    Thank you for your time,
    Timothy S.

    [EDIT]: A little searching revealed the line "cin.ignore(numeric_limits<streamsize>::max(), '\n');" which fixed the problem related to having extra characters typed before hitting enter during pauses, but I'd still like to know if there's a better way to pause, especially one that would let me retrieve the key that was pressed to un-pause. My apologies for not searching more before posting, I should have been able to find this the first time... Unfortunately, adding this appears to make the program require that enter be pressed twice at some pauses...

    main.cpp
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    #include "header.h"
    using namespace std;
    
    int main()
    {
        unsigned long MoneyEarned;
        unsigned long Money;
    
        short DiceValue;
        short Bet;
        short BetMoney;
    
        /* Show Info Screen and Init Game */
        ShowIntroScreen();
        cout << "Press [ENTER] to begin...";
        Pause();
        ClearScreen();
        Money = 1000;
    
        /* While the player has more than 100 dollars... */
        do
        {
            ShowInformation(Money);
    
            Bet=GetBet();
            BetMoney=GetAmount();
            DiceValue=DoDiceThrow();
            MoneyEarned=DoMoneyCalc(DiceValue, Bet, BetMoney);
    
            Money-=BetMoney;
    
            cout << "The bet: $" << BetMoney << " on ";
                switch(Bet)
                {
                    case 1:
                        cout << "6 and 8.";
                        break;
                    case 2:
                        cout << "4 and 10.";
                        break;
                    case 3:
                        cout << "2 and 12.";
                        break;
                }
            cout << endl << "The dice: " << DiceValue << endl << endl;
    
            if(MoneyEarned == 0)
            {
                cout << "You lost $" << BetMoney << "." << endl << endl;
            }
            else
            {
                cout << "You won $" << MoneyEarned-BetMoney << ". ";
                cout << "($" << MoneyEarned << " Won - $" << BetMoney << " Bet)" << endl << endl;
                Money+=MoneyEarned;
            }
            cout << "Press [ENTER] to continue...";
            Pause();
            Pause();
            ClearScreen();
        }
        while(Money>100);
    
        cout << "You are down to your last $100, which you save for the ride home." << endl;
    
        return 0;
    }
    header.cpp (header.h contains only the function definitions)
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    #include "header.h"
    
    using namespace std;
    
    /* FUNCTION DEFINITIONS */
    void Pause(void)
    {
        cin.get();
    }
    
    void ClearScreen(void)
    {
        system("cls");
    }
    
    void ShowIntroScreen(void)
    {
        cout << "   Welcome to Craps 1.0" << endl << endl;
        cout << "Rules: " << endl;
        cout << "You start with $1000 to bet." << endl;
        cout << "There are three possible bets: " << endl;
        cout << "2 and 12 (Win Ratio 5:1)" << endl;
        cout << "4 and 10 (Win Ratio 2.5:1)" << endl;
        cout << "6 and 8 (Win Ratio 1.5:1)" << endl << endl;
        cout << "You can bet between $10 and $100 each round." << endl << endl;
    }
    
    void ShowInformation(unsigned long Money)
    {
        cout << "CRAPS 1.0" << endl << endl;
        cout << "Money:  $" << Money << endl << endl;
    }
    
    short GetBet(void)
    {
        unsigned short BetType;
    
        cout << "Type of Bet (1 - 6/8, 2 - 4/10, 3 - 2/12): ";
        cin >> BetType;
        if ((BetType==1) || (BetType==2) || (BetType==3))
        {
            return BetType;
        }
        else
        {
            return 1;
        }
    }
    
    short DoDiceThrow(void)
    {
        short DiceValue;
    
        srand(time(NULL));
        DiceValue = (rand() % 6) + 1;
        DiceValue += (rand() % 6) + 1;
    
        return DiceValue;
    }
    
    unsigned short DoMoneyCalc(short Dice, short Bet, short BetMoney)
    {
        unsigned long MoneyEarned;
    
        switch(Bet)
        {
            case 1:
                if ((Dice==6) || (Dice==8))
                {
                    MoneyEarned=BetMoney*1.5;
                }
                else
                {
                    MoneyEarned=0;
                }
                break; break;
            case 2:
                if ((Dice==4) || (Dice==10))
                {
                    MoneyEarned=BetMoney*2.5;
                }
                else
                {
                    MoneyEarned=0;
                }
                break; break;
            case 3:
                if ((Dice==2) || (Dice==12))
                {
                    MoneyEarned=BetMoney*5;
                }
                else
                {
                    MoneyEarned=0;
                }
                break;
            /*default:
                MoneyEarned=0;
                cout << "LOST!";
                break;*/
        }
    
        return MoneyEarned;
    }
    
    unsigned long GetAmount(void)
    {
        unsigned short BetAmount;
    
        cout << "Bet Amount ($10-$100): ";
        cin >> BetAmount;
    
        if(BetAmount < 10)
        {
            BetAmount=10;
        }
        if(BetAmount > 100)
        {
            BetAmount=100;
        }
    
        return BetAmount;
    }
    Last edited by timmeh; 09-05-2009 at 01:25 PM.

  2. #2
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    If cross-platform isn't a concern and you're using Windows, you can use getche() in conio.h.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    26
    Quote Originally Posted by yaya View Post
    If cross-platform isn't a concern and you're using Windows, you can use getche() in conio.h.
    It isn't, and that works, thanks!

    Just to make sure, getch() returns an int equal to the ASCII code for the key pressed, correct? Is that a decimal ASCII code, a hex ACSII code or am I completely off?

    Thanks again!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It returns an ASCII code. (There are no such things as "decimal ASCII" or "hex ASCII", just like we don't talk about "decimal int" or "hex int". An integer is an integer, and whether you write it in decimal, hex, or cuneiform doesn't matter.)

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    26
    Quote Originally Posted by tabstop View Post
    It returns an ASCII code. (There are no such things as "decimal ASCII" or "hex ASCII", just like we don't talk about "decimal int" or "hex int". An integer is an integer, and whether you write it in decimal, hex, or cuneiform doesn't matter.)
    Sorry, I worded that wrong. Perhaps it would be best if I came up with an example, since I don't know the correct terminology.

    If the player pressed [L] would it return 76 or 4C or does it matter?

    Thanks!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by timmeh View Post
    Sorry, I worded that wrong. Perhaps it would be best if I came up with an example, since I don't know the correct terminology.

    If the player pressed [L] would it return 76 or 4C or does it matter?

    Thanks!
    It would return both, as 76 and 0x4C are the same thing.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    26
    Quote Originally Posted by tabstop View Post
    It would return both, as 76 and 0x4C are the same thing.
    Okay, I understand now, thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get(); not working with my switch statement
    By tenor_jazz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2008, 10:33 PM
  2. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  3. Confused about cin.get(); and classes.
    By RaccoonKing in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2005, 11:44 AM
  4. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 07:51 AM
  5. Problems using while loop with cin.get()
    By Arooj in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2004, 01:58 AM