hi...
let's say i have an infinty loop:
how to break of the loop externally?Code:int main() { while(true) { //... blah blah } }
using interrupt?
thanks
hi...
let's say i have an infinty loop:
how to break of the loop externally?Code:int main() { while(true) { //... blah blah } }
using interrupt?
thanks
Have a variable, like quit, which is usually zero. When you want to quit, set it to one.
Or else you can just check the return values of your functions.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
If something inside your loop blocks, you will need to use signals.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
Code:void t1() { while(true) { //point b } } int main() { bool bk = false; char c; while(!bk) { //point a scanf("%c", &c) if(c == 'y' || c == 'Y') bk = true; t1(); } }
based on the code basically when it come to point b... it will be forever stuck there...... now my question i s how to break it.. if point b is not implemented any signal handler or etc etc....?
Thanks
You must have a break in the point b loop or it will run forever, it is that simple. How to break it is entirely up to you.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
You know what mitakeet means, right?
Code:while(1) { //... c = getinput(); if(c == 'x') break; //... }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
ya i know....... but how do we break something like this:Originally Posted by dwks
that's the problem......Code:while(1) { //... //... }
Why not post some real code?
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
I may be reading this wrong, but is this what you are asking: "Let's say we have the following code. How do I stop it; how do I break out of running the program?"Originally Posted by draggyHere in Windows I pressed Ctrl+C. Ctrl+Break works as well.Code:#include <stdio.h> #include <string.h> int main(void) { for ( ;; ) { char line[80]; fputs("enter some text: ", stdout); fflush(stdout); if ( fgets(line, sizeof line, stdin) ) { char *newline = strchr(line, '\n'); if ( newline ) { *newline = '\0'; } printf("line = \"%s\"\n", line); } } return 0; } /* my output enter some text: hello world line = "hello world" enter some text: okay, now what? line = "okay, now what?" enter some text: hey -- how do I stop this thing? line = "hey -- how do I stop this thing?" enter some text: ^C */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
sorry for the late reply.... a bit busy over here..
ok ...
example:
ok above is a infinity loop, which can't be terminate unless using Crtl + c....Code:int main() { while(true) { //point b //without break or loop terminator } }
As we can see most OS have the ability to manage and terminate any task by send a message or command or etc etc.....
My question is how to terminate the infinty loop, where we at outside the boundry of that infinty loop or program......
thanks
How about:Now you just have 'terminate' modified by something else, and you're all set. Otherwise:Code:volatile int terminate = 0; while( !terminate ) { ... }
This doesn't make any sense. If you're "outside the boundry of that ... loop", then how again do you need to terminate it? If you're not within the bounds of said loop, there's nothing to terminate. Your entire program doesn't run every single instruction all at the same time. It happens sequentially.My question is how to terminate the infinty loop, where we at outside the boundry of that infinty loop or program......
Quzah.
Hope is the first step on the road to disappointment.
Perhaps exit()?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Optionally you could use a thread?
e.g. The infinity loop is in the thread:
Main wants to stop the loop, so it kills the thread.Code:while(1) { // stuff }