Member Avatar for Jackk123
#include "stdafx.h"
#include <iostream>
using namespace std;

int main ()
{
    int a, b, c;    
    cout << "š";
    cin >> a;

    cout << "Č";          
    cin >> b;

    c = a+ b;
    cout << "Ć" << c << endl;
    return 0;                 
}

So i tried using characters from my own language such as: š,Č,Ć
But they don't work, and i know that I have to add something to make it work.

Can someone please help me with this?

Recommended Answers

All 4 Replies

The main thing I am seeing here is that you are trying to save the characters to int variables, rather than char. While it is true that the standard char type is only 8 bits wide, and thus would not be able to hold the multi-byte characters, using an int means that the character's bytes get interpreted as their numeric value, rather than as a glyph.

The solution is to use the wide character type, wchar_t. This allows you to use a multi-byte character, with the specific size and encoding defined as compiler and system dependent. Along with this, you should use the wide-character versions of the input and output streams (std::wcin and std::wcout) rather than the standard character streams (std::cin and std::cout). This ensures that the wide characters are in fact interpreted as wide by the stream. You also need to use the capital L before any character or string literals, to indicate that they are wide literals. Thus, your code should look like this:

#include <iostream>
using namespace std;

int main ()
{
    wchar_t a, b, c;
    wcout << L"š";
    wcin >> a;
    wcout << L"Č";
    wcin >> b;
    c = a + b;
    wcout << L"Ć" << c << endl;
    return 0;
}

However, even this does not ensure that the characters will display correctly! Recall what I said about character encoding being defined as system dependent by the language standard? Well, the system first of all has to be able to display the characters, which isn't likely to be true with a console application without some adjustment of the console settings. In the case of the Windows console, the default is the PC OEM character set, but this can be changed by changing the code page. Whether there is a code page that matches the encoding you're using is another question; while there are indeed Unicode-8 and Unicode-16 code pages, they will only help you if you are yourself using either UTF-8 or UTF-16.

Finally, not all compilers will allow non-ASCII characters in source code, an even if yours does, the encoding which the compiler uses may not match that which the character was originally in. Thus, while it may look like you have one character in your editor, the compiler may treat it as a completely different character. I think - though I haven't been able to confirm this - that Visual C++ currently assumes UTF-8 as the default encoding.

Member Avatar for Jackk123

I tried compiling the code, it just displayed a blank cmd window?

Yeah, that's what I expected, unfortunately. As I said, you need to be able to change the code page first, before you can be sure it will display the given glyphs. If you add the line SeConsoleOutputCP(65001); to the top of the program, it should do this for you.

#include <iostream>
using namespace std;

int main ()
{
    UINT old_cp = GetConsoleOutputCP();
    SetConsoleOutputCP(65001);

    wchar_t a, b, c;
    wcout << L'š';
    wcin >> a;
    wcout << L'Č';
    wcin >> b;
    c = a + b;
    wcout << L'Ć' << c << endl;
    SetConsoleOutputCP(old_cp);
    return 0;
}

However, I've done some further checking, and it seems that even if you're able to do that, it might not work with the 'standard' wide streams; apparently, they aren't properly implemented in GCC, at least, and I don't know if they are in Visual C++ either. You may need to use some Windows specific functions for this purpose, instead:

#include "windows.h"

int main()
{
    HANDLE stdout = GetStdHandle(STD_OUTPUT_HANDLE);
    if(INVALID_HANDLE_VALUE == stdout) return 1;


    UINT codepage = (UINT) 65001;
    SetConsoleOutputCP(codepage);


    wchar_t *unicode = L"šČĆ";

    int lenW = wcslen(unicode);
    int lenA = WideCharToMultiByte(codepage, 0, unicode, lenW, 0, 0, NULL, NULL);
    char *ansi = new char[lenA + 1];

    WideCharToMultiByte(codepage, 0, unicode, lenW, ansi, lenA, NULL, NULL);
    WriteFile(stdout, ansi, lenA, NULL, NULL);

    delete[] ansi;

    return 0;
}

This is a bit of a hack, but it should display the characters in question.

Member Avatar for Jackk123

The thing is those characters are normal letters in my language. I put them inside of cout for the sake of understanding what i need, how would i approach this on a full sentance?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.