Showing WideString variable into console

。_饼干妹妹 提交于 2019-12-11 05:25:18

问题


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


来源:https://stackoverflow.com/questions/9398269/showing-widestring-variable-into-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!