 |
|
|
 |
|
 |
Hi Anton first of all your header file is very useful to me and i must say thank you for making it :P
But after using it for a couple simple things i was wondering if it would be posible to use in some way "cou::fg_green" as an argument for a fonction. Im really noob so this question might not even make sense. Anyways ill try to show you what im trying to do:
#include <windows.h>
#include <iostream>
#include "Console.h"
using namespace std;
namespace con = JadedHoboConsole;
/********************************************************
Fonction to center text in console with 3 args
-test: string you want to center
-filling: char you want to use to fill the blanks
-argForConsole: an variable to pass something
like con::fg_red to the variable
**********************************************************/
void center (string text, char filling, argForConsole){
for (unsigned int i = 0; i < ((80 - text.length())/2); i++)
cout << argForCon << filling;
cout << con::fg_green <<; text;
for (unsigned int i = 0; i > ((80 - text.length())/2); i++)
cout << argForCon << filling;
cout << con::fg_white;
}
int main () {
string test("what ever text to be centered");
center (test, '*', argForCon);
}
basicaly i want to fing a way to pass something like con::fg_gray to a variable i guess it would be a string type ??
anyways i hope this doesnt sound to dumb and if you can help me out it would be apreciated
thanks alot
Charles
|
|
|
|
 |
|
 |
Hi Charles,
Your question actually makes a lot of sense. The first thing to consider is the type of the color manipulators. In the section 'Points of interest' you can see that the color manipulators are not simple objects but functions. Thus if you want to pass a manipulator as a parameter you will have to pass the appropriate function pointer. That is all there really is to it
But since you indicate being a bit new to C++ I'll be a bit more explicit.
First thing to do when dealing with function pointers is to make the whole function pointer declaration somewhat readable with a typedef:
typedef std::basic_ostream& (*ColorManipulatorFnPtr)(std::basic_ostream&));
This line creates the correct function pointer type with the obvious name "ColorManipulatorFnPtr". Now it becomes a lot easier to declare a function that accepts a color manipulator as one of its arguments:
void center( const std::string& text, char filling, ColorManipulatorFnPtr FillColor )
{
const size_t ( (80 - text.length())/2 );
for (unsigned int i = 0; i < PadSize; i++) std::cout << FillColor << filling;
std::cout << con::fg_green <<; text;
for (unsigned int i = 0; i < PadSize; i++) std::cout << FillColor << filling;
std::cout << con::fg_white;
}
int main()
{
center( std::string( "Hello world!" ), '*', con::fg_red );
return 0;
}
Disclaimer: untested code! Use at your own peril. Symptoms of invoking UB may include but is not limited to wiped hard-disks haku's in your boot sector and WW3
|
|
|
|
 |
|
 |
Wow Jaded your great thanks alot for the help!!!!
I did not try this yet but i will tonight and ill reply to let you know if it works or not
Charles
|
|
|
|
 |
|
 |
First of all thanks alot for the help !!!!
your explanation is very clear and makes sense to me
but i must say this line doesnt compile
typedef std::basic_ostream& (*ColorManipulatorFnPtrr)(std::basic_ostream&));
so i tried with an extra colon like this cuz well idn
typedef std::basic_ostream& ((*ColorManipulatorFnPtrr)(std::basic_ostream&));
still doesnt work lol :P
it gives me this error for this line
error: expected initializer before '&' token
and a bit later it says
error: 'ColorManipulatorFnPtr' has not been declared
and since you already know im a noob you'll probably understand why i cant resolve this.
btw im using Code::Blocks as IDE and im ussing the GNU GCC Compiler
|
|
|
|
 |
|
 |
Hi Charles,
Typing code late at night and posting it before throwing it through a compiler was not a smart move So this time I did check my penmanship.
First off, I added the necessary include files. Without the <iostream> file the compiler won't have a clue what an std::basic_ostream is and thus can't grok the typedef. Secondly, I removed the final parenthesis which I copied by mistake. And finally I realized that the std::basic_ostream name I copied from my article is not a complete type but a template declaration so that needs to be changed to std::ostream (which itself is a typedef to something like std::basic_ostream< char, char_traits< char > > or something like that).
#include <iostream>
#include <string>
#include "Console.h"
typedef std::ostream& (*ColorManipulatorFnPtr)(std::ostream&);
void center( const std::string& text, char filling, ColorManipulatorFnPtr FillColor )
{
const size_t PadSize( (80 - text.length())/2 );
for (unsigned int i = 0; i < PadSize; i++) std::cout << FillColor << filling;
std::cout << con::fg_green <<; text;
for (unsigned int i = 0; i < PadSize; i++) std::cout << FillColor << filling;
std::cout << con::fg_white;
}
int main()
{
center( std::string( "Hello world!" ), '*', con::fg_red );
return 0;
}
|
|
|
|
 |
|
 |
