1

I'm starting to write a video poker program and I'm running into some issues.

I have a Hold function as you can see below:

void Game::Hold( bool& choice )
{
    if( choice == true )
        {
            Console::BackgroundColor(Red);
            Console::ForegroundColor(Black);
            cout << "HOLD";
            Console::BackgroundColor(Black);
            Console::ForegroundColor(Red);
        }
        else
            cout << "HOLD"; 
}

This functions allows me to block out the text so the player knows which cards are selected and which are not. The problem I'm having is that the first and last "Holds" wont block off if being held.

So far-this is my code that is calling the Hold function:

void Game::Play( void )
{

    Menu();

    Console::Clear();
    Deck nGame;
    nGame.Shuffle();
    Game Hand;
    Card currentHand[ 5 ]; 
    bool p_Hold[ 5 ] = { 0 , 0 , 0, 0, 0 };


    for( int i = 0; i < 5; i++ )
        currentHand[ i ] = nGame.Draw();

    cout << "Type in which cards you would like to hold. Type \"d\" when done.\n\n";
    char uChoice[ 5 ] = {};

    for( int i = 0; i < 5; i++ )
    {
        if( uChoice[ i ] == 'd' ) 
            break;

        for( int i = 0; i < 5; i++ )
            cout << "    " << currentHand[ i ] << "   ";

        cout << endl;

        for( int i = 0; i < 5; i++ )
        {
            cout << "  ";
            Hand.Hold( p_Hold[ i ] ); 
            cout << "   ";  
        }

        cout << "\n\n\nWould you like to hold Card " << i + 1 << "? (1 = Yes/0 = No): ";

        cin.get( uChoice[ i ] );
        cin.clear();

        cin.ignore( INT_MAX, '\n' );

        cout << endl;

        if( cin.good() )
        {
            for( int i = 0; i < 5; i++ )
            {
                if( uChoice[ i ] == '1' )
                    p_Hold[ i ] = true;
                else
                    p_Hold[ i ] = false;
            }
        }
    }
}
Kara
  • 6,115
  • 16
  • 50
  • 57
Josh Lake
  • 567
  • 1
  • 6
  • 17

2 Answers2

1

You haven't shown what Console::BackgroundColor() actually does, so it's hard to be certain. However...

Normally cout buffers its output until later. Because of this, the Console::BackgroundColor() might be changing something that takes effect right away, then the cout << "HOLD" is buffered, then you reset the color before the "HOLD" text gets a chance to be sent to the console.

Perhaps you need to flush the output immediately before changing the color:

void Game::Hold( bool& choice )
{
    if( choice == true )
        {
            cout.flush();
            Console::BackgroundColor(Red);
            Console::ForegroundColor(Black);
            cout << "HOLD";
            cout.flush();
            Console::BackgroundColor(Black);
            Console::ForegroundColor(Red);
        }
        else
            cout << "HOLD"; 
}
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • The console isnt the problem. Ive been using that same class for almost every console application I write. The problem resides in the for loop not allowing me to show the first and last "true" HOLD in the sent array – Josh Lake Dec 06 '11 at 18:32
  • I'm afraid I still don't understand. If `Game::Hold()` isn't the problem, could you describe what *is* the problem? I don't understand what you mean by "true HOLD". Also, maybe you can update your question title too. My suggested change was apparently not helpful. – Greg Hewgill Dec 06 '11 at 18:39
  • If I select '1' everytime it asks me to Hold, the text will not change for the 1st and last HOLD but will change for the 2nd, 3rd, and 4th. – Josh Lake Dec 06 '11 at 18:42
  • Well I can't immediately see why that would be a problem. I don't see any obvious off-by-1 errors in your code. However, I might suggest printing out the contents of various arrays like `p_Hold` before and after modifying it to see that it contains what you believe it should. – Greg Hewgill Dec 06 '11 at 18:46
  • http://imgur.com/dnio6 - as you can see. First and last not displaying. First card also doesn't hold. – Josh Lake Dec 06 '11 at 18:50
  • I see. So does the `p_Hold` array contain what you expect it to on each iteration of your interactive hold loop? Knowing that will help determine whether the problem is in your input loop, or in your printing logic. (This debugging technique is often called "When in doubt, print more out".) – Greg Hewgill Dec 06 '11 at 18:57
  • p_Hold[ 0 ] always hold 0 for some reason. – Josh Lake Dec 06 '11 at 19:02
  • I would print the character you receive from `cin.get()` to see whether that's what you think it should be. I would recommend using `cin.getline()`, then you won't have to mess around with all that `cin.clear()` and `cin.ignore()` stuff. It looks like you may have had problems in this area previously. – Greg Hewgill Dec 06 '11 at 19:08
  • @JoshLake Looks like you never enter the block under `if(cin.good())`. Try moving `cin.clear()` *after* `cin.ignore()`. – jrok Dec 06 '11 at 19:08
  • problem fixed. cin.clear and cin.ignore had to be before cin.get – Josh Lake Dec 06 '11 at 19:43
0

Stricto sensu, std::cout and other C++ standard streams don't have any colors or fonts.

There is a standard on ANSI escape codes defining how to change fonts & colors on old character based terminals (and current terminal emulators like xterm and newer clones).

But if you care about terminal based I/O, I do suggest using a library for that like ncurses

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547