Here is were i get the error.
To explain, i want to print the → character which according to http://www.endmemo.com/unicode/unicodeconverter.php
The code is 2192. but i may be using the wrong code if so what is the right way to print → .
int _tmain(int argc, _TCHAR* argv[])
{
UINT oldcp = GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8);
cout<<"\x2192"<<endl;
SetConsoleOutputCP(oldcp);
return 0;
}
A char on your platform is 8 bits. Your code part "\x2192" tries to put 16 bits in it. What will not fit, so you get the warning.
You possibly meant several characters, like "\x21\x92" or "\x92\x21"? That creates a valid string with two chars (+ the 0). You may still adjust it to have the proper value if comments are correct.
From the use of _tmain and SetConsoleOutputCP I guess you are mostly about Windows. I'm afraid I don't know much about that; hopefully someone who knows more about that specific case will chime in, but this program generates the output you're looking for in a quick test I tried here with a UTF-8 terminal. Here's the program:
#include <iostream>
int main(void)
{
std::cout << "\xE2\x86\x92" << std::endl;
return 0;
}
And example output:
$ make example && ./example
c++ example.cpp -o example
→
I just directly output the UTF-8 encoding of the → character.
Equivalently (at least for clang):
#include <iostream>
int main(void)
{
std::cout << "→" << std::endl;
return 0;
}
Related
I write an application, which get a number from args.
ex:./app --addr 0x123 or ./app --addr 123
I don't know which api can identify if the parameter is hex or decimal and come up with the correct data?
Thanks.
One way of getting the arguments is through argc and argv in
int main(int argc, char* argv[]) {
// Some code here.
return;
}
This will give you an array with all the arguments as strings. For example:
#include <iostream>
int main(int argc, char* argv[]) {
for (int i{ 0 }; i < argc; i++)
std::cout << "Argument " << i << ": " << argv[i] << std::endl;
return;
}
will give you this output
$ ./app --addr 0x123
Argument 0: ./app
Argument 1: --addr
Argument 2: 0x123
Now, parsing the input is a bit more complicated. You could create your own logic to evaluate the arguments once you have the arguments as strings.
You could also use libraries. For example, you could use argparse by p-ranav. GitHub repository here. It is an easy to use library so you don't have to worry about positioning of the arguments.
Answering you question, how do you know if the input is hex or decimal? You could check if the argument after --addr starts with 0x or not. That could be one way, but you have to also consider the case when someone inputs a hex value without the 0x, like ./app --addr A2C. Also, documenting acceptable formats and printing a --help message can ameliorate this.
You have to think about your specific application, but some steps once you have the string after --addr that you could do to check input is:
Does it start with 0x?
If not, does it contains characters 0-1 and A-F? Is this allowed?
Is it in a valid range?
For example, 0xFFFFF is 1,048,575. But you may only want addresses up to 0xFFFF (65,535). What should happen in these cases?
Hope it helps!
P.S.: These are all implementations in C++.
strtol will do the job for you
https://www.cplusplus.com/reference/cstdlib/strtol/
I'm trying to print ASCII characters, but in the output I only see a little box. For example, ASCII 179 is a | character, but it doesn't print. Instead, it prints:
My code:
int main(int argc, char** argv) {
int a[] {179,180,191,192,193,194,195,196,197,217,218,32};
char b = a[2];
std::cout << b;
return 0;
}
How can I resolve this problem?
Note, when I use this code, the output prints the characters correctly:
std::cout << "┐"
But if I use the ASCII character, it prints a box instead.
Edit: To add... even when I output the characters to Notepad, I get the same result.
The problem seems to be an encoding issue. NetBeans by default, is not printing data with UTF-8 encoding.
Take a look at this tutorial to change NetBeans' default encoding.
I'm relatively new to c++, and I'm just making some simple programs.
One of my programs will need to open up a different .exe file. This .exe file will ask for 2 or 3 file names, then run and exit.
Just to test this out, I created a simple_calc.exe file, that ask for value1, then value2 then multiply them.
So let's say I want to create a "call_other_file.exe" and automatically run "simple_calc.exe" with value1 and value2 taken from "call_other_file.exe"s file.
How can I proceed to do that?
After searching a bit, i see something like:
system("simple_calc.exe -val1 -val2").
But that doesn't work for me. Or I'm not sure how to define val1 and val2...
edit: the program I want to access (simple_calc.exe in the example), I can not change the code there, I don't have access to it's .cpp file.
Any suggestions?
i see something like: system("simple_calc.exe -val1 -val2")
It should work. The reason why that didn't work might be that you didn't put int argc, char* argv[] in your "simple_calc"'s main function.
//simple_calc.cpp
int main(int argc, char* argv[])
{
if (argc == 2)
{
cout << "Error: At least two argument must be exist.";
return -1;
}
return 0;
// After that, you can use 'argv' arguments to calculate what you want to calculate. Also, in order to calculate them, you also need to convert 'argv' to integer or double, since they are string or char array.
}
For more information on C++ comand-line arguments see this site below:
http://www.tutorialspoint.com/cprogramming/c_command_line_arguments.htm
When you add the command-line arguments to your simple_calc.cpp, you can use this simple_calc.exe val1 val2. To call another program from your C++ program, you need system calls. In your main function;
system('simple_calc.exe')
You also need to know about how windows console work, if you are using windows. If you are using linux, its console commands are also different.
My suggestion is that you must first learn how consoles work different operating systems.
There are various ways to have two separate programs communicate with one another, but the system() function is probably the simplest; it just runs a string of text as though it were entered in a console window.
Basic example
The main program simply runs the other program with two arguments: hello and world.
/* main.c */
#include <stdlib.h>
int main() { return system("./other hello world"); }
The two arguments will be passed to the other program as C-style strings via the argv[] array. Note that they will be stored as the second and third elements, since the first item (argv[0]) will be the path to the program itself.
/* other.c */
#include <stdio.h>
int main(int argc, char *argv[])
{
int i = 0;
for (i = 1; i < argc; ++i) { printf("%s\n", argv[i]); }
return 0;
}
The output of the above programs will look like this:
hello
world
Process returned 0 (0x0) execution time : 0.004 s
Press ENTER to continue.
Arguments with spaces
As you may have noticed, arguments are space-separated. If you need to pass a string with spaces as a single parameter, you'll need to enclose it in quotes:
/* main.c */
#include <stdlib.h>
int main() { return system("./other \"this is all one string\" \"and so is this\" bye"); }
This would produce the following result:
this is all one string
and so is this
bye
Process returned 0 (0x0) execution time : 0.004 s
Press ENTER to continue.
Non-string arguments
Since the arguments are given as strings, you'll need to convert them into numbers (using conversion functions such as strtod(), atoi(), or atof()) if necessary. Here's an updated version of main.c:
/* main.c */
#include <stdlib.h>
int main() { return system("./other 4 6"); }
...and here's the corresponding other.c file:
/* other.c */
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int total, i;
for (total = 0, i = 1; i < argc; ++i) { total += atoi(argv[i]); }
printf("TOTAL: %d\n", total);
return 0;
}
This produces the following output:
TOTAL: 10
Process returned 0 (0x0) execution time : 0.005 s
Press ENTER to continue.
I'm trying to make simple multilingual Windows console app just for educational purposes. I'm using c++ lahguage with WxDev-C++/minGW 4.6.1 and I know this kind of question was asked like million times. I'v searched possibly entire internet and seen probably all forums, but nothing really helps.
Here's the sample working code:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
/* English version of Hello world */
wchar_t EN_helloWorld[] = L"Hello world!";
wcout << EN_helloWorld << endl;
cout << "\nPress the enter key to continue...";
cin.get();
return 0;
}
It works perfectly until I try put in some really wide character like "Ahoj světe!". The roblem is in "ě" which is '011B' in hexadecimal unicode. Compiler gives me this error: "Illegal byte sequence."
Not working code:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
/* Czech version of Hello world */
wchar_t CS_helloWorld[] = L"Ahoj světe!"; /* error: Illegal byte sequence */
wcout << CS_helloWorld << endl;
cout << "\nPress the enter key to continue...";
cin.get();
return 0;
}
I heard about things like #define UNICODE/_UNICODE, -municode or downloading wrappers for older minGW. I tried them but it doesn't work. May be I don't know how to use them properly. Anyway I need some help. In Visual studio it's simple task.
Big thanks for any response.
Apparently, using the standard output streams for UTF-16 does not work in MinGW.
I found that I could either use Windows API, or use UTF-8. See this other answer for code samples.
Here is an answer, not sure this will work for minGW.
Also there are some details specific to minGW here
I can see there are many questions related to strings and wide strings. But as none of them gives me information I am looking for... I am posting a new question.
I have this code...
std::string myName("vikrant");
std::cout<<myName<<std::endl;
std::wstring myNameHindi = L"मुरुगन";
std::wcout<<myNameHindi<<"-----"<<myNameHindi.size()<<std::endl;
std::wcout<<L"मुरुगन"<<std::endl;
std::string myNameHindiS = "मुरुगन";
std::cout<<myNameHindiS<<"-----"<<myNameHindiS.size()<<std::endl;
when I compile & run this code on my RHEL box(... (connected through ssh, running gcc 4.1.2) I get this o/p (please note middle two lines are not printing properly)
vikrant
.A0A(-----6
.A0A(
मुरुगन-----18
While on my apple laptop and one of FreeBSD(through ssh) box I dont get o/p from w_* code. I just get first and last cout executed
vikrant
मुरुगन-----18
My understanding was that if not specified these strings will be treated as UTF 8. and if string can handle it wstring will handle as well. Is there something wrong in that approach?
Some addon questions are...
is it just a display problem? or wstring is not reliable on linux?
Any additional information may help as well.
EASIEST WAY
Here is what are you looking for, #include <clocale> and for example, to have Turkish, just simply type setlocale(LC_ALL,"Turkish"); to your code.
You can also just leave it as setlocale(LC_ALL,""); it will use your local language.
#include <iostream>
#include <clocale>
int main(){
setlocale(LC_ALL,"Turkish");
std::cout << "I can type any Turkish character like ÖöÇ窺İiĞğÜüİ, anything.\n" << std::endl;
system("pause");
return 0;
}
SOME OTHER WEIRD WAY TO DO IT
This is a really weird way to do it but it will also work.
#include <iostream>
int main()
{
std::string characters="IiĞğÇçÜüŞşÖö";
int i;
for ( i=0; i<characters.length(); ++i ){
characters[i]=(characters[i]==-2) ? 159:characters[i]; //ş
characters[i]=(characters[i]==-3) ? 141:characters[i]; //ı
characters[i]=(characters[i]==-4) ? 129:characters[i]; //ü
characters[i]=(characters[i]==-10) ? 148:characters[i]; //ö
characters[i]=(characters[i]==-16) ? 167:characters[i]; //ğ
characters[i]=(characters[i]==-25) ? 135:characters[i]; //ç
characters[i]=(characters[i]==-34) ? 158:characters[i]; //Ş
characters[i]=(characters[i]==-35) ? 152:characters[i]; //İ
characters[i]=(characters[i]==-36) ? 154:characters[i]; //Ü
characters[i]=(characters[i]==-42) ? 153:characters[i]; //Ö
characters[i]=(characters[i]==-48) ? 166:characters[i]; //Ğ
characters[i]=(characters[i]==-57) ? 128:characters[i]; //Ç
std::cout << characters[i] << " ";
}
}