c++arrayscomparewchar

Compare two wchar arrays within nested for loops?


hope someone can help me figure this out i´m kind of new to C++ and programing..

I have 2 different arrays first one is Alphabet from a-z second is a integer array filled with virtual keycodes representing a-z.

the functions takes in a const wchar* as parameter that goes in to a stringstream and from there in to an array called buffer and then trying to compare Buffer[0] with Letter[0] if equal then simulate keypress with one of the keycodes, then compare next letter from string and keep going.. I cant se why this is not working haha.

Could there be a easier solution?

    unsigned short KeyCodes[26] = { 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A };
wchar_t Letter[27] = L"abcdefghijklmnopqrstuvwxyz";

void WriteSimulator(const wchar_t* String)
{
    wchar_t Buffer[256];
    ss << String;
    ss >> Buffer;

    for (int a = 0; a < wcslen(Buffer); a++)
    {
        for (int b = 0; b < 27; b++)
        {
              /*Here is what i did wrong!
                Comparing as strings instead of just char.*/ 
            if (wcscmp(&Buffer[a], &Letter[b]) == 0)
            {
                KeyPress(KeyCodes[b], false);
                break;
            }
        }
    }

}
//did write this from memory so it may be something missing...
//Solution!

void WriteSimulator(const wchar_t* String)
{
    wchar_t Buffer[256];
    ss << String;
    ss >> Buffer;

    for (int a = 0; a < wcslen(Buffer); a++)
    {
        for (int b = 0; b < wcslen(Letter); b++)
        {
            if (Buffer[a] == Letter[b])
            {
                KeyPress(KeyCodes[b], false);
                break;
            }
        }
    }
}

Solution

  • First, you have no definition in your code for the variable ss! But, assuming that's just a typo, then the real problem with your code is the line:

    if (wcscmp(&Buffer[a], &Letter[b]) == 0)
    

    where you are comparing the strings that start at Buffer[a] and Letter[b].

    To just compare the actual values of the single characters at each position, use:

    if (Buffer[a] == Letter[b])
    

    Hope this helps.