3 Replies - 2117 Views - Last Post: 29 September 2007 - 01:44 PM Rate Topic: -----

#1 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

c++ sound files in game

Posted 29 September 2007 - 09:58 AM

Just a quick question: Is there a way in include sound clips in your c++ program. For example, if I have a game, such as:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <string>
using namespace std;


int main()
{
	srand ( static_cast<unsigned int> ( time ( 0 ) ) );

  int randNum = rand() % 100 + 1;
  int num = 0;
  int GuessLow=0;	
  int GuessHigh = 0;
  int guess=0;
  string uname;
  
  cout<<"Please enter your name: ";
  getline ( cin, uname );
  cout<< uname <<'\n';
  cout<<"Welcome to the Random Number Guessing Game!!\n";

  
	{
		do
		{
			// Loops until user finds the number
			
			cout <<uname<<" "<<"please enter a number between (#1 - 100): ";
			cin >> num; // User's input of guessed number
			guess++;
						
			if (num < randNum) // If num is < than the random number, inform player to try again.

			   cout <<uname<<" "<<"I'm sorry-your number was too low-please try again!";GuessLow++;
			   
			 else
			
			if (num > randNum) // If num is > than the random number, inform player to try again.
			   cout<<uname<<" "<<"I'm sorry-your number was too high-please try again!";GuessHigh++;
			
				
			
			else
			
			break;
			
		} while (num != randNum);
		
		
		
	
	
		cout<<uname<<endl;
		cout<<" "<< "Excellent Job!! You guessed correctly!!"<< endl;//Informs if the player is successfull
		cout << "Guessed too high: " << GuessHigh;//Display the user's score at the end of the program
		cout << "Guessed too low: " << GuessLow;//Display the user's score at the end of the program
		cout<<  "Total number of guesses: "<<guess;//Display the # of guesses by user
		} 


	return 0;
}


The above is random number guessing game. Can sounds be included if I want the computer to make sound everytime a user enters a number that is correct? Thx much..

Is This A Good Question/Topic? 0
  • +

Replies To: c++ sound files in game

#2 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: c++ sound files in game

Posted 29 September 2007 - 10:17 AM

I don't think C/C++ support sound normally, but using another library you should be able to get them to. I found this sample code for playing a .wav file:
// play a wave file sound using the winmm library
// For DevCpp add the library libwinmm.a to linker via 
// Project/Project Options/Parameters/Add Library
// remember this is a console program!

#include <iostream>
#include <windows.h> // for PlaySound()

#define SND_FILENAME 0x20000
#define SND_LOOP 8
#define SND_ASYNC 1

using namespace std;

int main()
{
// play sound as loop (remove SND_LOOP for one time play)
// file boing.wav has be in the working directory
PlaySound("boing.wav",NULL,SND_FILENAME|SND_LOOP|SND_ASYNC); 

cin.get(); // wait
return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

Re: c++ sound files in game

Posted 29 September 2007 - 10:27 AM

Thx for the input...so i guess it may be able to support only WAV files...pretty much i was thinking of making it play sounds that the computer already had on...similar to when you log on, log off...ect..
Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: c++ sound files in game

Posted 29 September 2007 - 01:44 PM

what do you think those sounds are stored as? search in c:\windows or c:\windows\media for *.wav ;)

-jjh
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1