How to play sound when the program ends

How can I play sound when the program ends? I am using Windows 7.

Thank you :)
closed account (Dy7SLyTq)
#include <cstdlib>

atexit(YourFunction); //put this in the beginning of main()

void YourFunction()
{
//play sounds
}
I need it just to play a quick sound. I dont need it to play a sound file.
and when I am trying to do that
"PlaySound("C:\\SOUNDS\\BELLS.WAV", NULL, SND_ASYNC);" function I have an error : undefined reference to `PlaySoundA@12'
Last edited on
If you want a simple 'beep' sound try this:
cout << '\a';
closed account (Dy7SLyTq)
@op: did you #include <windows.h>?
DTSCode: Yes

Snaksa: It doesn't make any sound...
closed account (Dy7SLyTq)
new question... and maybe we should have asked this earlier... you are on a windows platform correct?
What do u mean by "windows platform"?
closed account (Dy7SLyTq)
the windows operating system
@ZivHirsch

Try this small program, to see how to get a wav file to play. This worked for me using C++ Visual Express 2012.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// PlaySound.cpp : main project file.

#include <iostream>
#include <windows.h>
#include <string>

#pragma comment(lib, "winmm.lib")

int main()
{
    std::wstring soundfile(L"C:\\Temp\\Flash Drive\\HelloCommander.wav");
// Change 'C:\\Temp\\Flash Drive\\HelloCommander.wav' above, to the name and location of your wav file
	PlaySound(soundfile.c_str(), NULL, SND_ASYNC);
	Sleep(2500);
	
	std::cout << "Press any key to close program.." << std::endl;
    getchar();
    return 0;
}
DTSCode: I am using Windows 7

whitenite1: I can't run it...
error: cannot convert 'const wchar_t*' to 'const CHAR*' for argument '1' to 'BOOL PlaySoundA(const CHAR*, HINSTANCE__*, DWORD)'

I am using Code::Blocks 10.5
Last edited on
closed account (Dy7SLyTq)
ah theres your error. you need to use a wchar_t instead of a char
DTSCode wrote:
ah theres your error. you need to use a wchar_t instead of a char
No soundfile needs to be a std::string and not a std::wstring.
closed account (Dy7SLyTq)
sorry i got the error reversed. my mistake.
A quick sound ? Since the thread is not yet closed if you ever happen to get here again. try

cout << "\7" << endl;
Topic archived. No new replies allowed.