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 you had.
//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;

    }

Recommended Answers

All 22 Replies

#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 :!:

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.

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

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.

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.

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.

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

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

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 .

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.

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

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

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 <signal.h>
//....etc...
extern "C"
{
void catch_signal(int sig_num);
}

//...etc.

Signals and signal handlers. Thanks, folks.

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.

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

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.

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 :!:

There is nothing to important going on in his code. There is no array being implimented and is not asking for imput. All he is trying to do is cout hello using a loop.. big whoop.
I was giving him the simple solution.

I was giving him the simple solution.

The reason dave was concerned is that you said it would print 9 times when it actually prints 10 times... (0-9 is 10).

The reason for JoBe's comment was that the code you provided is not what the original poster asked for - which is code that does an infinite loop until the user of the program explicitly cancels/interrupts it.

Ok... as long as we got that out of the way.
I didn't test the code... my bad...
I didn't read all the posts carefully... my bad again...
Thanks for clearing everything up...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.