[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

C++ and International characters

How can I use

char *x="Some accented or international characters";
cout << x << endl;

In VC++ on console window it shows junk.

also I would like to accept accented characters as input from command line. (I know argc, argv and getopt)

Sample code will be helpful.
0
prashant_n_mhatre
Asked:
prashant_n_mhatre
  • 8
  • 6
1 Solution
 
nietodCommented:
You are using ASCII characters.  You need to switch to wide characters, like UNICODE characters, like

const wchar_t *x= L"Some accented or international characters";
cout << x << endl;
0
 
nietodCommented:
Note that I madee a few changes there.  First of allj, techically string literals are constants in C++.  This is true for both ASCII and widw character string literals.  Many compilers will still allow you to treat them as non-constant, but this is still a mistake.  And if you change this literals your will cause problems  Pluss someday the compiler will probably be updated and no longer allow it.  So pavoid problems and treat them as constants like

const char *somestring = "some text";

next, if you want to create a character or sting literal that is a wide character, use a "L" before the literal, like

wchar_t widechar = L"A";
wchar_t astring[10] = L"ABC"
0
 
nietodCommented:
>> I would like to accept accented
>> characters as input from command
>> line. (I know argc, argv and getopt)
that appears to be impossible in windows.

You can use GetCommandLineW(), the unicode version of GetCommandline().  This will return a unicode version of the command line.  However, I have been told that it does not handle non-ASCII characters, probably due to the DOS roots of the command system.  

You might try asking about this on the windows topic area, since this is a windows issue, not a C++ issue, but I don't think you will find any option.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
prashant_n_mhatreAuthor Commented:
Doesn't work. I tried following:

#include  <iostream >
using namespace std;

int main( void )
{
   const  wchar_t *x= L"Aba";
   cout << x << endl;
   return 0;
}

It gives:

0046B01C

as output. I'm using VC++ 6.0
0
 
prashant_n_mhatreAuthor Commented:
EE site doesn't show my accented characters. Actually L"Aba" is an accented string.
0
 
prashant_n_mhatreAuthor Commented:
Ok...let's ignore command line option...but what about console application - like the above program?
0
 
nietodCommented:
Opps.  I forgot the fact that you also need to output this.  The part I showed you was to define a wide character string.  But then to output it you have to use a wide character stream object, like wcout.  Like

const wchar_t *x= L"Some accented or international characters";
wcout << x << endl;

similarly there is a wide character input stream, wcin.
0
 
prashant_n_mhatreAuthor Commented:
still it doesn't give the correct output. It shows..
??b??

I guess we may need to set something that will give hint to the compiler that the next output stream is a wide character stream...
0
 
nietodCommented:
That should work fine.  It works for me on VC at least.

Note that for non-ASCII characters you will need to enter the proper unicode character.  If you are entering an extended ASCII character (from decomal 128 to 255) these characters might not coorespond to the unicode character you want.
0
 
DanRollinsCommented:
prashant_n_mhatre,
The problem is quite likely related to the font that is available when you output to the console.  
* In which language are you attempting to output?  
* Which font have you chosen for use in your console windows?  
* Which version of Windows are your running?  
* Is windows running with a default language of Enginsh?

Hi nietod,
The experts in this section have agreed to NOT lock questions.  That is, we all post Comments so that
the question remains visible to other experts.

It would seem like a good point-making strategy to lock questions immediately (in your case, less than
one hour after the post), but it deprives the question Asker of the additional expert input that might
solve his specific problem.  After several days of give-and-take, when the "real" underlying question
has been revealed, if your post is quite clearly the only possible correct solution, it may (*may*)
be appropriate to post an Answer.

In the future, please post Comments, like everybody else.  Thanks!

-- Dan
0
 
nietodCommented:
>> The experts in this section have agreed
>> to NOT lock questions.
That is absolutely untrue.

>>  the future, please post Comments,
>> like everybody else.  Thanks!
In the future, please notify customer service when you have a complaint.
0
 
prashant_n_mhatreAuthor Commented:
nietod:

I'm also using VC. Is there something to do in control panel? My language is set to 'English(United States)'.

If different font is required..what shall I download?
0
 
nietodCommented:
teh part I've answered is how to make a standard C++ program output wide character strings.  That is a standard C++ issue.  When it comes with how to handle how to make windows handle wide characters in a console window (which is not a c++ issue, it is a windows issue) I am mostly at a loss.

You might look into the windows API functions like SetconsoleCP() which sets the code page used to ditermine charactes that appear on the console window.  This part is not my area of expertise.

If necessary you might have to reject my answer to see if another expert can help.  However, your best bet is to ask this question on the windows topic area.  The C++ part has been solved, you now need to know how to make a console window show unicode characters, that is a windows question.
0
 
prashant_n_mhatreAuthor Commented:
There is no question of rejecting your answer. I'm looking for standard C++ only. Although I use VC++ to test sample programs, I work on Unix.

I have found a good starting point.

Thanks a lot !!!
0
 
nietodCommented:
I'm not sure what support UNIX has for wide characters and uncode.  UNIX is much older than these ideas.  However modern versions probably have support.  You may have to do things to set the code page or similar type things.  I'm not sure how standardized it will be though.  i.e. it may depend a lot on what brand of UNIX you are using.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now