Thanks so much Jaded your the best and really fast at replying.
Everything works exactly how I want it too !!!
Ounce my text based RPG will be done in like 1-2 months ill share it with you :P
When I grow up i want to be just like you
Charles
|
|
|
|
 |
|
 |
Hi. I'm not a pro. Do I only need console.h by itself to reference from my Tcl script, or do I need to compile it? Guess I can figure that out on my own. Is the "interface" (? i guess it's called?) straightforward enough your example is all I need? I want to use in a Tcl script and probably make a wrapper for it. Anyway thanks for making this!
|
|
|
|
 |
|
 |
In a C++ project the header file is all you need, but I don't know how you would set up binding to the interface for Tcl. Hopefully someone more familiar with that language can help you there.
Antoon
|
|
|
|
 |
|
 |
I have been trying for days to learn how to add color to my simple console programs, and have downloaded many header files to no avail.
I am trying this Console.h file, and I am getting errors when compiling:
line 258 of Console.h : expected init-declarator before '&' token
line 258 of Console.h : expected ',' or ';' before '&' token
there are many more of these messages, a pair for color, I think.
I am just a noobie, trying to learn C++, and unfortunately, have not been doing it long enough to understand these errors.
My version of Dev-C++ is 4.9.9.2, using on a windows system.
Any help or advice would be appreciated.
|
|
|
|
 |
|
 |
I think I answered my own question. I was using the wrong Console.h file.
all good! thanks JadedHobo!
|
|
|
|
 |
|
 |
Hi Adam,
Could you elaborate a bit more on how exactly you used the 'wrong' console.h file. Not so much for my sanity but for other people who are trying to use this bit of code and are running into trouble. With your story they may be better equipped to solve their problem.
Hopefully the fun of producing colorful console applications helps to compensate the despair you may encounter on you're way to become an old C++ hand
Antoon
|
|
|
|
 |
|
 |
It does not work with wcout :(
|
|
|
|
 |
|
 |
Hi elbertlev,
I have added the wide version manipulators to the include file. The example projects have not been updated, but the changes have been tested with BCB5. Watch for an update of this article.
Antoon
|
|
|
|
 |
|
 |
It should work now
Antoon
|
|
|
|
 |
|
 |
Very useful indeed. I'm not a full-time developer and have learned to develop in C++ via books and the net. I recently took on a personal project to help me learn allot of different travel industry codes and names. During my development, I wanted to reflect the incorrect answers in red and correct answers in green...in short, this has certainly saved me allot of time and energy, thanks!
|
|
|
|
 |
|
 |
Just what i was looking for, works a treat!
I've used it as part of C++ Application for Uni, i however refrenced this page!
Excellend code! Thanks Again
|
|
|
|
 |
|
 |
5 out of 5....
superior stuff Hobo....
But how do you call the clr screen function?
"you pointing at me"-variable
|
|
|
|
 |
|
 |
The clr manipulator is used at the start of the demo app but here is another example:
using std::cout;
using std::endl;
namespace con = JadedHoboConsole;
cout << con::clr << con::bg_blue << con::fg_white << "Top of the console" << endl;
If you really want to use it as a function another alternative is:
using std::cout;
using std::endl;
namespace con = JadedHoboConsole;
con::clr( cout );
cout << con::bg_blue << con::fg_white << "Top of the console" << endl;
Enjoy your console programming
|
|
|
|
 |
|
 |
You use the command system("cls"), which is in encharge of clearing the screen
|
|
|
|
 |
|
 |
And It worked PERFECTLY on the first try, AWESOME WORK!!!
5 out of 5 for shure
|
|
|
|
 |
|
 |
Hi All
Well I have used the following code to add color to stdout and stderr.
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if(INVALID_HANDLE_VALUE == hStdOut)
Error = ERR_HT_HANDLE;
GetConsoleScreenBufferInfo(hStdOut , &ConsoleScreenBufferInfoOutPut);
//Setting color for stdout
ConsoleScreenBufferInfoOutPut.wAttributes = FOREGROUND_GREEN;
SetConsoleTextAttribute(hStdOut ,ConsoleScreenBufferInfoOutPut.wAttributes);
It is working fine.
But when I used the same code with color red for stderr, I am getting the color for stdout as red though i have mentioned it as gree.
I have tried to use different structure for consolescreenbufferinfo but the problem is not solved.
Can anyone help in this regard.
|
|
|
|
 |
|
 |
According to MSDN documentation on GetStdHandle stderr and stdout use the same physical console buffer. Therefore setting the color of stdout will also set the color for stderr and vice versa.
"Handles returned by GetStdHandle can be used by applications that need to read from or write to the console. When a console is created, the standard input handle is a handle to the console's input buffer, and the standard output and standard error handles are handles of the console's active screen buffer."
|
|
|
|
 |
|
 |
This is absolutly amazing. BRAVO! very nice work
|
|
|
|
 |
|
 |
how to get a purpure?
Widzimy cie gnoju kurde!! hahaha
|
|
|
|
 |