Trouble with special characters - c++

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.

Related

Enable the writing of ANSI escape codes on a file?

I am struggling with a problem. I searched all around the web and StackOverflow website and found similar questions, but none of them provided me the answer I am searching.
I am on a Linux system (Ubuntu) and basically want to know how to write an ANSI escape code in an output file. For example, if I want to write a red string on the terminal I do:
cout << "\033[31m" << "Red string";
and it works. But if I want to write it on a .rtf file for example:
#include <iostream>
#include <fstream>
using namespace std;
ofstream os( "this_file.rtf" );
os << "\033[31m" << "Red string";
os.close();
it doesn't work and output in the file something like:
#[31mRed string
is there a way to enable the writing of an ANSI escape code on an output file like that one? Thanks.
After all your answers and weeks of practice, the solution for this answer is pretty obvious and is the following: file redirection of ANSI escape sequences manipulation depends on the kind of file you are writing in and you have to manually set the way in which you want to translate the ANSI into the output file, depending of course also on the file format you are considering.

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

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

how to get list of all files in a directory and sub directories(full file path)

how to get list of all files in a directory and sub directories(full file path), And also show it if the file name is in Russian and Arabic
I did a lot of searching on Google but did not find anything that would solve my problem, any help is appreciated
It is better to include what you have tried so far to solve your problem. That way we can help you debug your own code which can be very good for you.
However, C++17 made iterating over directories very easy through the directory iterator. Read more about the directory iterator here and see the code below:
#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
std::string path = "/path/to/directory";
for (const auto & entry : fs::directory_iterator(path))
std::cout << entry.path() << std::endl;
}
Now second part of your question: you have entries paths, you can extract the file name out of it. Then, suppose you have two dictionaries one Russian and the other is Arabic. If you iterate over the filename character by character and check every time whether it is in the Russian dictionary or the Arabic one, you get the idea!
What you need to know is Arabic, Russian, whatever character has a Unicode code point (What's unicode?); meaning there is a unique value for it. Computers are good with 0s and 1s but not human readable characters (ASCII was made to solve this specific problem). If you are familiar with ASCII, you can consider Unicode as more inclusive character encoding standard. For instance see this page for Arabic characters encoding.
PS: Use hashtables implementations (instead of arrays) for your dictionary, since it has an amortized O(1) lookup.

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
}