Short little sound files of chicken, horses, dogs, cars, trains and other annoying things is the realm of the wave file (.wav). These are easy to play and guaranteed to confuse grandpapa. Again, we are using Windows' sound workhorse winmm.lib file. Ze function this time is PlaySound(). Here is ze C++ console code.

// play a sound with PlaySound() from the winmm.lib
// For DevCpp add the library libwinmm.a to the linker via 
// Project/Project Options/Parameters/Add Library
// a Dev-C++ tested console application by  vegaseat  19dec2004

#include <cstdio>      // getchar()
#include <windows.h>   // PlaySound()

#define SND_FILENAME 0x20000  // from mmsystem.h
#define SND_LOOP  8
#define SND_ASYNC 1
#define SND_ALIAS 0x10000

using namespace std;

int main()
  // pick a wave file supplied by Windows XP or one of your own ...
  char soundfile[] = "C:/Windows/Media/chimes.wav";
  // play sound as loop, warning - can be vexatious!!!
  // without SND_LOOP it just plays it once
  // play the system exit sound if set
  getchar();  // wait
  return 0;
13 Years
Discussion Span
Last Post by danibootstrap

Replaced iostream header and cin.get() wait with cstdio header and getchar() wait to bring the exe file down in size from 415k to 15k.


If you wanted to use the same code to play multiple wav files how would you go about it. The code I used is.
int main()

string soundstring = "Sound/.wav";
string scenecode = "0000";//Put 4 digit wav name here
soundstring.insert(6, scenecode);
char soundfile[] = soundstring; PlaySound(soundfile,NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);
return 0;

I would like to only need to change the 4 digit code to play a different wav file.

The error I'm getting is:
char soundfile[] = soundstring;

initializer fails to determine size of "soundfile"


Hey vegaseat,I was able to play the sound file (.wav) when i created a project.But now i require that i play the file just as a normal .cpp file (by not creating a project)How shall i do it?And siince i am not creting a project how shall i add the libwinmm.a to the file.Please tell me.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.