Question

My problem is that no method that used to work on string works on wstring. So I'm asking how I could easily clear wstring for aesthetical purpose.

My code right now:

    while (!foundRightOne)
    {
        wstring cTitle;
        ForegroundWindow = GetForegroundWindow();
        cout << "FRGW  " << ForegroundWindow << endl;

        int len = GetWindowTextLengthW(ForegroundWindow) + 1;
        wchar_t * windowTitle = new wchar_t[len];
        GetWindowTextW(ForegroundWindow, windowTitle, len);
        title += windowTitle;
        // OUTPUT
        cTitle = L"Title: ";
        cTitle += title;
        wcout << cTitle << endl;
        cTitle = ' ';
        //OUTPUT
        keyPress = getchar();
        system("CLS");
        if (keyPress == 'y' || keyPress == 'Y')
        {
            foundRightOne = true;
        }

    }

Basically it loops while I press y or Y, I press when I see the right cTitle and after ~20 cycles cTitle gets fully filled with text from last cycles.

Was it helpful?

Solution

std::wstring::clear should work, as both it and std::string are std::basic_strings. Check out std::basic_string documentation if you're somehow having trouble.

#include <iostream>

int main()
{
    std::string regularString("regular string!");
    std::wstring wideString(L"wide string!");

    std::cout << regularString << std::endl << "size: " << regularString.size() << std::endl;
    std::wcout << wideString << std::endl << "size: " << wideString.size() << std::endl;

    regularString.clear();
    wideString.clear();

    std::cout << regularString << std::endl << "size: " << regularString.size() << std::endl;
    std::wcout << wideString << std::endl << "size: " << wideString.size() << std::endl;
}

Output:

regular string!
size: 15
wide string!
size: 12

size: 0

size: 0

Here's an ideone link to that code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top