Defining a variable in different way and reading tokens

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.
Last edited on
Oh, and by the way, how could I print ASCII art without having to use a million escape characters? Ins't there a function that reads the literal value of the string?
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.


Well, you could specify in the string you're feeding to it, print( "%rHello %bworld%y!") by way of example, and do what you need to inside the function.

Or, you might consider http://www.cplusplus.com/reference/clibrary/cstdarg/va_list/

Can someone explain how to make that? Also, how can I separate "Hello", "World" and "!" using the operator + (like we do with << on cout)?


You aren't really 'separating' them with <<.

cout << "Hello " << "world" << "!" ;

is shorthand for: operator<<(cout, "Hello ").operator<<(cout, "world").operator<<(cout, "!") ;

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.


As you've described it: can't be done.

Oh, and by the way, how could I print ASCII art without having to use a million escape characters? Ins't there a function that reads the literal value of the string?


Save your ascii art to a text file and have your program read it into a string.


Last edited on
Thanks for the help.

Well, you could specify in the string you're feeding to it, print( "%rHello %bworld%y!") by way of example, and do what you need to inside the function.

What should I do inside the function? How I tell the function that if there is a %r, print the text in red?


You aren't really 'separating' them with <<.

I mean for human readability purposes. Like:
print("Hello "+"World"+"!");
Is much more readable than:
print("Hello ""World""!")

Looking forward to more help.
Last edited on
If you want your function to define outvar and chang trops, you would use address of "&"

1
2
3
void take(char  &);  //function prototype
take(trops);  //function call
void take(char &outvar) // function 


For the hello world you could us an array and a for loop.
http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/control/

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color); //color is an int

Use this website to find the colors you want.
http://www.daniweb.com/software-development/cpp/threads/9921
Last edited on
Just for funsies...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <Windows.h>   


struct set_color
{
	int color ;

	set_color(int c) : color(c) {}
};

std::ostream& operator<<(std::ostream& os, const set_color& sc)
{
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE) ;
	SetConsoleTextAttribute(h, sc.color) ;
	return os ;
}

const int blue = FOREGROUND_BLUE | FOREGROUND_INTENSITY ;
const int red  = FOREGROUND_RED  | FOREGROUND_INTENSITY ;
const int grey = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN ;


int main(void)
{
	std::cout << set_color(red) << "Did I change" << set_color(blue) << "the color?\n" << set_color(grey) ;
}
Topic archived. No new replies allowed.