| | |
How do you Terminate an Infinite Loop? Impossible!!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2005
Posts: 5
Reputation:
Solved Threads: 0
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.
//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.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int stop = 0; while (true) { stop++; cout<<"Hello!\n"; if(stop == 5) break; } return 0; }
OR
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int i = 0; for(;;) { i++; cout<<"Hello!\n"; if (i == 5) break; } return 0; }
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 :!:
•
•
Join Date: Nov 2005
Posts: 5
Reputation:
Solved Threads: 0
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.
//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.
Hmm, well, the only way I can think of is this way:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { char ch = 'j'; for (;;) { cout<<"Hello!\n"; cin>> ch; if(ch == 'Q') break; } return 0; }
•
•
Join Date: Nov 2005
Posts: 5
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jul 2005
Posts: 1,738
Reputation:
Solved Threads: 281
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.
•
•
Join Date: Nov 2005
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
Guys,
There's a much better way to do this.
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...
There's a much better way to do this.
C++ Syntax (Toggle Plain Text)
#include <signal.h> #include <iostream> using namespace std; void catch_signal(int sig_num); void catch_signal(int sig_num) { cout<<"HEY! somebody cancelled me..."<<endl; exit( 1 ); } int main( int argc, char * argv[] ) { signal(SIGINT, catch_signal); while ( true ) { cout<<"BLAH"<<endl; } return 0; }
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...
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
•
•
•
•
Originally Posted by winbatch
void catch_signal(int sig_num) { cout<<"HEY! somebody cancelled me..."<<endl; exit( 1 ); }
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
![]() |
Other Threads in the C++ Forum
- Previous Thread: C++ Build Errors
- Next Thread: save an image into a file using c++
| Thread Tools | Search this Thread |
Tag cloud for C++
api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference rpg simple string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






