使用 SAPI.H - 为什么我的程序只说第一个词
Using SAPI.H - why does my program only speak the first word?
我希望能够在程序中输入一个字符串,并使用MS-SAPI让计算机说出该字符串。我在C++这样做。这是我的代码:
#include "stdafx.h"
#include <sapi.h>
#include <iostream>
#include <string>
std::wstring str_to_ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
int main(int argc, char* argv[]) {
while(true) {
std::cout << "Enter some words: " << std::endl; std::cout << ">> ";
std::string text; std::cin >> text;
std::cout << "" << std::endl;
std::wstring stemp = str_to_ws(text);
LPCWSTR speech_text = stemp.c_str();
ISpVoice * pVoice = NULL;
if (FAILED(::CoInitialize(NULL))) {}
HRESULT hresult = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if (SUCCEEDED(hresult)) {
hresult = pVoice->Speak(speech_text, 0, NULL);
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
return TRUE;
}
}
问题是程序只说字符串的第一个单词,然后退出......我该如何解决这个问题?
输入
未正确读取。
std::cin >> text;
读取一个空格分隔的令牌后停止。如果输入是"我是现代少将的典范"。 std::cin >> text;
将在第一个空格停止阅读,并在text
中仅提供"I"。该行的其余部分保留在流中等待读取。
std::getline(cin, text);
可能更符合您想要的路线。 std::getline
将使用默认的行尾分隔符读取输入行末尾的所有内容。std::getline
的其他重载允许您指定分隔符,使其成为一个很好的通用分析工具。
相关文章:
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- 如果第一个元素包含任何零,则我的程序以不希望的方式运行
- 程序没有问 3 个问题,而是将我对第一个问题的回答用于其他两个问题
- 在代码块中运行我的第一个图形程序
- 为什么我的程序跳过第一个函数?(初学者C++)
- 我找不到第一个用户输入后立即退出程序的原因
- 初学者:循环单链表的第一个程序
- C++ 快板第一个显示程序 - 平移旋转刻度
- 程序不会读过文件中的第一个条目
- 程序在用户的第一个输入时不执行任何操作(跳过程序中的整个代码)
- 用 C::B 编译我的第一个应用程序
- 为什么第二个程序的输出和第一个程序不同
- 试图学习威纳皮.制作了第一个程序,必须向我显示一个窗口.CMD显示,但没有窗口
- 为什么第一个程序不起作用,而第二个程序有效?其次,为什么输出是它给出的
- C++ 初学者,第一个程序.查找速度和距离
- 使用 Gecode 运行第一个程序
- 需要帮助调试我的第一个程序,Visual Studio 找不到.exe文件
- 我试图在快板写一个基本的第一个程序
- 在Visual Studio中使用本地Windows调试器运行第一个程序。输出窗口显示大量"Cannot find or open the PDB file"