Good day to everyone. I have this trouble of stopping the timer at a specific time, say I have to stop the timer at 1:30. When I try to run it, it doesn't stop. Please help. Here's my C++ code.

#include <iostream>
#include <time.h>
using namespace std;

void wait ( int seconds )
{
	clock_t endwait;
	endwait = clock () + seconds * CLOCKS_PER_SEC ;
	while (clock() < endwait) {}
}

int main ()
{
	int counter = 0;
	int seconds = 0;
	int minutes = 0;

	for (seconds = 0; seconds < 90; seconds++)
	{
			if(seconds == 90)
			{
				cout<<"Time's up!";
			}
			else
			{
				if(seconds >= 60)
				{
					minutes++;
					seconds = 00;
					cout<< minutes << ":0" << seconds << "\r";
					wait(1);
				}
				else if(seconds < 10)
				{
					cout<< minutes << ":0" << seconds << "\r";
					wait(1);
				}
				else
				{
					cout<< minutes << ":" << seconds << "\r";
					wait(1);
				}
			}
	}

	return 0;
}

The timer is set to run for about 90 secndxs. Why do you think it should run until 1:30 ?

>>if(seconds == 90)

Impossible. The loop will stop as soon as seconds == 90, so the statement above will never get executed.

at line 29 you are setting seconds back to 0. So it's an infinite loop.
Use a different variable in for loop condition.

at line 29 you are setting seconds back to 0. So it's an infinite loop.
Use a different variable in for loop condition.

Oh yes, I hadn't noticed that.

The program can be greatly simplified

in main()
{
   wait(90); // wait 1 1/2 minutes
}

The program can be greatly simplified

But the reason why he did this way is to output the count down. In fact, count up. Starts from 0:00 to 1:30

at line 29 you are setting seconds back to 0. So it's an infinite loop.
Use a different variable in for loop condition.

Hmm... I'll try to modify the code as what you have suggested.

But the reason why he did this way is to output the count down. In fact, count up. Starts from 0:00 to 1:30

Agree. That's what I need. A counter that counts based on user input. But before I create the user input, I should first solve the problem without the input. :)

Thank you guys for helping me. I realized that a while statement could have solved the problem. Here's my solution to the problem. :) I've tried using a time of 90 and 45 seconds .:)

#include <iostream>
#include <time.h>
using namespace std;

void wait ( int seconds )
{
	clock_t endwait;
	endwait = clock () + seconds * CLOCKS_PER_SEC ;
	while (clock() < endwait) {}
}

int main ()
{
	int counter = 0;
	int seconds = 0;
	int minutes = 0;

	while(counter != 45)
	{
		seconds++;
		
		if(seconds >= 60)
		{
			minutes++;
			seconds = 00;
			cout<< minutes << ":0" << seconds << "\r";
			wait(1);
			counter++;
		}
		else if(seconds < 10)
		{
			cout<< minutes << ":0" << seconds << "\r";
			wait(1);
			counter++;
		}
		else
		{
			cout<< minutes << ":" << seconds << "\r";
			wait(1);
			counter++;
		}
	}

	cout<<"\n\nTime's up!";
	return 0;
}

Thank you everyone ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.