C++ Text-To-Speech undefined errors - c++

#include <windows.h>
#include <mmsystem.h>
#include <iostream>
#include <string.h>
#include <fstream>
char *sounds[] = {"d.wav","ai.wav","v.wav","i.wav"};
int main()
{
char input[20];
int k;
int i = 0;
std::cin >> input;
while (input[i])
{
k = input[i] - 'a';
PlaySound(TEXT(sounds[k]), NULL, SND_ASYNC);
}
system("pause");
}
In an attempt to make a text to speech program, I have come across this problem.
int k, reads the input and plays the .wav file connected with the string that is read. The problem arises from PlaySound(TEXT(sounds[k]), NULL, SND_ASYNC);. The errors read: (IntelliSense: identifier "Lsounds" is undefined) and (error C2065: 'Lsounds' : undeclared identifier). Which both seem to mean the same thing, however, I can not find the source of the problem or whats causing it. How was/is Lsounds undefined and how would I fix it?

It's TEXT(sounds[k]) that's causing the issue. TEXT() is a macro that expands to L followed by what you type inside, so that's the equivalent of Lsounds[k], hence the error.

The PlaySound API is used for playing WAVE files. You should use SAPI COM interface for TTS:
CComPtr <ISpVoice> cpVoice;
//Create a SAPI Voice
HRESULT hr = cpVoice.CoCreateInstance(CLSID_SpVoice);
if(SUCCEEDED(hr))
{
cpVoice->Speak(L"Hello World", SPF_DEFAULT, NULL);
}

Related

ifstream is crashing on x64 bit platform

