×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

textcolor(), ahhhhh

textcolor(), ahhhhh

textcolor(), ahhhhh

(OP)
Hi all,

When I use the textcolor() in a dos program, the color
doesnt change immediately, it registers about halfway through the program, not right before the text i want to color.


textcolor(YELLOW);
cout << "Hello";  // This is not yellow;
..


..

clrscr();
cout << "I am"; // this becomes yellow


Is there a function I can call to change the color immediately or am I missing something all together???


Thanx for your help



Korn
airdevil23@hotmail.com
 
"procrastination is the key to happiness"

RE: textcolor(), ahhhhh

It has been awhile since I used textcolor. It seems to me that it has something to do with clrscr and buffers. Does textcolor work consistently after clrscr? What happens when you flush your output buffer? What happens if you use cerr instead of cout (the difference between the two is cerr is not buffered)?


James P. Cottingham
 
www.ivcusa.com
All opinions are mine alone and do not necessarily reflect those of my employer.

RE: textcolor(), ahhhhh

(OP)
Textcolor only registers a new color when the clrscr() is called.

textcolor(YELLOW);
clrscr();
cout << "I am yellow.";

textcolor(BLUE);
cout << "I am still yellow";

textcolor(GREEN);
clrscr();
cout << "I am Green.";

Is there a command that flushes the output buffer? Is so What is it?

And the use of cerr doesnt not differ from the use of cout.

Thanx for your help.


Korn
airdevil23@hotmail.com
 
"procrastination is the key to happiness"

RE: textcolor(), ahhhhh

(OP)
One note, the function, cout.flush(); does not cause textcolor() to work accordingly.

Korn
airdevil23@hotmail.com
 
"procrastination is the key to happiness"

RE: textcolor(), ahhhhh

(OP)
Hey 22fat, thanx for your help,


I found out that textcolor only works with the old c
output functions.  All the verisons of printf  and whatnot.
Thanx.



Korn
airdevil23@hotmail.com
 
"procrastination is the key to happiness"

RE: textcolor(), ahhhhh

airdevil,

    Is there a command that flushes the output buffer? Is so What is it? the flush command flushes the output buffer if you not want a line feed, endl flushes the buffer with a line feed. You are probably correct about the clrscr which was what I thought but I wasn't too sure.

    There is a stream output class that does have the functionality of conio.h, it is constream and is called from the header constrea.h. The list below shows some of the manipulators.

Manipulator    Action

clreol    Clears to end of line in text window.
delline    Deletes line in the text window.
highvideo    Selects high-intensity characters.
insline    Inserts a blank line in the text window.
lowvideo    Selects low-intensity characters.
normvideo    Selects normal-intensity characters.
setattr(int)    Sets screen attributes.
setbk(int)    Sets new character color.
setclr(int)    Sets the color.
setcrsrtype(int)    Selects cursor appearance.
setxy(int, int)    Positions the cursor at the specified position.

    Here's an example

#include <constrea.h>

int main(void) {
   constream win1;
   win1.window(1, 1, 40, 20); // Initialize the desired space.
   win1.clrscr();             // Clear this rectangle.

   // Use the parameterized manipulator to set screen attributes.
   win1 << setattr((BLUE<<4) | WHITE)
        << "This text is white on blue.";

   // Use this parameterized manipulator to specify output area.
   win1 << setxy(10, 10)
        << "This text is in the middle of the window.";

   return(0);

   }

You can create multiple constreams, each writing to its own portion of the screen. Then, you can output to any of them without having to reset the window each time.

#include <constrea.h>
int main(void) {
   constream demo1, demo2;
   demo1.window( 1, 2, 40, 10 );
   demo2.window( 1, 12, 40, 20 );
   demo1.clrscr();
   demo2.clrscr();
   demo1 << "Text in first window" << endl;
   demo2 << "Text in second window" << endl;
   demo1 << "Back to the first window" << endl;
   demo2 << "And back to the second window" << endl;
   return(0);

   }


    The table and examples came from Borland's help file. I know this works under C++ 5.2 and earlier. I have not tried it with version 5.5.



James P. Cottingham
 
www.ivcusa.com
All opinions are mine alone and do not necessarily reflect those of my employer.

RE: textcolor(), ahhhhh

From what I have seen, if you pass a number to the function it works perfectly.

For example, if you want yellow text, you would pass:


textcolor(14);
cout << "I am yellow";


White text is color 0. You can play around with the different colors till you get one you like.


Hope this helps,

-Vic

vic cherubini
malice365@hotmail.com
www.epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====

RE: textcolor(), ahhhhh

I did not try it, but I think the change of the textcolor will become effective by use of Invalidate (BC++) or refresh(C++ -Builder)


hnd
hasso55@yahoo.com
 
 

RE: textcolor(), ahhhhh

I am extreamly new to C++ and i am intrested in the topic discussed here.  The example that 2ffat gave from a borland help file will not work.  I entered it into my editer/compiler which is Dev-C++ 4.01.  Any suggestion as to why it doesn't work?

Thanks,
~Ben

RE: textcolor(), ahhhhh

