Visual studios changes my '£' to '?' during runtime [duplicate] - c++

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";

£ 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.

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.

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";

I've been trying to answer the same question in C.
One of the answers in Displaying wide chars with printf gives a clue to a possibly more robust method. When using the Code::Blocks editor neither \x9C or \xA5 works. This might be due to the default POSIX local being set for its console output, this is ASCII-only, so no characters above \x7f work. To set the locale to UTF-8 we can use setlocale(LC_ALL,"").
#include <stdio.h>
#include <locale.h>
int main() {
setlocale(LC_ALL,"");
printf("%lc",163);
}
In C++ we can use
setlocale(LC_ALL,"");
cout << char(163) << 123.45 << endl;
With a UTF-8 locale set we need to convert the unicode character \x9c into the two-byte UFT-8 encoding. Which is \xc2a3. So
printf("\xc2a3%.2f",123.456);

Related

C++ - A few quetions about my textbook's ASCII table

In my c++ textbook, there is an "ASCII Table of Printable Characters."
I noticed a few odd things that I would appreciate some clarification on:
Why do the values start with 32? I tested out a simple program and it has the following lines of code: char ch = 1; std::cout << ch << "\n"; of code and nothing printed out. So I am kind of curious as to why the values start at 32.
I noticed the last value, 127, was "Delete." What is this for, and what does it do?
I thought char can store 256 values, why is there only 127? (Please let me know if I have this wrong.)
Thanks in advance!
The printable characters start at 32. Below 32 there are non-printable characters (or control characters), such as BELL, TAB, NEWLINE etc.
DEL is a non-printable character that is equivalent to delete.
char can indeed store 256 values, but its signed-ness is implementation defined. If you need to store values from 0 to 255 then you need to explicitly specify unsigned char. Similarly from -128 to 127, have to specify signed char.
EDIT
The so called extended ASCII characters with codes >127 are not part of the ASCII standard. Their representation depends on the so called "code page" chosen by the operating system. For example, MS-DOS used to use such extended ASCII characters for drawing directory trees, window borders etc. If you changed the code page, you could have also used to display non-English characters etc.
It's a mapping between integers and characters plus other "control" "characters" like space, line feed and carriage return interpreted by display devices (possibly virtual). As such it is arbitrary, but they are organized by binary values.
32 is a power of 2 and an alphabet starts there.
Delete is the signal from your keyboard delete key.
At the time the code was designed only 7 bits were standard. Not all bytes (parts words) were 8 bits.

Trouble with special characters

First of all, I'm a newbie in programming so you might have to be patient with me. The thing is, I'm writing a program that basically gets input and uses it to output some information plus the inputs in a .doc.
My problem is that I have some constant strings that output in a screwed up way when I use special characters like é í ó ã õ º ª.
I was able to fix it by adding setlocale(LC_ALL, ("portuguese")) but then I screwed my outputs of inputs (aka variable strings) that doesn't print special characters any more. Any clues how i can solve this? I've already tried wstrings and looked everywhere but couldn't find a single solution.
I can show my code here if it helps.
Here is an example of my problem:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
wcout << "Enter special characters like éíó: ";
getline (cin, a);
cout << a;
}
I can't make the constant string and the variable string output correctly in the console at the same time.
You are probably using Windows. The Windows' Command Prompt default encoding is CP850, this encoding is rarely used anywhere else and it will display most special symbols differently from what you usually see in your favorite text editor. You can try to use the Windows APIs SetConsoleOutputCP(1252); and SetConsoleCP(1252); to change to CP1252, an encoding that is somewhat more compatible and should display those symbols the same way you see in the editor. You will need the #include <windows.h>, if its available.

Printing arabic string in c++

