+ 5

How can you output something as a superscript and subscript in c++?!?

Ive searched​ on the internet and i cant find some help for it. I know the ascii code have a number code only for 1 2 3 and maybe a few letters. Any tips how could i do it? :) im a beginner,but this could look really​ amazing for my output.

17th Oct 2017, 7:12 PM
derXred
derXred - avatar
13 Answers
+ 15
//================================== // Writing Unicode strings to file // Done for : derXred // By : Babak Sheykhan // Note : You can't run it on SL // Live lnk : http://rextester.com/QJZ70144 //================================== #include <locale> #include <codecvt> #include <iostream> #include <fstream> int main() { const std::locale utf8_locale = std::locale(std::locale(), new std::codecvt_utf8<wchar_t>()); std::wofstream wfout; wfout.imbue(utf8_locale); wfout.open(L"Stats.txt"); wfout << "Line 1: " << L"Hello\x00B2!" << std::endl; wfout << "Line 2: " << L"O\x00B3 x\x2080 + x\x2081 + x\x2082" << std::endl; wfout << "Line 3: " << L"x\x2083\x208A\x2084" << std::endl; wfout << "Line 4: " << L"\x20AC Euro!" << std::endl; wfout << "Line 5: " << L"2\x2077\x207B\x2079" << std::endl; wfout.close(); } ScreenShot : [https://pasteboard.co/GPtzFCS.jpg] Ext live lnk : [http://rextester.com/QJZ70144] (Naturally, Have no visual output) SL lnk : [https://code.sololearn.com/c9baEuxEWzIS] Ref 1 : [http://www.cplusplus.com/reference/codecvt/codecvt_utf8/] Ref 2 : [http://www.cplusplus.com/reference/fstream/filebuf/imbue/] Discussion : [https://stackoverflow.com/questions/15911141/writing-unicode-to-a-file-in-c]
18th Oct 2017, 9:26 AM
Babak
Babak - avatar
+ 16
Under ordinary circumstances you won't print out Unicode characters like ℃ or 5² or © etc, using cout object, but rather wcout (wide character output stream) provides some facilities to do so. Format: std::wcout << L"Your Unicode string"; Real Example std::wcout << L"hello\x00B2!" << std::endl; std::wcout << L"O\x00B3 x\x2080 + x\x2081 + x\x2082" << std::endl; std::wcout << L"x\x2083\x208A\x2084" << std::endl; std::wcout << L"\x20AC Euro!" << std::endl; std::wcout << L"2\x2077\x207B\x2079" << std::endl; Notice 1: You shouldn't use wide-output and narrow-output (cout etc) together in your program. Reference : [http://www.cplusplus.com/reference/iostream/wcout/] Related discussion : [https://stackoverflow.com/questions/8947949/mixing-cout-and-wcout-in-same-program] std::wcout << L"Your Unicode string"; std::cout << "Your ASCII string"; Notice 2: Success in producing the correct Unicode characters depends on the console font in which programs being executed. (I ran the code in my Windows machine with consolas font and it worked perfectly fine) ScreenShot : [https://pasteboard.co/GPoKMCr.jpg] Live external link: [http://rextester.com/FOSBP96641] SL link : [https://code.sololearn.com/cNSvg5FamvMT] Unicode reference : [https://chromium.googlesource.com/external/liblouis/+/23fb3bd7f3d4c857c26f6caef0f2dcca7578c7f3/tables/unicodedefs.cti]
17th Oct 2017, 8:58 PM
Babak
Babak - avatar
+ 16
In fact, I'm thankful for the opportunity you gave me to help out. I'm glad that you finally get your answer. Good luck, my friend.
18th Oct 2017, 10:47 AM
Babak
Babak - avatar
+ 12
You've done some good work so far. I will do a little more research and come back again. Feel free and don't hesitate about asking further questions.
18th Oct 2017, 4:24 AM
Babak
Babak - avatar
+ 2
@Babak Sheykhan (PERS) Sir, you are my hero :) The code works like a charm. In my research i was getting to the point that i should use UTF-8, but i was far away from what i should do to make it work. Link, more about the types of encoding(for a beginner, this might help to understand): https://stackoverflow.com/questions/700187/unicode-utf-ascii-ansi-format-differences Again, i can`t thank you enough!
18th Oct 2017, 10:16 AM
derXred
derXred - avatar
+ 1
I don't think it's possible by conventional means. Superscript and subscript are just ways to format text, and the standard output file doesn't really support text formatting - that's mainly an HTML thing.
17th Oct 2017, 7:43 PM
DaemonThread
DaemonThread - avatar
+ 1
@Babak Sheykhan (PERS) Thank you so much ! :) I will read every link you post and i will come back with more words of thanks. Best regards
17th Oct 2017, 9:40 PM
derXred
derXred - avatar
+ 1
@Babak Sheykhan (PERS) Well i hope i will finish it soon, even if it wont be close to perfection i will try to post it over here(idk if i can post multiple headers and cpps files),but maybe someone is looking for smth like that. The algorithm is for a construction engineer problem (I dont think many are interested about this field)
17th Oct 2017, 9:41 PM
derXred
derXred - avatar
+ 1
@Babak Sheykhan (PERS) I can not thank you enough :) THANK you again!
17th Oct 2017, 10:04 PM
derXred
derXred - avatar
+ 1
(I dont know if i should start another discussion or not) @Babak Sheykhan (PERS) If im tring to output to a file,then im back from where i started with the output on the file(ive changed the font in notepad to consolas), on the console all is perfect. std::wofstream f(L"some file.txt"); f << L"hello\x00B2!" << std::endl; // this is fine f << L"O\x00B3 x\x2080 + x\x2081 + x\x2082" << std::endl; //here i get only "O3 x" (3 is as a superscript) f.close(); So ive read a bit about what i quess it might be the issue: "Output During compilation, compiler outputs diagnostics to the console in UTF-16. The characters that can be displayed at your console depend on the console window properties. Compiler output redirected to a file is in the current ANSI console codepage." link: https://msdn.microsoft.com/en-us/library/xwy0e8f2.aspx So the compiler cant output in the file what i can see in the console??!! (or is just smth that i have to change in some settings(probably i sound stupid,but had to ask))
18th Oct 2017, 1:33 AM
derXred
derXred - avatar
+ 1
How do i print a superscript in C?
27th Feb 2019, 8:34 PM
Osae-Addo Emmanuel
Osae-Addo Emmanuel - avatar
0
@DaemonThread So you believe i should try to do a function in another programming language? and after i could make a dll for it and add it to my c++ project? :) If you know if is possible to use smth like that from html i should try it, but im not sure if i can use the html for my WinForm app :( probably im saying smth stupid over here :)) so im just asking, i dont want to confuse someone
17th Oct 2017, 8:42 PM
derXred
derXred - avatar