Weird bug with color in cout

Hi all, I'm writing a simple program that displays information and I've run into a bug where my colour bleeds outside of where it should. I've replicated the bug with the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main()
{

    //Variables
    string frog;
    int i = 0;

    //Body
    do
    {
        SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ), 0x02 );
        cout << endl << "Please type 'frog' (case sensitive): ";
        SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ), 0x07 );
        getline ( cin, frog );

        if ( frog == "frog" )
        {
            SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ), 0x2A );
            cout << endl << "FROG!" << endl;
            SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ), 0x02 );
            cout << "Ribbit." << endl << endl;
            SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ), 0x0A );
            cout << "Ribbit." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
            cout << "Blah blah blah." << endl;
        }

        else

        {
            cout << endl << "C'mon, you can spell frog. :(" << endl << endl;
        }

    }
    while ( i < 1 );

}


If you type 'frog' and press enter 3 times, the word "FROG!" is properly displayed with a green background and bright green text (might have to scroll up to see it). If you type frog 4 times, the background colour bleeds outside of the line. It only seems to modify the 4th frog input and afterward, the previous are unaffected. Also, the bug only seems to happen when there are many lines of cout in the code (like above). Ideas? Any help would be greatly appreciated! Thanks in advance!
Last edited on
You probably are receiving an error on some of your system calls. If SetConsoleTextAttribute() returns something, make sure you check it for errors.
I'm not receiving any errors in the logs when compiling the code above. Any other ideas?
@Mary M

I compiled your code in MS Visual 2012 Express, and everything looked fine to me. Each word was in the correct colors, and each section of blah's was good. What compiler are you using? If I have that compiler, I'll try again, with it.
Hi whitenite, I'm using the compiler included with CodeBlocks. I think it's the GCC compiler.

Did you type:
frog (enter)
frog (enter)
frog (enter)
frog (enter)

And on the fourth input, the color of the word "FROG!" didn't bleed? Here's what it looks like for me: https://i.imgur.com/v3SHBtU.jpg
@Mary M

No, I didn't see anything like what you showed. Here's what it looks like after I typed in 'frog' four times. It's in my dropbox.

https://www.dropbox.com/s/56vzk18gktqd4iy/frog.bmp
https://www.dropbox.com/s/lsnu921bkd2mqhy/frog.png

The png file is the picture on top of the compiler, and the bmp is the file cropped.

Also, here is a release version of the program. Hopefully, you can run it, on your computer.

https://www.dropbox.com/s/j3yk4o0ro2600by/Frog%20colors.exe

Also, sorry, I don't have CodeBlocks
Last edited on
Thanks for looking into this for me whitenite. Do you figure it's an oddity with the GCC compiler?
@Mary M

It's probably not the compiler, but the header files. And most likely not much you can do about it, except not have too many words outputted after you change colors.

Were you able to run the 'Frog colors' program? If so, did it show the same bleed-through as yours? If it did, then it may be a problem with way your computer processor handles the colors, and not the compiler or headers.

I also compiled it with Dev-C++ v5.6.2, and it ran fine also. It uses the MinGW compiler.
Last edited on
Your program works perfectly, no colour bleeding anywhere no matter how many frogs I type.

If the header files are problematic, would it be advisable to try downloading new ones?
@Mary M

I wouldn't. If this is the only problem you're facing, you may fix this one problem, but introduce other problems with some of your other programs.

Tip.. If anyone mentions the bleed through, just tell them it's NOT an error, but a feature in the program ;)
I get similar results as your Mary Magdalene with code::blocks aswell. Basically it only happens after enter frog correct 11 times and keep happening after.

http://s27.postimg.org/c2011j7zn/image.png

I checked if any of the setConsoleTextAttribute failed but none of them failed. I would say its not being buffered but you are using endl so that shouldn't be the case. Someone with more knowledge in windows might know.
Last edited on
I'm not receiving any errors in the logs when compiling the code above. Any other ideas?


The errors it was suggested you check for would not happen when compiling code. The suggestion was to check return values from functions to see if errors were occurring.

For instance, in the following we check for errors in the API calls:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

void SetOutColor(HANDLE out, WORD color)
{
    if (!SetConsoleTextAttribute(out, color))
        cerr << "Unable to change text out attribute.\n";
}

enum { lightgreen_on_darkgreen = 0x2A, darkgreen = 0x02, lightgreen = 0x0A, gray = 0x07 };

int main()
{

    HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);

    if (stdout_handle == INVALID_HANDLE_VALUE)
    {
        cerr << "Unable to get handle for stdout\n";
        return 0;
    }

    for (;;)
    {
        SetOutColor(stdout_handle, darkgreen);
        cout << endl << "Please type 'frog' (case sensitive): ";

        SetOutColor(stdout_handle, gray);

        string frog;
        getline(cin, frog);


        if (frog == "frog")
        {
            SetOutColor(stdout_handle, lightgreen_on_darkgreen);
            cout << endl << "FROG!" << endl;
            SetOutColor(stdout_handle, darkgreen);
            cout << "Ribbit." << endl << endl;
            SetOutColor(stdout_handle, lightgreen);
            cout << "Ribbit." << endl;

            for (unsigned i = 0; i < 26; ++i)
                cout << "Blah blah blah.\n";
        }
        else
            cout << "\nC'mon, you can spell frog. :(\n\n";
    }

}


Last edited on
@Mary M

I read where giblit said it happened for him after typing in frog 11 times, so I tried it, also. And yes, I was getting the same results as him, with my program. Weird. I had only tested the program with 5 inputs, so never saw the bleed-through before.
Last edited on
@Mary M

Okay, I fooled around with the code a bit more. I added a color change to light gray on black, and then the newlines. Then changed the color to what was to be printed. Even after 14 typed 'frog's, I'm still not getting any bleed-through.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main()
{

 //Variables
 string frog;
 int i = 0;

 //Body
 do
 {

	SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ), 0x02 );
	cout << endl << "Please type 'frog' (case sensitive): ";
	SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ), 0x07 );

	getline ( cin, frog );

	if ( frog == "frog" )
	{
	 cout << endl;

	 SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ), 0x2A );
	 cout << "FROG!";
	 SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ), 0x07 );
	 cout << endl;
	 SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ), 0x02 );
	 cout << "Ribbit.";
	 SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ), 0x07 );
	 cout << endl << endl;
	 SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ), 0x0A );
	 cout << "Ribbit.";
	 cout << endl;

	 for (unsigned i = 0; i < 26; ++i)
		cout << "Blah blah blah.\n";
	}

	else

	{
	 cout << endl << "C'mon, you can spell frog. :(" << endl << endl;
	}

 }
 while ( i < 1 );
}
Topic archived. No new replies allowed.