Showing WideString variable into console

2019-08-01 16:22发布

问题:

I'm having problems showing a WideString into the console. I'm totally new on Builder C++ and C++ in general. Not sure if I need some headers or maybe the values shown when debugging could help. It seems that when doing

wcout << s;

it is showing the address instead of the "wchar array".

Here it is my code:

//---------------------------------------------------------------------------
#include <iostream.h>
#include <vcl.h>
#include <string>
#include <wstring.h>
#pragma hdrstop
//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{

    int a;
    WideString s;
    string str;

    cout << "Enter a: ";
    cin >> a;
    //to read the return
    cin.get();
    cout << "Enter str: ";
    cin >> str;
    //to read the return
    cin.get();
    cout << "\n";
    s = L"HELLO";
    wcout << s;
    cout << "\n\n";
    wcout << L"BYE";
    cout << "\n\nPress any key to continue...";
    cin.get();

    return 0;

    }
//---------------------------------------------------------------------------

And that is the output:

Enter a: 4
Enter str: potato

2fb0b4

BYE

Press any key to continue...

回答1:

You're passing a WideString to wcout. WideString is a whole class that contains and operates on wide characters, not just a string. Use the c_bstr method of WideString to get to the actual character string.

WideString str;
str = L"HELLO";
wcout << s.b_cstr();