954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Special Characters in C++

I need to insert a "é" and other such symbols. At first I just tried using é straight out in C++ and it returned Θ. So I looked at my character map and did the unicode version \u00E9 where I wanted the é and it still returned Θ. I guess they are using two different standards. Can anyone point me to a list of such common special characters and their c++ encodings? My attempts at a google search have been inconclusive. Thanks :cheesy:

CStallion
Light Poster
36 posts since May 2006
Reputation Points: 10
Solved Threads: 1
 
I need to insert a "é" and other such symbols. At first I just tried using é straight out in C++ and it returned Θ. So I looked at my character map and did the unicode version \u00E9 where I wanted the é and it still returned Θ. I guess they are using two different standards. Can anyone point me to a list of such common special characters and their c++ encodings? My attempts at a google search have been inconclusive. Thanks :cheesy:

There's nothing standard about extended ASCII characters. the extended ASCII code for that character in your IDE's font is obviously different to the code in your output console's font.

using this code, the character é yields a value of -126 on my system

#include <iostream>

int main()
{
    char c;
    std::cin.get(c);
    std::cout << static_cast<int> c;
}
Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

So I try "\-126" and get a compile error. Any other ideas?

CStallion
Light Poster
36 posts since May 2006
Reputation Points: 10
Solved Threads: 1
 

I think you're c++ program will have to change fonts before beginning to output the text. The font used by the c++ console program is not the same font that the IDE editor uses. The IDE does not use cout or other console output functions to write to the window -- it uses win32 api functions. Don't know how to do that in console program.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
So I try "\-126" and get a compile error. Any other ideas?


-126 is 82 hex -- try \x82

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
So I try "\-126" and get a compile error. Any other ideas?


"\-126" isn't a char, the double quotes translate that you are attempting to use a string literal, which isn't what you want. There should be no need for the backslash either, since you're not using an escape character.

Just to re-iterate, this is not a C++ issue, this is an implementation-specific font issue. The windows 2000 cmd.exe console uses the "Terminal" font, hence why I got a value of -126 (or 0x82) for the 'é' character. if your implementation uses a different font, there's a strong possibility that the number -126 is not what you need (The code in my earlier post is indifferent to the font)

rambling aside - here is how you might print the é character, if your output uses the Terminal font.

#include <iostream>

int main()
{
    char c = -126;
         // you could also use 0x82 instead of -126
    std::cout << c;
}


or..

#include <iostream>

int main()
{
    std::cout << static_cast<char> (-126);
}


You may also be able to change your IDE to use the same font as your console window, although I haven't tested that idea.

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

Good! I changed my IDE font to the font "terminal" and then inserted the é straight out and everything worked fine. Thx!

CStallion
Light Poster
36 posts since May 2006
Reputation Points: 10
Solved Threads: 1
 
Good! I changed my IDE font to the font "terminal" and then inserted the é straight out and everything worked fine. Thx!

What? I thought you wanted to see 'é', not that other character. When I did that with Dev-C++ it only made the IDE look like the console, not the other way around.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You