Coloring with for statements

This is a discussion on Coloring with for statements within the C++ Programming forums, part of the General Programming Boards category; I have a program that I am trying to get to color the correct answers. For my testing stages I ...

  1. #1
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65

    Coloring with for statements

    I have a program that I am trying to get to color the correct answers. For my testing stages I am just trying to get it to color the T green, and the F red.

    Code:
    //Easy Coloring Of Words/Letters
    #define Color_White SetConsoleTextAttribute(stdOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
    #define Color_Red	SetConsoleTextAttribute(stdOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
    #define Color_Green SetConsoleTextAttribute(stdOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    #define Color_Blue  SetConsoleTextAttribute(stdOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
    Code:
    	//Here is the for statements
    	for(x=0; x<size; ++x)
    	{
    		Color_White
    		cout<<endl<<setw(7)<<student[x];
    		cout.flush();
    		for(y=0; y<10; ++y)
    		{
    			if(score[y][x] == 'T')		//If It is 'T' then it is green
    			{
    				Color_Green
    				cout<<score[y][x+1];
    				cout.flush();
    			}
    			else if(score[y][x] == 'F')	//If it is 'F' then it is red
    			{
    				Color_Red
    				cout<<score[y][x+1];
    				cout.flush();
    			}
    			else						//If for some odd reason it isnt one fo the two, its blue
    			{
    				Color_Blue
    				cout<<score[y][x+1];
    				cout.flush();
    			}
    		}
    		cout.flush();
    		Color_White
    		cout<<" # Correct: "<<setw(5)<<count[x]<<"Grade: "<<grade[x];
    	}
    The for statements or something dont work. It colors them randomly. I will include the source code so you can get the big picture.
    Last edited by GreenCherry; 10-29-2002 at 09:57 AM.

  2. #2
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    Sorry, it wouldn't let me attach the source.
    Last edited by GreenCherry; 12-18-2008 at 03:38 AM.
    I have a rabbit in my pants! Please be happy for me.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Does it print the correct text, regardless of colour?

    Try writing a smaller program to isolate the problem.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65

    Exclamation

    Ya, all the text prints correctly, I will try to make a smaller program. If anyone else has any ideas before i finish making o test program then by all means tell me.
    I have a rabbit in my pants! Please be happy for me.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Try my color header, it allows code like this:
    Code:
    cout << RED << "This is red text" << BLUE << "This is blue";
    And, it compiles on any platform and computer.
    Attached Files Attached Files
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    Thanks but no thanks, i figured it out already. If you look at my code the problem was that i put [x+1] when i shouldve put [x] (above the flush lines).
    I have a rabbit in my pants! Please be happy for me.

  7. #7
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    sang thats pretty damn sweet. Is it ok with you if i change the color names, ie red, blue to say COLORRED?

    edit// didn't work (unchanged):

    Code:
    #include <iostream.h>
    #include <windows.h>
    #include <petter-color.h>
    
    int main()
    {
    	cout<<RED<<"This text is red.\n";
    	cout<<BLUE<<"This text is blue.\n";
    
    	return 0;
    
    }
    PHP Code:

    --------------------ConfigurationColorTest Win32 Debug--------------------
    Compiling...
    ColorTest.cpp
    D
    :\MyProjects\Mine experimental or unfinished\ColorTest\ColorTest.cpp(14) : error C2065'RED' undeclared identifier
    D
    :\MyProjects\Mine experimental or unfinished\ColorTest\ColorTest.cpp(15) : error C2065'BLUE' undeclared identifier
    Error executing cl
    .exe.

    ColorTest.exe 2 error(s), 0 warning(s
    Last edited by RoD; 10-30-2002 at 01:26 PM.

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    All the functions and varaibles are in the Petter:: namespace.
    Code:
    using namespace Petter;
    will fix everything.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  9. #9
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    Can one:
    Code:
    using namespace petter;
    using namespace std;
    and use both namespaces?

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Yes.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    cool....
    [is amused easily]
    [starts outputting fun colors]

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  12. #12
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    I get this:

    PHP Code:
    U:\Assignments\Ch9\Quiz.cpp(147) : error C2678binary '<<' no operator defined which takes a left-hand operand of type 'class ostream_withassign' (or there is no acceptable conversion)
    U:\Assignments\Ch9\Quiz.cpp(151) : error C2678binary '<<' no operator defined which takes a left-hand operand of type 'class ostream_withassign' (or there is no acceptable conversion
    on these lines:
    Code:
    cout<<GREEN<<score[y][x+1];
    cout<<RED<<score[y][x+1];
    
    //or even if i do this:
    cout<<RED<<"Hi!";

  13. #13
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Perhaps your compiler doesn't support Koenig lookup.
    I've tested it with VC++.NET and CodeWarrior.

    Just for debugging: Does this work?
    Code:
    Petter::operator << (std::cout,Petter::RED);
    std::cout << "hej";
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  14. #14
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    Yes, that does work, it turns my entire program red, and it prints 'hej', but how is that supposed to help me?
    I have a rabbit in my pants! Please be happy for me.

  15. #15
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    It tells you that your compiler cannot handle Koenig lookup, ie it is unabel to use my color header.

    To make my header work with older compliers, try this:
    Code:
    using Petter::operator<<;
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-10-2006, 01:05 AM
  2. Textring Problem: Bad coloring
    By Sentaku senshi in forum Game Programming
    Replies: 2
    Last Post: 10-18-2002, 04:28 PM
  3. window coloring
    By Labelizm in forum Windows Programming
    Replies: 4
    Last Post: 07-22-2002, 08:06 PM
  4. Coloring in the background of shape rectangle...
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 03-09-2002, 08:09 AM
  5. C editor (syntax coloring)
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-29-2001, 05:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21