Sign in to follow this  

c++ beeping driving me insane!!!

This topic is 3839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This program worked fine the other day, now it won't do anything. I'm working on a simple workout program, that will beep when exercises or rounds change (boxing). It is supposed to beep based on input, and worked fine but now it won't. Here's the code, please help, it deals with c++ and the cout << '\a' << flush sequence.
#include <fstream>
#include <iostream>
#include <windows.h>
#include <ctime>

using namespace std;

int main()
{
	double timer = 0;
	double lastTime = 0;

	double totalTime = 0;
    
	int numberOfRounds = 0;
	int exercises = 0;
	double* secondsPerRound = new double[100];


	cout << "Enter the number of EXERCISES PER ROUND : ";
	cin >> exercises;
	
	cout << endl;	

	cout << "Enter the number of rounds: ";
	cin >> numberOfRounds;

	cout << endl << "Exercises: " << exercises << endl << "Round: " << numberOfRounds;
	cout << endl;



		for (int i = 0; i < numberOfRounds; i++)
		{
			cout << "Round " << i + 1 << "  time (seconds): ";
			cin >> secondsPerRound[i];
		}

		timer = clock();
	
	
	for (int i = 0; i < numberOfRounds; i++)
	{		
		for (int j = 0; j < exercises; j++)
		{
			cout << "Waiting " << secondsPerRound[i] << " seconds: " << endl;
			cout << '\a' << flush;

			lastTime = timer;
			timer = clock();
			totalTime += secondsPerRound[i];

			while(((timer - lastTime) / 1000) <= secondsPerRound[i])
			{
				timer = clock();
			}	
		}
	}

	cout << '\a' << flush;
    
	double minutes = 0;
	double seconds = 0;

	cout << "You have been going for: " << totalTime << " seconds,  or " << (int)(totalTime) / 60 << " minutes and " << (int)(totalTime) % 60 << " seconds";
	cout << endl << "Press any key" << endl;
	
	char key;

	cin >> key;


	return 0;
}

Share this post


Link to post
Share on other sites
It's driving me crazy because I haven't changed any code and it worked yesterday but doesn't work today.

What's the deal?

Share this post


Link to post
Share on other sites
Are you running a Console App by any chance? If so, have you observed any different font today than from yesterday, or from your other console windows?

This happened to me when I accidently changed the font somehow, and it made beeps as soon as some character was printf'd. Never found a solution though.

Share this post


Link to post
Share on other sites
Yes, it's a console program. That's weired that it works on others computers. I don't know what to do.

No fonts are different. Is there any other way to make this thing beep? Using windows API seems disgusting... do I dare try it? And can I have console input like this program?

Share this post


Link to post
Share on other sites
Try Beep():

#include <windows.h>

...
Beep (2, 2);

The first parameter is the frequency, the second is duration
of the beep.

You dont need to change anything (cout/cin will still work).

Share this post


Link to post
Share on other sites
Beep() doesn't work either. This blows. Maybe I should try to update my sound drivers on my computer?

Share this post


Link to post
Share on other sites

Beep (750, 300);

This works for me.

If that doesnt work, try:

MessageBeep (-1);

Share this post


Link to post
Share on other sites
Beep(750, 300) worked!!!

I love you. Thank god... I'm ready to blow up the house.

Awesome, you kick so much ass :)

Share this post


Link to post
Share on other sites
Ah!!!!

Ok. Beep worked for a while, and now it's not. If I enter 2 for both exercises and rounds (should beep 5 times), it will only beep twice.

Share this post


Link to post
Share on other sites
Have you played with the duration of Beep()?

Odds are that your code is going faster then the beeps.
Try a smaller duration, and try it.

Share this post


Link to post
Share on other sites
Yes, I've tried shorter durations. The same thing happened:

Enter the number of exercises: 2
Enter the number of rounds: 2

It only beeps twice, meaning it only beeps through the first iteration (first round). What the hell?

It just worked a few minutes ago, and now it's not. I really don't understand this.

Share this post


Link to post
Share on other sites
The only thing wrong with your code is a memory leak (secondsPerRound
is never deallocated); but that shouldnt effect this.

If I find another solution I'll post it. I tested your code about
6 times, and it worked each time...

Share this post


Link to post
Share on other sites
I found the problem. I'm on my laptop. When my laptop is plugged in, it works fine. When it's unplugged it doesn't work.

Talk about bullshit.

Anyway, thanks guys.

-Yomama

Share this post


Link to post
Share on other sites
Quote:
Original post by heyman
I found the problem. I'm on my laptop. When my laptop is plugged in, it works fine. When it's unplugged it doesn't work.

Talk about bullshit.

Anyway, thanks guys.

-Yomama


That will almost certainly be your laptops power (battery) optimisation settings. Check that you're permitting your soundcard to work when you're on battery power.

Share this post


Link to post
Share on other sites
Sign in to follow this