This seems such a simple problem but I've got no way to put the "£" into my code, all i get is the "ú" symbol?
This seems such a simple problem but I've got no way to put the "£" into my code, all i get is the "ú" symbol?
Not as simple as it seems. In a particularly impressive move of conflict between backwards compatibility and sane behaviour, the compatibility won, and so you end up with your compiler's editor using the Windows-1251 (most likely) character set while the console uses the IBM-DOS character set, codepage 443 or 850 or something like that. (443 is US, 850 is Germany, don't know what UK would be.)
In effect, your compiler compiles the code, which is Win-1251, and takes the character codes of the string literally into the executable. There, in your exe, is a character code that would be the pound sign in Win-1251.
However, when printing to the console, the same character code is interpreted as an 'ú' instead.
Solution? Uh... I'll leave that to others. I think there's a setting somewhere in the console properties for that, but that's only a local solution.
gcn_zelda: there's no character 156 in ASCII...
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Of course, this depends on which font you have, or perhaps which code page you have for command line programs.
This sounds a damn sight more compliacated then i thought it would be, if its any help I'm using microsofts visual c++ on windows xp pro using the british character set. Is there no way just insert a specific character like you can in HTML?
We guessed as much.if its any help I'm using microsofts visual c++ on windows xp pro using the british character set
Not really, no. Not a named character. It is possible to insert a specific character code, but that brings you back to the dependency.Is there no way just insert a specific character like you can in HTML?
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
c++
cout << (char)156;
c
printf("%c",156);
Last edited by apsync; 01-05-2005 at 03:11 PM.
dependency????Originally Posted by CornedBee
Dependency on the character set that happens to be in use.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
I tried that as one of the first things, i just got a whole heap of errors chucked at me, plus we're not really allowed to use regular C code as that is what we've being taught up to now, I'm pretty sure that it has to stay as true to c++ as humanly possible.c++
char r = 156;
cout << r;
c
printf("%c",156);
I'm not really bothered if it doesn't work on this system, what matters is if it works when compiled on a linux shell.Dependency on the character set that happens to be in use.
The fact that it works for you is irrelevant, since it may not work on others' computers.
Edit : And why did you open up the whole std namespace for one cout? Just use std :: cout.
In that case, find a text editor that saves the files in the ISO-8895-1 encoding (I believe that's Linux's standard set), save them with that, transfer over the the Linux box and it should work.I'm not really bothered if it doesn't work on this system, what matters is if it works when compiled on a linux shell.
Point in case is that if you're developing at one machine but want to run on another, you'll have to keep this stuff in mind - and unfortunately, I'm not aware of any proper way to find out about these problems at runtime. Hey, even Java doesn't manage it!
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Well it wont help him much if it works for you but not for him will it??
If you want a bad, non-portable, not sure to work and probably error-prone method you will need to find out what value that character has, This little program can help you with that
Then you will manually have to chech all the characters that are printed out and when you find it you will know that on your particular system with your charset and everything £ has the value to the left.Code:#include <iostream> using namespace std; int main() { unsigned char c = 0; for(int i=0; i<256; c++, i++) cout << (int)c << ": " << c << endl; }
Characters are problematic anyway. Case in point? Try running this program, compiled as C++ in MSVC++:
So, what do you think? Is the German umlaut ö a printable character? The answer: the program crashes...Code:#include <cctype> #include <iostream> int main() { using namespace std; cout << isprint('ö') << endl; }
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
The valid range of arguments for isprint() goes from 0 through 127. In many compilers, the functions isprint(), isalnum(), etc. are implemented by indexing into an array of 128 chars. That doesn't mean that chars with values greater than 127 won't print, just that you had better not test them with isprint(), isalnum(), etc.Originally Posted by CornedBee
Regards,
Dave
I don't know what the standard says, but MS's documentation on their implementation says this:
The confusing part about this is that not every single-character character constant is within that allowed range, because some of them are negative integer values. Yes, it is clearly warned about, but hey, this means that I can't even write a text in my native language without resorting to tricks!The is routines produce meaningful results for any integer argument from –1 (EOF) to UCHAR_MAX (0xFF), inclusive. The expected argument type is int.
Caution For the is routines, passing an argument of type char may yield unpredictable results. An SBCS or MBCS single-byte character of type char with a value greater than 0x7F is negative. If a char is passed, the compiler may convert the value to a signed int or a signed long. This value may be sign-extended by the compiler, with unexpected results.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law