Last night I got so fustrated trying to do something really simple in c++. I needed to represent a GBP £ sign in a c++ windows console application. My smug friend using linux terminal and GCC assured me that the pound sign did not keep him up last night, but for me compiling away on MSBUILD it was a nightmare. Something so simple. yet after trying EVERY POSSIBLE mixture of search terms, google provided me with nothing! For once google had failed me! It did however suggest escape character methods and all sorts of other fruitfull options but none that worked. When outputing a £ to the command line I got “u”. Not much use when your trying to represent a price in GBP! It turns out that microsoft, being american, does not naturally support the character and the following is required to achieve such a simple thing.
//show pound sign in c++
char pound=156;
cout << “This will cost ” <<pound << “5.00” ;
Why am I blogging this?? In the hope that after this blog post gets indexed by google, the many others who appear on forums searching for this solution, and those who will come across it in the future, will find this and get an early night! Unlike myself 😛
December 20, 2007 at 1:07 am
Omg, I love you so much. I went to the extent of scouring through the character map to see if I was being retarded. Out of interest how did you work it out in the end?
December 20, 2007 at 1:08 am
Oh and is this ANSI C compliant?
December 26, 2007 at 11:06 pm
Honestly I just thought of all the ways I could possibly put this damn pound sign into my app! Then I remebered back to a lecture where I first saw odd data type assignments like char foo=23, then realised I needed the right character code for a pound sign, and yes I assume this is ANSI compliant. I got to my tutorial the following morning and realised my tutor had emailed this solution to the rest of the class, if only I had checked my email! 😀
August 30, 2008 at 2:35 am
You sir, are a true master among men, i’ve been looking for this solution for hours now and if it was not for your blog, Google would have failed me too.
Thank you so much 🙂
September 11, 2008 at 8:10 pm
omg thank you so much will help with my school work so much.
doing wage slip’s without a pound sign. she would rip my head off thanks
September 16, 2008 at 9:55 pm
Well, i wanted this for a quick program and found it after one search on Google, thankyou 😀
September 17, 2008 at 12:08 pm
Thank you so so much! Google almost failed me and my mind couldn’t cope with that :p.
November 4, 2008 at 4:41 pm
Thanks so much! I’ve just spent the past hour trying to figure this one out! Got straight to this once I realised it was a cout problem (I’ve been working with char to string conversion, though it was that). Thanks again!
November 19, 2008 at 7:07 pm
You are an absolute number one, I’ve never been able to figure this out and had to change a program I wrote to use dollar signs. Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you!!!!!!!!!!
December 18, 2008 at 12:55 pm
Hi, I have a question. Why have you initialised pound to 156?
December 18, 2008 at 12:56 pm
Oh right I got it. ASCII code for £ is 156. Get rid of my comment above
January 8, 2009 at 5:30 pm
You can find this information by Googling for ‘ascii table’. If you don’t find the correct code there, try adding ‘extended’. This should also be available in the character map on Windows, just look for the ASCII value. If you’re still at a loss, look into Unicode and Unicode string literals.
January 22, 2009 at 5:25 pm
Again, one of the many google devotees who would have failed without this simple 2 lines of code. Thank you very much, and btw your blog i now the first result when searching for pound sign C++.
March 17, 2009 at 5:34 pm
Dont know why c++ is set up to change charicters. But it has been defined in such a way that you can define any charicter code within a string, without resorting to seperate variables.
The hex value for 156 is 9c. To supply the hex code for a charicter in a string use \x.
So for a string such as “This will cost £5.00″ use:
“This will cost \x9c5.00”
It doesnt look as pretty, but its potentially more efficient.
Regards
Pingback: C++ £ @ Andy Longhurst
April 11, 2009 at 2:10 pm
Sorted me out! Thanks Tom!
August 5, 2009 at 8:18 am
You can look up other such symbols in an ASCII character chart and output them. For example a yen symbol is 157. Those are the only two “foreign” (i.e. non-dollar) currency symbols in the extended ASCII codes. Good luck finding a euro symbol without supporting Unicode or changing the font.
September 25, 2009 at 1:58 pm
Thank you very much =) Trying to do some extra work for my object orientated programming lectures and couldnt for the live of me remember such a simple solution. You made google usefull again
September 30, 2009 at 1:02 pm
truly amazing
October 2, 2009 at 10:43 am
Fantastic solution except for one thing.
It didn’t work for me, a quick google around and i found that where some people use 156, others seem to use 163.
163 works for me, but other people in my class use 156.
What’s the difference? I’m on a mac and they’re on PCs. Who’s being the inconsistent one here? Microsoft or Apple (i have no bias, i’m just curious).
Otherwise, thanks for such a great solution.
January 5, 2010 at 8:26 pm
Thank you sir!
May 3, 2010 at 11:51 pm
I won’t say this to many men, but i love you! I am so glad you put this on ur blog and google’s picked it up, i’ve been looking for this for ages! 😀
May 21, 2010 at 6:33 am
It’s really weird that on the ASCII table it says 163 for the pound sign, but the character code 156 works for me. thanks a lot! neat solution and saved me a lot of trouble
October 9, 2010 at 12:45 pm
I am on a Forensic Computing undergrad course, and although the lecturer has told us not to worry about the £ sign, being British and all, I thought I had better find out how to print it.
First Google, came up with your post and I would like to thank you for posting it, for ALL of us!
January 12, 2011 at 3:00 pm
Congratulations, you were first on google! Ha
April 30, 2011 at 6:40 pm
Thanks for the post, I found my answer within seconds 🙂
May 28, 2011 at 2:40 pm
Yeah…You saved me. You’re great.
May 8, 2015 at 1:53 pm
8 Years on from the original post and this helped me loads!
Couldn’t find this info anywhere else. Thanks!