tags:

views:

127

answers:

3

I have the following code:

cout << "String that includes a £ sign";

However, the £ sign is not being recognised by the compiler, and instead an accented u is being displayed. I'm able to insert one using the following method:

cout << "String that includes a " << char(156) << " sign";

where 156 is the ASCII code of the £ sign. Is there a way I can include this ASCII code in the string itself without having to separate it out like this? For example:

cout << "String that includes a <some way of iserting the £ sign> sign";
+10  A: 

£ does not have an "ASCII code" - ASCII is 7 bits (0..127). There are various extended 8 bit characters sets which include £ but there is no single standard for this and the £ symbol may have various different values in different environments. You should probably be using Unicode if you need international symbols with maximum portability.

Paul R
Yep. However, you might try `std::wcout << L"String that includes a £ sign";`. Depending on your platform, this might work.
sbi
For clarification. In Visual Studio: Project -> Properties ->Configuration Properties -> General -> Change "Character Set" from "Use Multi-Byte Character Set" To "Use Unicode Character Set"
SauceMaster
+3  A: 

ASCII only defines characters up to 127. (The 8th bit was intened for parity, which was needed for the noisy transmission line of the 1960s when ASCII was designed) Beyond that, the characters vary by code page and font. There is a discrepancy between the font used by the editor you use to write your C++ code, and the font used by whatever is displaying the output.

So, you need to display character 156 to display a £ on your output device. You can explicitly enter a character 156 on a WIndows PC, by holding done the Alt key and pressing "0159" on the numeric keyped. Alternately, you can use a character code in the string in hex:

 cout << "String that includes a \x9C sign"; 
James Curran
+5  A: 

You can use the \x escape sequence to insert a character with its hexadecimal code. In your case

cout << "String that includes a \x9C sign";

Like Paul says, this just outputs a character with code 156, but gives no guarantee that it is actually the pound sign on different systems.

Jappie