Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textcolor(), ahhhhh 1

Status
Not open for further replies.

airdevil

Programmer
Oct 31, 1999
10
US
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 << &quot;Hello&quot;; // This is not yellow;
..


..

clrscr();
cout << &quot;I am&quot;; // 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

&quot;procrastination is the key to happiness&quot;
 
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

All opinions are mine alone and do not necessarily reflect those of my employer.
 
Textcolor only registers a new color when the clrscr() is called.

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

textcolor(BLUE);
cout << &quot;I am still yellow&quot;;

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

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

&quot;procrastination is the key to happiness&quot;
 
One note, the function, cout.flush(); does not cause textcolor() to work accordingly.

Korn
airdevil23@hotmail.com

&quot;procrastination is the key to happiness&quot;
 
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

&quot;procrastination is the key to happiness&quot;
 
airdevil,

[tab]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.

[tab]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[tab]Action

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

[tab]Here's an example
Code:
#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)
        << &quot;This text is white on blue.&quot;;

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

   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 << &quot;Text in first window&quot; << endl;
   demo2 << &quot;Text in second window&quot; << endl;
   demo1 << &quot;Back to the first window&quot; << endl;
   demo2 << &quot;And back to the second window&quot; << endl;
   return(0);

   }

[tab]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

All opinions are mine alone and do not necessarily reflect those of my employer.
 
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:

[tt]
textcolor(14);
cout << &quot;I am yellow&quot;;
[/tt]

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
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
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

 
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
 
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
 
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
 
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
 
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 << &quot;\n Foreground Background&quot;;
for (i = 0; i <= 16; i++)
{
con << setclr(15) << setbk(0); //white text on black
con << &quot;\n &quot; << setw(2) << i;
con << setclr(i) << &quot; ABCDEF&quot;;
con << &quot; &quot; << setbk(i) << &quot; &quot;;
}
con << setclr(WHITE) << setbk(BLACK)
<< setxy(1,24) << &quot;Press any key to exit&quot;;
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.

 
It's been a long time since I used these but it runs in my mind that you have to define a window first.
Code:
con.window(1, 1, 40, 20); // set to the desired size.
con.clrscr();
con << &quot;\n    Foreground    Background&quot;;

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
 
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.
 
The textcolor changes when you input clrscr after you're
desired color:

textcolor(BLUE);
clrscr();
cout<<&quot;now you see it will be blue&quot;;

textcolor(RED);
clrscr();
cout<<&quot;are you getting this ?? I am red!!hahaha&quot;;


 
intsead of using cout, use cprintf()

textcolor(RED);
clrscr();
cprintf(&quot;red text&quot;);

and make sure you declare it
void textcolor(int newcolor);
 
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.
 
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 &quot;const&quot; 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 &quot;const&quot; 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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top