Sale no puedo convertir string en const char
#include <iostream> #include <cstdlib> #include <ctime> #include <string> #include <windows.h> #include "MMSystem.h" using namespace std; int main(int argc, char** argv) { srand(time(0)); string ra[] = {"demon_0.wav","demon_1.wav","jocker.wav"}; for (int i = 0; i < 1; i++){ int random = rand() % 3; //cout << ra[random] << endl; PlaySound(TEXT(ra[random]),NULL,SND_FILENAME|SND_ASYNC); system("pause"); } return 0; }
1 respuesta
La función PlaySound
recibe como primer parámetro un LPCTSTR
BOOL PlaySound(
LPCTSTR pszSound,
HMODULE hmod,
DWORD fdwSound
);
Al definir tu array como string
, debes utilizar la función c_str()
para entregar a la función PlaySound
un puntero a char
.
Te pongo un ejemplo de lo que tendrías que hacer.
#include <iostream>
using namespace std;
void f( const char *wav )
{
printf( "%s", wav );
}
int main()
{
string ra[] = {"demon_0.wav","demon_1.wav","jocker.wav"};
f( ra[1].c_str() );
return 0;
}
En tu código, bastaría con hacer:
PlaySound(ra[random].c_str(),NULL,SND_FILENAME|SND_ASYNC);
playsound
se le ingresa unchar*
pero si es así, en el primer parámetro quita la funciónTEXT();
y deja el parámetro comoPlaySound(ra[random][0],NULL,SND_FILENAME|SND_ASYNC);
saludos..