Getting input and passing it to WinAPI function

So, my problem, brothers, is this:

I am trying to get input from the user, with geline. Like so:
1
2
string input = "";
getline(cin,input,'\n');


Then pass it to a funtion that takes a LPCWSTR. Like so:
 
hr = pVoice->Speak(input, 0, NULL); //Nevermind the usage or the specific funtion 

But, I cannot seem to be able to change a string to a LPCWSTR AND use it with getline. I don't see an obvious solution, but I think you guys can help!
Thanks!
Last edited on
Two options:

Option one: Don't use std::getline. Try instead std::cin.getline(), it saves it to a C-Style string (or widechar? Normally just use ascii...).

Option two:
You can change input into a C-Style string by using one of its member functions, like so:
 
hr = pVoice->Speak(input.c_str(), 0, NULL);
The Speak method you are using requires a wide string; that is, a wchar_t pointer. Normal std::string's will give you a char pointer, so you'll have to either widen it to a std::wstring then use the c_str() method or use the ASCII version (if it exists) of the Speak method (probably SpeakA()).

I'm assuming that this method is actually something from the WinAPI. You may also want to look at this article, since I suspect it may contain information related to what you're trying to do here.

http://www.cplusplus.com/articles/2w6AC542/
It take it that by Speak you mean the ISpVoice::Speak interface method which is part of Microsoft SAPI??

In that case, as it's a COM interface, there is not an ANSI entrypoint.

Have you tried using wcin and wstring instead of cin and string??

1
2
3
4
5
6
7
wstring input; // no need for for = L""; as the constructor
               // initializes it to L"" for you
getline(wcin, input); // no need for L'\n' as that's the default

wcout << L"Say : " << input << endl;

hr = pVoice->Speak(input.c_str(), 0, NULL); // still need to use c_str() 


Andy
Last edited on
WOW, guys, thanks for the help. I tried using getline with wstring, but didn't know I had to use wcin as well. Thanks. VERY helpful. Thank you!
New problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include <sapi.h>

using namespace std;

int main(int argc, char* argv[])
{
    ISpVoice * pVoice = NULL;

    if (FAILED(::CoInitialize(NULL)))
        return FALSE;

    HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
    if( SUCCEEDED( hr ) )
    {
		wstring input;
		while(true)
		{

			
			cout << "Enter text:\n";
			getline(wcin,input);

			hr = pVoice->Speak(input.c_str(), 0, NULL);
			pVoice->Release();
			pVoice = NULL;
		}
    
    }

    ::CoUninitialize();
    return 0;
}



This code fails (crashes) after the first time of correctly pronouncing the string. Help?

EDIT: Problem solved. The problem was that I was essentially closing the voice with
1
2
pVoice->Release();
pVoice = NULL;

after each speak and trying to use it again. I moved that code to after the loop.
Last edited on
Topic archived. No new replies allowed.