<iostream> included by default

… so I can use

cout << "value is:" << variable << endl;  
insead of  
printf("value is %!???what was the encoding again", varible);  

… to debug.

I’m a bit of a fan of the old Pascal (and P5/java) style “println()”.

Could you perhaps wrap “cout” and make it a print function that doesn’t require knowing the argument type?


print("I have no idea what I'm printing =" + somevariable);  
println(" and I want an automatic carriage return." );  

Personally I find the insertion operator “<<” pretty ugly :slight_smile:
especially for print’s with many variables.

i second that. cout << ‘forever’ << endl;


Until then: http://www.cplusplus.com/reference/clib-…-rintf.html

Back in the day* I was close to printing this out to have it next to the OpenGL primitives cheat sheet.

*not THAT long ago…

At the moment we have:
ofToString() which takes numbers and returns them as strings.

This allows you to do.

string str = "coordinates are x:" + ofToString(posX) + "y:" + ofToString(posY) + "\n";  
printf( str.c_str() );  

obviously it would be nice to skip the ofToString altogether and also the .c_str() for printf.

One thing I was thinking is that you could have.

ofPrintln("coordinates are :x", posX, " y: ", posY);  

Where ofPrintln could take an unlimited number of arguments like printf. It would then turn numbers into a string, and concatonate them with all the strings passed in and then print out to the console.

The hurdle to this seems to be that you need to know the number and data types of your arguments - see: http://www.thescripts.com/forum/thread702500.html

maybe there is a way around this?

the other option is some sort of ofString class that extends from std::string and overrides the + and += operators. it could be combined with a simple ofPrintLn which accepts a string and prints it with a carriage return.

 ofString str = "coordinates are x:" + posX + " y:" + posY;  

Or it could have its own print function?

What do people think? Is there a smarter way to do this?

My 2 euro cents,
I think something like an ofString class would make a lot of sense, std::string can be a pain to work with, if you are coming from Flash or Java even more so.


hahakid - can you explain what the differences are?

there is someone here at eyebeam working on a document that is designed to help people with transition and any points about it are super helpful.

(you can see the progress here – just getting started. forgive the documentation, not formatted for wiki’s yet --our pre tag isn’t wrapping, but you can copy and past this into a text file: http://wiki.openframeworks.cc/index.php-…-sing-users)

about printing out, I was wondering if there wasn’t some genius preprocessor style way to convert println (“dsfsdf " + pos + " sdfsdfs”) into something else… I digged around, but didn’t see anything yet that’s helpful in terms of unknown number of arguments. the stuff you sent theo was super interesting, I had no idea how printf actually worked :slight_smile:

I think we’re happy to add iostream in ofConstants as long as it doesn’t present any issues… they don’t recommend it here:
but I think we do alot of things they don’t recommend.

take care!!

Big disclaimer that some of these problems are prob due to my programming rubbishness and being disaorganised.

I remember being a bit suprised at the crappyness of std::string when I began with C++.

Some of them might be more compiler problems, I would logically expect to be able to do:

string inputString = "Hello" + 1 + "test";  

but I get

error C2110: ‘+’ : cannot add two pointers

That works fine in Java. It’s easy to get around by making the number a string first, but it makes for ugly code.

Only yesterday I was trying to do something like:

if( inputString == "something" )   

And it doesn’t seem to work that way,

binary ‘==’ : no operator found which takes a left-hand operand of type ‘std::string’ (or there is no acceptable conversion)

ok fine, I google around and find std::string.compare (http://www.cppreference.com/cppstring/compare.html) which returns 0 if the strings are the same, which is a bit unintuitive, but the same as java’s compareTo.

Hmm, seems I get the above error in one part of the code, but not another, very strange.

Either way, Java strings feel a lot friendlier, simple things like converting strings to upper or lowercase are included, there are lots of ways to do it in C++, but something like:

transform(str.begin(), str.end(), str.begin(), tolower);  

(shortest i found now with google)

is not as easy on the eye as


Do correct me if I’m wrong here and there is a better way…

I’m definitely planning on picking up the compiler and contributing some code when I have some more time, maybe this would be a good place to start, no chance of breaking anything major :wink:


When I first started with strings I assumed too that

string str = "someText"+2;   

Would result in “someText2”

But the + operator is overloaded to shift through the string - so if I recall correctly.

string str = "someText"+2;   

results in: “meText” – shifting the string by 2 places. (A total waste if you ask me).

Same for +=

So that is why we have ofToString for the time being - but it is quiet the clumsy solution.

In case someone lands on this page via Google,

You can most certainly do this:

if( inputString == "something" )  

It turns out I didn’t have #include in my file, I was being tripped up since i was using std::strings all over the place and it somehow still worked…


Just came across this:

while looking for some more C++'y way to convert a string to an int an so on, it’s a template’ized way to do the conversions, let’s you do stuff like:

	string overRideScreenString = "1";  
	int theScreenAsInt = convertTo<int>( overRideScreenString );  

Might come in handy!