I was writing a simple scripting system. Actually, I was just simplifying my C++ code for easier developing. In other words, I was creating functions.
PS: I know my code is highly system dependent and blah blah blah. I don't plan on compatibility, and I prefer easier code than cross-platform code, since I'm beginning at C++. I will worry about that later.
First, I created a "print" function. It can take a color attribute and print the text to the console in that color.
1 2 3 4 5 6 7 8
|
void print(int color, char* output)
{
HANDLE handle= GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute( handle, color);
cout<< output;
SetConsoleTextAttribute( handle, grey);
}
|
I would call the function like this:
print(blue,"Hello World! ")
It would work, but what if I want multiple colors in a single "print" call? I would like something like this, if possible:
print($red"Hello"$blue"World"$yellow"!")
Instead, it has to take 3 print calls to print that on the screen.
Can someone explain how to make that? Also, how can I separate "Hello", "World" and "!" using the operator + (like we do with << on cout)?
Now to the next question.
I also did a different cin function, it was called "take". Here's how I call it:
take(trops);
And here is how it is done:
1 2 3
|
void take(char* outvar){
cin >> outvar;
}
|
I wanted the attribute outvar to be an variable that automatically defines itself based on the contents of the user input. It won't work because the compiler will argue about "trops" not being defined. I wan't the function to automatically define trops. In other words, I want it to simply be a clone of cin.
Thanks for the help. Looking forward to answers. Feel free to give system-dependent code if it's Windows(and 64bit).
EDIT: I would also like to know how to make an function have an optional attribute, that is, one that is not obligatory. In example, the color atribute in "print" has to not be obligatory - grey by default. - Solved.