0

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;
}
3
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by picogenkaku
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.

Edited by Ancient Dragon: n/a

0

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

Edited by subith86: n/a

0

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
}

Edited by Ancient Dragon: n/a

1

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

0

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.

0

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. :)

0

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 ;)

Edited by picogenkaku: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.