Random list sound

1
  • Sale I can not convert string to 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;
    }
    
  • asked by Illud Harpyja 30.12.2016 в 20:00
    source

    1 answer

    1

    The function PlaySound receives as a first parameter a LPCTSTR

    BOOL PlaySound(
       LPCTSTR pszSound,
       HMODULE hmod,
       DWORD   fdwSound
    );
    

    When defining your array as string , you must use the function c_str() to give the function PlaySound a pointer to char .

    I give you an example of what you should do.

    #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;
    }
    

    In your code, it would be enough to do:

    PlaySound(ra[random].c_str(),NULL,SND_FILENAME|SND_ASYNC);
    
        
    answered by 05.01.2017 в 12:38