Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
,i want to get the input for switch through voice recognition
#include <iostream>
#include <string>
using namespace std;
int main()
{
string ch;
cin>>ch;
switch(ch)
{
case "1":
{
// do something here
}
}
system("Pause");
return 0;
}


What I have tried:

#include <stdafx.h>
#include <sapi.h>

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 ) )
    {
        hr = pVoice->Speak(L"Hello world", 0, NULL);
        pVoice->Release();
        pVoice = NULL;
    }

    ::CoUninitialize();
    return TRUE;
}

i have done tts by this code
Posted
Updated 21-Dec-17 3:01am

The C/C++ switch statement does not work with strings. The condition must be an integral or enumeration type (see switch statement - cppreference.com[^]).

But you can use single characters:
char ch;
cin >> ch;
switch (ch)
{
case '1':
    // do something here
    break;
}

Otherwise you have to use if - else [if] blocks or a loop checking against the items of a lookup table.
 
Share this answer
 
After correcting the error in your code
C++
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice); // removed semicoln aftger pVoice

it works.

You now need to explain what more you are trying to do.
 
Share this answer
 
Comments
Member 13588042 23-Dec-17 10:15am    
actuallty the tts works perfectly fine but i want to do speech recognition using sapi
Richard MacCutchan 23-Dec-17 11:48am    
Then you need to study the documentation to learn how to do it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900