Question:
Is there a way to outfile a theta symbol in C++?
Bob the Builder
2012-04-01 10:31:53 UTC
I am writing a program using files. I am at the point where I am just displaying my results and the i need to display the x,y and z angles with , thetaX as the title =46.59 degrees as the answer. My program works fine, I dont need help with that, I just want to know how to get a theta and degree symbol in C++. Is there a way?
Three answers:
Tizio 008
2012-04-01 13:11:56 UTC
You have to understand first encoding.



Since you are using an editor to input the special char "as you can see it", you have to get the encoding of the editor you've used to write the text. Or, better, be sure not to get it wrong using an explicit encoding of the char, e.g. \u03d1 and forget L and whatever. e.g.



printf("%s", "this is a normal string with a \u03d1\n");



prints the correct thing as far as the terminal can the actual encoding used to encode the char 03D1 (theta), which is e.g. utf-8 on my system.



If you know it, just be sure your editor encoding match the one of the output, and write the character "directly"; this works for sure for UTF-8, but not for any other "wide" encoding that may put a 0 in the string (since it would be interpreted as string terminator; intentionally it can't happen for utf-8 which was designed to avoid this, among other things) — though this way your source code could be ruined if read in the wrong encoding (to ensure the best portability you should use only ASCII chars)



if you want to use wide char instead, you must be sure of your locale settings, e.g. with



setlocale(LC_CTYPE, "");

printf("%ls\n", L"þis is a ϑ\n");



(and the correct editor encoding, utf-8).



The reference explain it better and more exactly and more extensively. In your case I suggest you to just avoid wide char and go for the first solutoin.



__EDIT__

totally forgot you've asked for C++ sorry. The speech is however almost the same, though you will use cout instead...
oops
2012-04-01 11:18:34 UTC
@Frank: "Use an L before any string with the non-ASCII characters."



No, this is wrong. The L is for creating wide character(wchar_t) strings, whatever that happens to mean on your system. With Visual C++ on Windows, it means UTF-16. With GCC on Linux, it means UTF-32.



You can, however, use UTF-8(which is also Unicode), and that goes into a regular single width char-based string.
Frank
2012-04-01 10:41:34 UTC
If you're outputting to a file, you hopefully have a stream to write to.



outputfile << L"θ = " << title << endl;



That should output the theta properly to your file. If you want to print to a console that supports unicode, you can use std::wout instead of cout.



Use an L before any string with the non-ASCII characters.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...