Superscript on C ++ console output

I would like my program to output "cm 2 " (see square).

How to make superscript 2?

+6
c ++ superscript
source share
6 answers

As Zan said, it depends on which character encodes your standard output. If it supports Unicode, you can use the encoding for ² ( U + 00B2 ). If it supports the same Unicode encoding for source files and standard output, you can simply paste it into a file. For example, my GNU / Linux system uses UTF-8 for both, so this works great:

#include <iostream> int main() { std::cout << "cm²" << std::endl; } 
+16
source share

This is not something that C ++ can do on its own.

You will need to use a specific function of your console.

I do not know any consoles or terminals that implement super-script. Maybe I'm wrong.

+5
source share

I tried to complete this task with the goal of solving a quadratic equation. Writing ax² inside a cout << , holding ALT as you type 253, displayed correctly only in the source code, BUT NOT on the console. When the program started, it looked like a bright rectangle instead of superscript 2.

A simple solution to this problem seems to distinguish the integer 253 as a char, like this ... (char)253 .

Since our professor does not recommend the use of "magic numbers", I declared it as a constant variable ... const int superScriptTwo = 253; //ascii value of super script two const int superScriptTwo = 253; //ascii value of super script two .

Then, when I wanted superscript 2 to be displayed in the console, I passed my variable as char , like this ... cout << "f(x) = ax" << (char)superScriptTwo << " + bx + c"; and it displays perfectly.

Perhaps it is even easier to just create it as a char to start with, and not worry about casting it. This code will also print super script 2 on the console when compiling and running in VS2013 on my Lenovo running Windows 7 ...

 char ssTwo = 253; cout << ssTwo << endl; 

I hope someone finds this helpful. This is my first post ever, so I apologize in advance if I accidentally violated the protocols to answer a question posted 5 years ago. Any such occurrence was not intentional.

+3
source share

Yes, I agree with Zan.

Basic C ++ has no built-in functions for printing superscripts or indexes. You need to use any additional user interface library.

+2
source share

For super scripting or tweaking, you need to use the ascii value for the letter or number.

For example: Super scripting 2 for x² we need to get ascii value of super script of 2 (google search for this), i.e. - 253 . To enter the ascii character you need to do alt + 253 here, you can write any number, but in this case it's 253.

For example: - cout<<"x²";

So now it should display on a black screen.

0
source share

Why don't you try ASCII?
Declare the character and give it an ASCII value of 253 , and then type the character.
So your code should look like this:

 char ch = 253; cout<<"cm"<<ch; 

This will definitely print cm 2 .

-one
source share

All Articles