Text-to-speech pass string to Speak

Iā€™m new in programming, so need your help, just trying to make small 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". Can you help me figure out with this goal in this code:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#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 with marshal:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#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
Last edited on
Looks like the problem is you can't use marshal_as<String^>() to convert a std::string to .NET managed String.

Maybe you could try:
synth->Speak(line.c_str())
You can try this:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "stdafx.h"

using namespace System;
using namespace System::Speech::Synthesis;
using namespace System::IO;
using namespace System::Collections::Generic;

const char* FILE_NAME = "lines.txt";

String^ getUserInput() 
{
  String^ Str = Console::ReadLine();

  return Str;
}

List<String^>^ getFileLines(String^ filename)
{
  List<String^>^ filenames = gcnew List<String^>();

  filenames->AddRange(File::ReadAllLines(filename));
   
  return filenames;
}

int main()
{
  List<String^> ^lines = getFileLines(gcnew String(FILE_NAME));
  StreamWriter^ writer = File::AppendText(gcnew String(FILE_NAME));
  try
  {
    for (int n = 0; n < 10; n++)
    {
      Console::Write("Write: ");
      String^ input = getUserInput();
      if (!lines->Contains(input))
      {
        writer->WriteLine(input + Environment::NewLine);
        lines->Add(input);
        Console::WriteLine("Line {0} saved.", input);
        SpeechSynthesizer^ synth = gcnew SpeechSynthesizer();
        synth->Speak("Text saved");
      }
      else
      {
        SpeechSynthesizer^ synth = gcnew SpeechSynthesizer();
        synth->Speak("Line exist");
      }
    }
  }
  catch (Exception^ ex)
  {
    Console::WriteLine(ex->ToString());
  }
  finally
  {
    writer->Close();
  }
  return 0;
}


If you are serious then you should get yourself a book.
https://www.amazon.com/Visual-NET-Platform-Experts-Voice/dp/1590596404/ref=sr_1_12?s=books&ie=UTF8&qid=1470659290&sr=1-12&keywords=Stephen+fraser
It's a bit old but will teach you all the basics you need to know.
Last edited on
hello Thomas1965 thank you for your feedback, advice and solution, works good, only lack with writing to file, here file is empty, not sure why... but it is not important in this case, I'm understand that I just have to use input as variable to speak, synth->Speak("Text saved"); right?
Last edited on
hello liuyang thanks for feedback, not sure how to use it synth->Speak(line.c_str()) maybe you can show me on small example with my code, I've tried it but I got errors, maybe doing something wrong...
Last edited on
Have a look at the different constructors of the String class - maybe you will see the problem
https://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx
Topic archived. No new replies allowed.