Thread: Coloring my text. Compiler error problem.

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    33

    Question Coloring my text. Compiler error problem.

    - I've been using two header files to color my text.

    - The first one color_green.h saves the original color settings and then sets the new colors (green text).

    - And second one called restore_color resets to original colors.

    Code:
    // This is the header "color_green.h"
    
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
      WORD wOldColorAttrs;
      CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
    
    
      /*
       * First save the current color information
       */
      GetConsoleScreenBufferInfo(h, &csbiInfo);
      wOldColorAttrs = csbiInfo.wAttributes;
    
    
      /*
       * Set the new color information
       */
      SetConsoleTextAttribute ( h, FOREGROUND_GREEN | FOREGROUND_INTENSITY );
    
    /*************************************************/
    // This is the header "restore_color.h"
    
    /*
    * resets to the original colors
    */
      SetConsoleTextAttribute ( h, wOldColorAttrs);



    This has been working. But then my compiler has started giving me the error messages(both of them in "restore_color.h") :

    error: 'h' undeclared (first use in this function) &
    error: 'wOldColorAttrs' undeclared (first use in this function)


    Why am I getting error messages?
    Last edited by sampleandhold; 04-04-2012 at 09:50 AM.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    edit: i didn't see the last line comment. sorry.

    are you including restore-color.h before the other one? in C the declaration of the variables has to be seen by the compiler before they are used.

    also, typically in C you don't put executable code in a header file, just definitions.
    Last edited by dmh2000; 04-04-2012 at 09:57 AM.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    33
    Quote Originally Posted by dmh2000 View Post
    edit: i didn't see the last line comment. sorry.

    are you including restore-color.h before the other one? in C the declaration of the variables has to be seen by the compiler before they are used.

    also, typically in C you don't put executable code in a header file, just definitions.
    I include the color_green.h first. But I don't include it until I'm going to use it. Should I include it at the beginning together with the other includes?

    Do you know why I'm getting error messages?

    And should I make the color stuff a function instead?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Where exactly did you get those header files?

    Because they make no sense - they look like fragments of code.

    Like you're meant to do something like this
    Code:
    void foo ( ) {
        // declare vars
    #include "color_green.h"
       // do stuff in green
    #include "restore_color.h"
    }
    Which is entirely possible, but a completely bone-headed way of programming.

    > And should I make the color stuff a function instead?
    Yes, for the love of all things good and wholesome - YES
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    33
    Quote Originally Posted by Salem View Post
    Where exactly did you get those header files?

    Because they make no sense - they look like fragments of code.

    Like you're meant to do something like this
    Code:
    void foo ( ) {
        // declare vars
    #include "color_green.h"
       // do stuff in green
    #include "restore_color.h"
    }
    Which is entirely possible, but a completely bone-headed way of programming.

    > And should I make the color stuff a function instead?
    Yes, for the love of all things good and wholesome - YES
    Well, I'm making a text adventure game and within the game there's a part when your'e using a computer and it goes back and forth between the computer look (which is green text) and the regular look. I'm a fairly new programmer and this seemed to be good idea and it did work for a long time until I started getting error messages.

    Well, I'll take heed and make the color routine a function.

    And I got the code right here on cprogramming.com in the tutorials: How To Color My Text or something like that.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Post some links - I don't feel like searching...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    33
    I made it a function. I'm still getting the same error messages:

    error: 'h' undeclared (first use in this function) &
    error: 'wOldColorAttrs' undeclared (first use in this function)

    How can I can fix these errors?

    Here's my function:

    Code:
    int colorItGreen()
    {
        int static colorIsGreen=0;
    
    
       
       if(colorIsGreen == 0)
       {
      HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
      WORD wOldColorAttrs;
      CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
    
    
      /*
       * First save the current color information
       */
      GetConsoleScreenBufferInfo(h, &csbiInfo);
      wOldColorAttrs = csbiInfo.wAttributes;
    
    
      /*
       * Set the new color information
       */
      SetConsoleTextAttribute ( h, FOREGROUND_GREEN | FOREGROUND_INTENSITY );
      colorIsGreen = 1;
    
    
      printf("\n this is colorItGreen! Green!\n\n");
          getchar();
          colorIsGreen = 1;
          return;
      }
      if(colorIsGreen == 1)
      {
      /*
       * Restore the original colors
       */
      SetConsoleTextAttribute ( h, wOldColorAttrs);
      colorIsGreen = 0;
      printf(" it's restored");
      getchar();
      return;
      }
    }
    Here's the link: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Last edited by sampleandhold; 04-04-2012 at 12:10 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    Move this up a couple of lines, so the scope is the whole function, and not just one of the if statements.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    33
    Quote Originally Posted by Salem View Post
    > HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    Move this up a couple of lines, so the scope is the whole function, and not just one of the if statements.
    That took care of the first error. I'm still getting the error: 'wOldColorAttrs' undeclared (first use in this function)

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    33
    I moved "wOldColorAttrs = csbiInfo.wAttributes;" up too.

    And it worked to color the text green. But when it was supposed to be restored back, the colors are not same as they were originally.


  11. #11
    Registered User
    Join Date
    Feb 2012
    Posts
    33
    I changed the code to this:

    Code:
    void colorItGreen()
    {
        int static i, colorIsGreen=0;
    
    
      HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
      WORD wOldColorAttrs;
      CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
    
    
    
    
       if(colorIsGreen == 0)
       {
           wOldColorAttrs = csbiInfo.wAttributes;
      /*
       * First save the current color information
       */
      GetConsoleScreenBufferInfo(h, &csbiInfo);
    
    
    
    
      /*
       * Set the new color information
       */
      SetConsoleTextAttribute ( h, FOREGROUND_GREEN | FOREGROUND_INTENSITY );
      colorIsGreen = 1;
    
    
      printf("\n this is colorItGreen! Green!\n\n");
          getchar();
          colorIsGreen = 1;
          return;
      }
      if(colorIsGreen == 1)
      {
      /*
       * Restore the original colors
       */
      SetConsoleTextAttribute ( h, wOldColorAttrs);
      colorIsGreen = 0;
      printf(" it's restored");
      getchar();
      return;
      }
    }
    The text turns green. But it won't restore the color. It just stays green.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > WORD wOldColorAttrs;
    In order for this to maintain it's value from one call to the next, you need to make it static (like your colorIsGreen flag).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Feb 2012
    Posts
    33
    Quote Originally Posted by Salem View Post
    > WORD wOldColorAttrs;
    In order for this to maintain it's value from one call to the next, you need to make it static (like your colorIsGreen flag).
    I was thinking about doing that.

    It works now! Thank you so much for the help. Your'e a lifesaver

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Coloring text
    By thepolo in forum C Programming
    Replies: 5
    Last Post: 05-25-2009, 07:56 PM
  2. Road Coloring Problem solved
    By BobMcGee123 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-21-2008, 12:40 AM
  3. Coloring My Text
    By bjl in forum C++ Programming
    Replies: 10
    Last Post: 02-05-2008, 07:37 PM
  4. about coloring text
    By GanglyLamb in forum C Programming
    Replies: 4
    Last Post: 10-22-2002, 12:21 PM
  5. Textring Problem: Bad coloring
    By Sentaku senshi in forum Game Programming
    Replies: 2
    Last Post: 10-18-2002, 03:28 PM