954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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

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
# include

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.

dj_money
Newbie Poster
5 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 
#include <iostream>

using namespace std;

int main()
{
	int stop = 0;

	while (true)
	{
		stop++;

		cout<<"Hello!\n";

		if(stop == 5) break;
	}

	return 0;
}


OR

#include <iostream>

using namespace std;

int main()
{
	int i = 0;

	for(;;)
	{
		i++;
		cout<<"Hello!\n";

		if (i == 5) break;
	}

	return 0;
}


Hope this helps :D

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

[ code ]code in here [ /code ] but without the space between the square brackets :!:

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 

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.

dj_money
Newbie Poster
5 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

Hmm, well, the only way I can think of is this way:

#include <iostream>

using namespace std;

int main()
{
	char ch = 'j';

	for (;;)
	{
		cout<<"Hello!\n";

		cin>> ch;

		if(ch == 'Q') break;
	}

	return 0;
}
JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 

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.

dj_money
Newbie Poster
5 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 
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.

dj_money
Newbie Poster
5 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

Guys,
There's a much better way to do this.

#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...

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

Where in your code do you specify that the signal thingy is looking for Ctrl+C?

cscgal
The Queen of DaniWeb
Administrator
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 
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 .

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
Where in your code do you specify that the signal thingy is looking for Ctrl+C?



SIGINT is the code for Ctrl-C. There are other codes for Ctrl-Z, etc.

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 
You shouldn't be putting standard output stuff in a signal handler. Search also for sig_atomic_t .



(I was just giving him an example so that I could show that in my output it actually worked.)

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

Something like this makes a better example (for future reference).

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Unfortunately, over my head, though I trust your suggestion.

Also, for what it's worth, to avoid a compiler warning, I should have done this:

#include
//....etc...
extern "C"
{
void catch_signal(int sig_num);
}

//...etc.

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

Signals and signal handlers. Thanks, folks.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Hey Guys and Girls, Thanks A Bunch. Especially you WinBatch, Thanks a lot for the advice. I guess it could be done after all with the signal.h header. I like your codes, but the only thing that I didn't like about it was when CTRL-C was keyed, the cout"HEY! somebody cancelled me... " popped up too fast and disappeared, actually the whole program just auto-exited. But I still like the codes. Extremely Good impression WinBatch. Thanks again.

dj_money
Newbie Poster
5 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 
the only thing that I didn't like about it was when CTRL-C was keyed, the cout"HEY! somebody cancelled me... " popped up too fast and disappeared, actually the whole program just auto-exited.

FAQ > How do I... (Level 1) > Stop my Windows Console from disappearing everytime I run my program?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Try this:

int main (void){

int test = 0;
while (test < 10){

cout<<"Hello \n";
test++;

}//end of while

return 0;
}

This code will only do 9 times.
If you want to do 10 just replace while (test < 10)
with while (test <= 10).

brian63304
Newbie Poster
11 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

Try this:

int main (void){

int test = 0; while (test < 10){

cout<<"Hello \n"; test++;

}//end of while

return 0; }

This code will only do 9 times. If you want to do 10 just replace while (test < 10) with while (test <= 10).

You obviously don't test your code before you post, and are likely in for a world of hurt when you begin attempting to index into arrays.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
You obviously don't test your code before you post, ...

and doesn't read the whole thread, otherwise, he would have seen that this solution was allready given and wasn't the solution the thread starter was looking for :!:

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You