Most of the examples in this forum are for Borland compilers. Some libraries, like constrea.h are specific to this compiler. Dev-C++ may have a simliar library. You may have to look at its documentation for it.

James P. Cottingham
www.ivcusa.com

RE: textcolor(), ahhhhh

That brings up a good question.  Where do i look for the commands spefic to a library file.  Is there a way to tell by looking at the code that constructs it or what.

Thanks,
~Ben

RE: textcolor(), ahhhhh

Without written docs, you are forced to look at the libraries to see what it does. IMHO, this is more of an art than anything else. Start by looking in the directory where your header files are stored. Look for one that might contain something interesting then open up the header file with your favorite editor. Look at any comments then look at the functions or classes defined in the header. Start experimenting. If you are really brave, open up the .cpp code to see what actualy happens. Of course, all this is impossible if all your libraries are precompiled and you don't have access to the source code.

James P. Cottingham
www.ivcusa.com

RE: textcolor(), ahhhhh

Hello,everyone

I have a problem when I use BC++ 5.02 to
compile below code:

// name : EX13_5.CPP
#include <conio.h>
#include <constrea.h>

constream con;

void main()
{
    int i;

    con.clrscr();
    con << "\n    Foreground    Background";
    for (i = 0; i <= 16; i++)
    {
        con << setclr(15) << setbk(0);  //white text on black
        con << "\n  " << setw(2) << i;
        con << setclr(i) << "  ABCDEF";
        con << "      " << setbk(i) << "          ";
    }
    con << setclr(WHITE) << setbk(BLACK)
        << setxy(1,24) << "Press any key to exit";
    getche();
}


I alway got these link error message:


Error:  Error: Unresolved external 'setxy(int,int)' referenced from E:\TEMP\EX13_5.OBJ
Error:  Error: Unresolved external 'setbk(int)' referenced from E:\TEMP\EX13_5.OBJ
Error:  Error: Unresolved external 'setclr(int)' referenced from E:\TEMP\EX13_5.OBJ

I cannot find the error from the source
code. Please help me .

Thanx.

RE: textcolor(), ahhhhh

It's been a long time since I used these but it runs in my mind that you have to define a window first.

con.window(1, 1, 40, 20); // set to the desired size.
con.clrscr();
con << "\n    Foreground    Background";


I've also had better luck with setattr than setclr.

Also, don't use conio with constrea. Some function names might get confused.

James P. Cottingham
www.ivcusa.com

RE: textcolor(), ahhhhh

Thanks for your help.

But the same problem is still there, while
I changed the source code base on your advice.

I already commented the conio head file.

RE: textcolor(), ahhhhh

The textcolor changes when you input clrscr after you're
desired color:

  textcolor(BLUE);
  clrscr();
  cout<<"now you see it will be blue";
  
  textcolor(RED);
  clrscr();
  cout<<"are you getting this ??  I am red!!hahaha";
  

RE: textcolor(), ahhhhh

intsead of using cout, use cprintf()

textcolor(RED);
clrscr();
cprintf("red text");

and make sure you declare it
void textcolor(int newcolor);

RE: textcolor(), ahhhhh

Wow, I really must be old school...  then again, I've only been programming C++ for 2 years (and even to this day, only weeks from the end of my second year of programming, we have NOT learned about pointers, and we managed to spend a full MONTH on recursion).  In school, ever since I started Computer Sciences I Pre-AP, I have been taught to use all the iostream.h functions for data input and output, and the damned apstring.h class for string data.  Don't get me wrong, iostream is great in that it is simple and easy to use, but it does have many shortcomings (in my opinion).  One of them is its inability to conform to specified text windows.  Whenever an endl is outputted, the cursor returns to the left margin of the whole screen rather than the current text window.  Also, it doesn't cope with text formatting as well as the conio.h member functions.  I dunno, I guess I like the old ...printf() style of programming, but my programs have become a lot more robust and functional since I learned how to use real strings and all the IO control functions of conio.h.  I strongly recommend that everyone learn how to use it, as it is extremely useful.

RE: textcolor(), ahhhhh

Hey all,
    
    This for any of you that are using Borland C++ 5.02. When you try to use constrea.h for console output formatting you may get compile or make errors.

ex.
Error: Unresolved external 'setxy(int,int)' referenced from

    After much suffering with this problem I finally found a fix on the web.

Borland C++ 5.02 error : (SSU uses version 5.01, which doesn't have this problem!)
The word "const" was left out of a parameter list on line #280 of constrea.h.
If you own your own copy of Borland C++ 5.02, you can fix it by following these steps:
1. Open the file constrea.h from the include directory where Borland C++ is stored on your computer.
2. Go to line #280. It currently reads:
friend ostream& _RTLENTRY operator<<(ostream& _s,omanip2<T1,T2>& _f)
3. Add the "const" modifier to the 2nd formal parameter so that it reads as follows:
friend ostream& _RTLENTRY operator<<(ostream& _s, const omanip2<T1,T2>& _f)
4. Save the updated file to the same place from where you retrieved it.


Once I made this change to the constrea.h header file my programs compiled and ran fine.

happy programming!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close