I'm beginner in C++
I'd like to print Arabic statement in C++ using Borland C, but I failed , I also tried to save the file as UTF-8 but it didn't help.
So please if any one knows anything about this or about what is the problem or how to configure the compiler to print Arabic please help me.
#include<iostram.h>
#include<conio.h>
void main()
{
clrscr();
char x [5] = {'ا','ح','م','د'};
for(int i = 0; i< 5; i++)
cout << x[i];
getche();
}
First of all, you are assuming that your source code can contain Arabic characters. This is a very bad idea, and depends on the assumption that the compiler is interpreting your source file in the same code page as your editor is writing it in.
The safest way to handle Arabic or other arbitrary Unicode in Windows C++ is to compile with _UNICODE, declare variables of wchar_t (and friends), and use Unicode constants like '\u6041' for your Arabic characters. If you must do things with 'char', you will have to come up with the multi-byte \x sequences in the right code page for your Arabic characters, and deal with the fact that a single char can't hold an Arabic character in UTF-8.
Finally, since you are using cout, this will only show you Arabic if the current code page of your DOS box is an Arabic code page.
If your BorlandC++ is under DOS
By default you have not any character set to show it as Arabic. But those days, there were applications which change extended ASCII characters to other languages such as Arabic, Persian, ... .
Steps you should do:
If you are using Windows Vista/7+, first you should use DosBox (you need Fullscreen-mode)
You must change the default ASCII font table in memory
Something like vegaf.com which defines Persian/Arabic alpha-beta
Note: UTF-8 is undefined for this system
C++11 is the only C++ standard that can offer native support for UTF-8 ( and other UTF charsets ) encoding.
in pre C++11 releases you can simply use a third part library if you need UTF-8 support like this one .

Will cin recognize \n typed in from keyboard as a newline character?

I am a beginner for C++ so I'm sorry if this question sounds stupid..
I made this little program to help me get familiar with the properties of cin:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string next;
cout<<"Enter your input.\n";
cin>>next;
cout<<next;
return 0;
}
When I typed in \n from the keyboard input, I was returned \n.
Also, when I changed the variable next from a string to a character and gave it the same input as above, I was returned only a \.
My question is: Why am I not returned with a new line instead? Doesn't cin recognize \n type in from keyboard as a newline character? Or is it just applicable to cout?
\n is an escape sequence in C++; when it appears in a character constant or a string literal, the two character sequence is replaced by the single character representing a new line in the default basic encoding (almost always 0x0A in modern systems). C++ defines a number of such escape sequences, all starting with a \.
Input is mapped differently, and in many cases, depending on the device. When reading from the keyboard, most systems will buffer a full line, and only return characters from it when the Enter key has been pressed; what he Enter key sends to a C++ program may vary, and whether the file has been opened in text mode or binary mode can make a difference as well—in text mode, the C++ library should negotiate with the OS to ensure that the enter key always results in the single character represented by \n. (std::cin is always opened in text mode.) Whether the keyboard driver does something special with \ or not depends on the driver, but most don't. C++ never does anything special with \ when inputting from a keyboard (and \n has no special meaning in C++ source code outside of string literals and character constants).
If you need your program to recognize \n as a new line character at input you can check this out:
https://stackoverflow.com/a/2508814/815812
What Michael say is perfectly correct.
You can try out in similar way.
Technically speaking, this depends on things outside your program, but assuming your terminal simply passes the individual bytes corresponding to the '\' and 'n' characters (which I think any sane one will), then the behavior you're seeing is expected.
"\n" is nothing more than a shortcut added to the programming language and environment to let you more easily represent the notion of the ASCII return key. It's not a character itself, just a command to tell the program to generate a non-printable character that corresponds to pressing the Enter key.
Let's say you're in Notepad or whatever and you press the Tab key. It tabs over a spot. Typing "\t" just enters the literal characters "\" and "t". Internally, whoever wrote Notepad had to say what it should do when the user pressed Tab, and he did so by using the mnemonic like
if(key == '\t') {
// tab over
}

Inserting a "£" sign in an output string in C++

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";
£ 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.
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.
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";
I've been trying to answer the same question in C.
One of the answers in Displaying wide chars with printf gives a clue to a possibly more robust method. When using the Code::Blocks editor neither \x9C or \xA5 works. This might be due to the default POSIX local being set for its console output, this is ASCII-only, so no characters above \x7f work. To set the locale to UTF-8 we can use setlocale(LC_ALL,"").
#include <stdio.h>
#include <locale.h>
int main() {
setlocale(LC_ALL,"");
printf("%lc",163);
}
In C++ we can use
setlocale(LC_ALL,"");
cout << char(163) << 123.45 << endl;
With a UTF-8 locale set we need to convert the unicode character \x9c into the two-byte UFT-8 encoding. Which is \xc2a3. So
printf("\xc2a3%.2f",123.456);