Reputation:
I’m new in programming, so need your help, just trying to make program with Text to Speech C++ in Visual Studio 2015 on Windows 10 using Speech Synthesizer object in a CLR Console application. But I can't figure out, how to get line through the variable "t" to speak not only synth->Speak("Line saved"); and synth->Speak("Line exist"); , but with "t" like this: "Line (text line) exist". So how can I pass a string to the Speak
function?
Can you help me figure out:
#include "stdafx.h"
#include <conio.h>
#include <Windows.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
using namespace System;
using namespace System::Speech::Synthesis;
using namespace System::IO;
const string FILE_NAME = "lines.txt";
vector<string> getFileLines(string file) {
ifstream in(FILE_NAME);
vector<string> lines;
for (string line; getline(in, line); ) {
lines.push_back(line);
}
return lines;
}
string getUserInput() {
string str;
getline(cin, str);
return str;
}
int main()
{
vector<string> lines = getFileLines(FILE_NAME);
ofstream fileOut(FILE_NAME, ios::app);
for (int n = 0; n < 10; n++)
{
cout << "Write: > ";
std::string t = getUserInput();
auto it = std::find(lines.begin(), lines.end(), t);
if (it == lines.end()) {
fileOut << t << endl;
lines.push_back(t);
cout << "Line \"" << t << "\" saved.\n";
SpeechSynthesizer^ synth = gcnew SpeechSynthesizer();
synth->Speak("Text saved");
}
else
{
cout << "LIne \"" << t << "\" exist.\n";
SpeechSynthesizer^ synth = gcnew SpeechSynthesizer();
synth->Speak("Line exist");
}
}
cout << endl;
getUserInput();
return 0;
}
and this way with marshal:
#include "stdafx.h"
#include <conio.h>
#include <Windows.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <msclr\marshal_cppstd.h>
using namespace msclr::interop;
using namespace std;
using namespace System;
using namespace System::Speech::Synthesis;
using namespace System::IO;
const string FILE_NAME = "lines.txt";
vector<string> getFileLines(string file) {
ifstream in(FILE_NAME);
vector<string> lines;
for (string line; getline(in, line); ) {
lines.push_back(line);
}
return lines;
}
string getUserInput() {
string str;
getline(cin, str);
return str;
}
int main()
{
vector<string> lines = getFileLines(FILE_NAME);
ofstream fileOut(FILE_NAME, ios::app);
for (int n = 0; n < 10; n++)
{
cout << "Write: > ";
std::string t = getUserInput();
auto it = std::find(lines.begin(), lines.end(), t);
if (it == lines.end()) {
fileOut << t << endl;
lines.push_back(t);
cout << "Line \"" << t << "\" saved.\n";
String^ str = marshal_as<String^>(str);
std::string line = "Line " + t + " exists!";
synth->Speak(marshal_as<String^>(line));
}
else
{
cout << "LIne \"" << t << "\" exist.\n";
String^ str = marshal_as<String^>(str);
std::string line = "Line " + t + " exists!";
synth->Speak(marshal_as<String^>(line));
}
}
cout << endl;
getUserInput();
return 0;
}
I got this errors:
Error C4996 'msclr::interop::error_reporting_helper<_To_Type,_From_Type,false>::marshal_as': This conversion is not supported by the library or the header file needed for this conversion is not included.
Error C2065 '_This_conversion_is_not_supported': undeclared identifier X_TTS2 c:\program files (x86)\microsoft visual studio 14.0\vc\include\msclr\marshal.h 219
Upvotes: 1
Views: 2513
Reputation: 598134
Per the documentation:
If you try to marshal a pair of data types that are not supported,
marshal_as
will generate an error C4996 at compile time. Read the message supplied with this error for more information. The C4996 error can be generated for more than just deprecated functions. One example of this is trying to marshal a pair of data types that are not supported
The supported conversions are documented:
The marshal_as()
function supports marshaling a std::string
to a System::String^
if you use marshal_cppstd.h
, which your example does:
#include <msclr\marshal_cppstd.h>
std::string line = "Line " + t + " exists!";
synth->Speak(marshal_as<String^>(line));
So the error you show does not make sense, unless it is referring to this statement:
String^ str = marshal_as<String^>(str);
You are trying to marshal a String^
to a String^
, which is not a supported marshal conversion. Also, using a variable in the same statement that declares it is undefined behavior anyway, so you need to remove the statement completely as it is useless.
Alternatively, marshal_as()
supports marshaling a const char*
if you use marshal.h
:
#include <msclr\marshal.h>
std::string line = "Line " + t + " exists!";
synth->Speak(marshal_as<String^>(line.c_str()));
Upvotes: 1