4 Replies - 2852 Views - Last Post: 21 November 2009 - 08:15 PM Rate Topic: -----

#1 Vixthefox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-October 08

how can I stop a loop? (stopwatch program)

Posted 21 November 2009 - 02:17 PM

I'm working on a timer program that uses for loops. Although it might not be a right way to do it, but how can I make a for loop stop by pressing a key, as in, starting a for cycle by pressing the space bar and stopping it the same way?

so far I have this. how do I make it stop by pressing the space bar?

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

using namespace std;

int main()
{
	int ms,s,m,st;
	system("pause");
	for(m=0;m<=59;m++)
	{
	for(st=0;st<=5;st++)
	{
		 for(s=0;s<=9;s++)
		 {
						  for(ms=0;ms<=9;ms++)
						  {
						  Sleep(50);
						  system("cls");
						  cout<<"\n\n\n\n\t\t\t\t"<<m<<":"<<st<<s<<":"<<ms<<endl;
													 }
													 }
													 }
													 }
return 0;
}



EDIT: I just had an idea, should I use a logic operator and a function to stop it?, but how would I do that?

This post has been edited by Vixthefox: 21 November 2009 - 03:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: how can I stop a loop? (stopwatch program)

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 605
  • View blog
  • Posts: 1,868
  • Joined: 02-August 09

Re: how can I stop a loop? (stopwatch program)

Posted 21 November 2009 - 04:09 PM

Look up kbhit(), that funtion will tell you when a key has been pressed.
Was This Post Helpful? 1
  • +
  • -

#3 Vixthefox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-October 08

Re: how can I stop a loop? (stopwatch program)

Posted 21 November 2009 - 05:02 PM

View PostAphex19, on 21 Nov, 2009 - 03:09 PM, said:

Look up kbhit(), that funtion will tell you when a key has been pressed.


I just tried it, it works, any ways I can get it to capture the time and display it?
Was This Post Helpful? 0
  • +
  • -

#4 MacClassicNerd  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 27-July 09

Re: how can I stop a loop? (stopwatch program)

Posted 21 November 2009 - 05:51 PM

View PostVixthefox, on 21 Nov, 2009 - 04:02 PM, said:

I just tried it, it works, any ways I can get it to capture the time and display it?

Just store the time into a variable and then output it.

This post has been edited by MacClassicNerd: 21 November 2009 - 05:56 PM

Was This Post Helpful? 0
  • +
  • -

#5 Vixthefox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-October 08

Re: how can I stop a loop? (stopwatch program)

Posted 21 November 2009 - 08:15 PM

I got it all to work now, thanks guys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1