i am trying to use ifstream to open a txt file and read data from that file with ifstream, while that code works just fine on x86 bit platform, it causing weird application crash when using it on x64 bit platform.
ifstream font;
font.open(filename);
if ( font.fail() )
{
return false;
}
// Used to read 95 ASCII characters in the text file
for ( int i = 0; i < 95; i++ )
{
font.get(temp);
while (temp != ' ')
{
font.get(temp);
}
font.get(temp);
while (temp != ' ')
{
font.get(temp);
}
font >> m_Font[i].left;
font >> m_Font[i].right;
font >> m_Font[i].size;
}
Removing font.fail() check causes the program to be stuck at font.get(temp); which means that font.fail() is causing the crash of the program, if someone knows a solution to this problem or can point to an alternate way of reading the text file in this context please let me know.
The reproducible example can be found below, make sure to set the build configuration to x64 and subsystem to Windows`
#include <Windows.h>
#include <iostream>
#include <fstream>
using namespace std;
struct TestStruct
{
int test;
}; TestStruct* testStruct;
bool Test(char* filename)
{
testStruct = new TestStruct[8];
if (!testStruct)
{
return false;
}
ifstream font;
font.open(filename);
if (font.fail())
{
return false;
}
char temp;
for ( int i = 0; i < 8; i++)
{
font.get(temp);
while (temp != ' ')
{
font.get(temp);
}
font.get(temp);
while (temp != ' ')
{
font.get(temp);
}
font >> testStruct[i].test;
}
font.close();
return true;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow)
{
AllocConsole();
freopen("CONOUT$", "w", stdout);
while ( Test((char*)"test.txt") )
{
}
return 0;
}
P.S I am failing to reproduce this issue while executing binary by itself from build folder however it still occurs while launching it with local windows debugger attached.
You have an infinite loop that calls the same function with the same parameter. This function does exactly the same procedure but has a side effect of creating an array of TestStruct.
When everything "goes ok", the function returns true, and is called again. One problem is that you never delete the array you are dynamically creating inside of the function. That means that sooner or later you would run out of memory, and this infinite loop would be broken. But it wouldn't be broken in the way you plan it: the operator new (the one that you are using) would throw an exception (std::bad_alloc) instead of returning nullptr.
That is what I see from your code. The compiler may see the same, and it can optimize the code in many strange ways to achieve the same behavior. Try to correct the issues that cause the UB and memory leaks, and maybe the compiler would change it's mind.
So as some pointed out, casting from one string type to another type, in this case from const char* to char* is causing undefined behavior, at first i fixed it in the reproducible example but couldn't fix it in my main project but after changing all string types to the ones that original function is taking it solved the problem, i.e:
D3DX11CreateShaderResourceViewFromFile is taking LPCTSTR and caused a crash while i was trying to cast LPCTSTR from WCHAR*

Capture the output of a QR scanner from a raspberry in C++

I have a QR scanner attached to my raspberry pi 3 B+. I need to capture the output of the scanner from C++ on raspbian. The scanner writes the read information to the window with the focus, like a keyboard input.
I have tried to capture the keyboard input with the ncurses library and other similar choices but sometimes I get an incomplete code, because I don't know when the read info ends, and I don't know when I have a complete input.
I tried something like this:
#include <stdio.h>
#include <iostream>
#include <ncurses.h>
#include <future>
using namespace std;
int main(int argc, char *argv[])
{
initscr();
cbreak();
noecho();
scrollok(stdscr, TRUE);
nodelay(stdscr, TRUE);
char buffer[1000];
while (true) {
getstr(buffer);
cout<< buffer << endl;
napms(500);
}
return 0;
}
The problem is that I may get the code in a fragmented way if it's too long or if the output slows down for whatever reason.

Detect invisible glyphs from character array in C++

I am working on a simple plugin for a game emulator in C++.
The purpose of the plugin is to detect if message posted by user contains more than 3 spaces or message with these strings: : or ;
The code looks like this so far:
#include "common/hercules.h"
#include "common/memmgr.h"
#include "common/mmo.h"
#include "common/socket.h"
#include "common/strlib.h"
#include "map/clif.h"
#include "map/pc.h"
#include "plugins/HPMHooking.h"
#include "common/HPMDataCheck.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
HPExport struct hplugin_info pinfo = {
"GM Impersonate", // Plugin name
SERVER_TYPE_MAP, // Which server types this plugin works with?
"1.0", // Plugin version
HPM_VERSION, // HPM Version (don't change, macro is automatically updated)
};
bool my_pc_process_chat_message(bool retVal___, struct map_session_data *sd, const char *message) {
if (retVal___ == true) {
if (stristr(message, " ")) {
clif->messagecolor_self(sd->fd, COLOR_RED, "Possible GM Impersonation Detected - you cannot use more than 3 spaces in chat.");
return false;
}
if (stristr(message, " : ") || stristr(message, " ; ")) {
clif->messagecolor_self(sd->fd, COLOR_RED, "Possible GM Impersonation Detected - you cannot use : or ; in chat.");
return false;
}
}
return true;
}
HPExport void plugin_init(void) {
addHookPost(pc, process_chat_message, my_pc_process_chat_message);
}
I am not a C++ programmer, I used a sample plugin to work this code out, which appears to be working miraculously. I am now trying to improve this little plugin to detect invisible glyphs, as if they were spaces, in the message char array.
How can I achieve this?
I found this post - https://stackoverflow.com/a/15813530/2332336 but this appears to be for string, not char array. Any ideas?
I'm not sure the solution you linked to is the same as "invisible/Non-ASCII" as it seems only to deal with stripping out 8-bit ASCII codes. However if you want to try the solution you linked to, there is no reason to let the difference in char arrays and strings stand in your way. The String class has a constructor that will work to convert for you.
string mystring(message);
Then you can feel free to use the "stripUnicode" method from the linked solution to see if that gets what you were expecting.

C++ Keylog Not Working Properly

I'm trying to make a simple keylogger in C++ (for learning only) and it's not quite working how I would like it to. My goal is to have it write to a txt. Here's the code I have so far:
#include <iostream>
#include <fstream>
#include <conio.h>
#define LOG(x) logger << x;
int main()
{
using std::ofstream;
using std::fstream;
ofstream logger("logger.txt", fstream::app);
char ascii;
bool typing;
for(;;)
{
if(_kbhit())
{
typing = true;
ascii = getch();
while(typing == true) //tried 'if', doesn't work
{
LOG(ascii);
std::cout << ascii << std::endl;
//typing = false;
//break
//tried using the above two and didn't work
}
}
else typing = false;
}
logger.close();
}
When I make while(typing == true) continuous, the key that is pressed continuously gets printed, but at least it actually gets saved to the txt. When I try to make the loop stop after one keyboard click, nothing gets saved to the txt.
So what am I doing wrong? Thanks for any help!
The variable typing is never set to false, so it stays true and your loop continues. The following code works:
#include <fstream>
#include <conio.h>
int main()
{
std::ofstream logger("logger.txt", std::fstream::app);
for(char ascii; ascii != 3;)
{
ascii = getche();
logger << ascii;
}
return 0;
}
getche() prints the character typed, and 3 is the ASCII code for Ctrl+C. This logs all characters, even non-printable ones.
A few comments on your code:
Don't use macros (#define) unless you are substituting a large amount of code and using it often, or plan on changing what something does.
You use loops and variables where you don't need to. getch and related functions wait for input.
logger.close() is automatically done when logger goes out of scope and is destructed.
return 0 should be at the end of main. It's not necessary, but it is used to return to the OS and return 0, although automatically put in, is important to have in for clarity.
I personally don't use using statements. Just write out the namespace, it helps avoid collisions. That's why it's in a namespace.

Can't launch website in C++

Trying to launch a website in Visual C++ 2010 Express, every researched solution has not worked. So I gather I need more in-depth assistance. Here's my code:
#include <stdafx.h>
#include <stdio.h>
#include <iostream>
#include <io.h>
#include <string>
#include <string.h>
#include <conio.h>
#include <windows.h>
using namespace System;
using namespace std;
int main()
{
char name[240];
cout<<"\nHello, Please enter your name: ";
cin.getline(name,240);
cout<<"\nHello "<<name <<", your ID has been confirmed!";
cout<<"\nContinuing to launch website 'xyz' now.";
// system("C:\\Program%Files\\Google\\Chrome\\Application\\chrome.exe");
ShellExecute, TEXT("open"), TEXT(c:\\program
files\\google\\chrome\\application\\chrome.exe), NULL, NULL, SW_SHOWNORMAL;
goto end;
end:
cout<<"\n\nProgram completed, Pess any key to exit. ";
_getch();
return 0;
}
Like this.
ShellExecute(NULL, TEXT("open"), TEXT("c:\\program files\\google\\chrome\\application\\chrome.exe"), NULL, NULL, SW_SHOWNORMAL);
You had missing parens, missing double quotes and quite possibly spurious characters between 'program' and 'files' in your path. You also had a missing parameter to ShellExecute.
Reading a book and learning a minimum of C++ syntax would be a good idea. These are very basic errors.
system("\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\" http://heise.de");
works for me.
See How to call an external program with parameters?