How do you Terminate an Infinite Loop? Impossible!!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2005
Posts: 5
Reputation: dj_money is an unknown quantity at this point 
Solved Threads: 0
dj_money dj_money is offline Offline
Newbie Poster

How do you Terminate an Infinite Loop? Impossible!!!

 
0
  #1
Nov 11th, 2005
Help me here guys! I try building an infinite loop but don't know how to terminate it.

//Description:
//This program asks the user to terminate an infinite loop
//with a sentinel value of any kind.

//This is a regular infinite loop.
//This would just run infinitely with no end.
//I've tried many ways to terminate it but I can
//never get pass the errors.
//I've used the break statement and still give me errors.
//Help me. I think it's impossible to break an infinite loop.


# include <iostream>

using namespace std;

int main (void){

int test = 0;
while (test < 10)
cout<<"Hello \n";

return 0;
}




// Here is the one that I've tried and it works.
//It will terminate the loop but it's NOT an Auto Infinite Loop like the one above.
//Description:
//This program asks the user to terminate an infinite loop
//with a sentinel value of any kind.




# include <iostream>
# include <cmath>

using namespace std;

int main(void){

int test = 0;
char stop;


while (test++ < 10){
cout<<"Hello \n";
cin>>stop;
if (stop == 'q') //Pressing (sentinel value) q on the keyword will terminate the program.
break;
}
cout<<"Program has been terminated! \n";

return 0;

}

//help guys, i think it's impossible to end an infinite loop
//that keeps running automatically until someone press
//a sentinel value to terminate it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: How do you Terminate an Infinite Loop? Impossible!!!

 
0
  #2
Nov 11th, 2005
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int stop = 0;
  8.  
  9. while (true)
  10. {
  11. stop++;
  12.  
  13. cout<<"Hello!\n";
  14.  
  15. if(stop == 5) break;
  16. }
  17.  
  18. return 0;
  19. }

OR

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int i = 0;
  8.  
  9. for(;;)
  10. {
  11. i++;
  12. cout<<"Hello!\n";
  13.  
  14. if (i == 5) break;
  15. }
  16.  
  17. return 0;
  18. }

Hope this helps

One other thing, when posting code use code tags like this:

[ code ]code in here [ /code ] but without the space between the square brackets :!:
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 5
Reputation: dj_money is an unknown quantity at this point 
Solved Threads: 0
dj_money dj_money is offline Offline
Newbie Poster

Re: How do you Terminate an Infinite Loop? Impossible!!!

 
0
  #3
Nov 11th, 2005
Thanks for the advice. How you got your program running was for an infinite loop to run and end automatically by itself. Well, what I meant that was hard to do was "How to manually end an auto-infinite loop." I think it's just impossible. But i may be proven wrong.
//help guys, i think it's impossible to end an infinite loop
//that keeps running automatically until someone press
//a sentinel value to terminate it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: How do you Terminate an Infinite Loop? Impossible!!!

 
0
  #4
Nov 11th, 2005
Hmm, well, the only way I can think of is this way:

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. char ch = 'j';
  8.  
  9. for (;;)
  10. {
  11. cout<<"Hello!\n";
  12.  
  13. cin>> ch;
  14.  
  15. if(ch == 'Q') break;
  16. }
  17.  
  18. return 0;
  19. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 5
Reputation: dj_money is an unknown quantity at this point 
Solved Threads: 0
dj_money dj_money is offline Offline
Newbie Poster

Re: How do you Terminate an Infinite Loop? Impossible!!!

 
0
  #5
Nov 11th, 2005
The way you have yours is the same like my second one.
Your code is about pressing a key from the keyboard to get "Hello" to continue infinitely until we key in the sentinnel value to interrupt it.
Well, what's hard to do was to not press anything from the key and just let cout<< "Hello" To Loop infinitely by itself UNTIL a sentinnel value is given from the keyboard and that will be the only time to press a key.

An Example of the Program Output:

Hello
Hello
Hello
Hello
Hello
"etc..." it would say Hello infinitely without pressing any key.
until a sentinel value or sentinel key is punched from the keyboard to Interrupt the
infinite looping program.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,738
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 281
Lerner Lerner is offline Offline
Posting Virtuoso

Re: How do you Terminate an Infinite Loop? Impossible!!!

 
0
  #6
Nov 11th, 2005
Using cin >> will pause the program until input occurs. My suggestion would be to consider using kbhit() from the non-standard conio.h file (not all compilers have this file). Sprinkle calls to kbhit() before or after any critical sections and if you want to quit only if a keystroke was made and it was a specific, then make the conditional compound. To my knowledge, which is far from all encompassing, there is no standard way to do this, though. I'll be happy to hear otherwise.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 5
Reputation: dj_money is an unknown quantity at this point 
Solved Threads: 0
dj_money dj_money is offline Offline
Newbie Poster

Re: How do you Terminate an Infinite Loop? Impossible!!!

 
0
  #7
Nov 11th, 2005
Originally Posted by Lerner
Using cin >> will pause the program until input occurs. My suggestion would be to consider using kbhit() from the non-standard conio.h file (not all compilers have this file). Sprinkle calls to kbhit() before or after any critical sections and if you want to quit only if a keystroke was made and it was a specific, then make the conditional compound. To my knowledge, which is far from all encompassing, there is no standard way to do this, though. I'll be happy to hear otherwise.
Thanks Lerner,
I've figured there would be no standard way to do this. Thanks for the advice. I'll try conio.h but I doubt that I would have it in my compiler. I'll try looking.

Thanks again guys.
If there are still other solutions. I'm glad to hear from it. would be very helpful.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: How do you Terminate an Infinite Loop? Impossible!!!

 
0
  #8
Nov 11th, 2005
Guys,
There's a much better way to do this.

  1. #include <signal.h>
  2. #include <iostream>
  3. using namespace std;
  4. void catch_signal(int sig_num);
  5. void catch_signal(int sig_num)
  6. {
  7. cout<<"HEY! somebody cancelled me..."<<endl;
  8. exit( 1 );
  9. }
  10. int main( int argc, char * argv[] )
  11. {
  12. signal(SIGINT, catch_signal);
  13. while ( true )
  14. {
  15. cout<<"BLAH"<<endl;
  16. }
  17. return 0;
  18. }

BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
BLAH
^CBLAH
BLAH
HEY! somebody cancelled me...
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,053
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 131
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: How do you Terminate an Infinite Loop? Impossible!!!

 
0
  #9
Nov 11th, 2005
Where in your code do you specify that the signal thingy is looking for Ctrl+C?
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,433
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 249
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: How do you Terminate an Infinite Loop? Impossible!!!

 
0
  #10
Nov 11th, 2005
Originally Posted by winbatch
void catch_signal(int sig_num)
{
	cout<<"HEY! somebody cancelled me..."<<endl;
	exit( 1 );
}
You shouldn't be putting standard output stuff in a signal handler. Search also for sig_atomic_t.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC