Im trying to add a code so that when itdoes somthing it will return back to the start of int main. Im not sure though what the code is.
Im trying to add a code so that when itdoes somthing it will return back to the start of int main. Im not sure though what the code is.
Code:int main () { while (1) { bool doesSomething = 0; //code here if (doesSomething == 1) { continue; } //code here } }
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
ya i knew someone was going to put that code. after you write alot down in the source code its hard to make soething with while. Like I need it so that Part of the code at the buttom is goinng to redirect you back to the int main. While wont work because the code connects to other stuff.Ill try it but i doubt it. Like what i mean is. If x dosent equal to y then return to int main. I cant use While because it uses other parts of code.Originally Posted by Dae
I see what you mean, so 1) the variable being checked would have to be global, and 2) you'd need the check for that relationship being constant throughout the program. Well.. I know in win32 you can have a while statement that constantly checks for messages, and if one shows up no matter where you are in your program it will process, but I'm not sure how to get that effect in normal c++. I assume you could have that effect in OpenGL, as it uses a library for the OS (like win32). Hmm.. wonder if there is a way to do it in normal c++, or the reason you can do it in win32 is because you're taking advantage of the OS.
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
ill see what i can do.
You could use execptions. Something like this:
KurtCode:#include <iostream> int something_wrong = 0; void do_it() { something_wrong++; if ( something_wrong < 10 ) throw( 1); } int main() { std::cout << "starting program." << std::endl; start: try { do_it(); } catch (int i) { std::cout << "starting again " << something_wrong << std::endl; goto start; } }
I decided to swtich the code. Around so that i can use a while statment. It might take me a couple of hours though
Why would it take that long? put the while part at the beginning of code, and } at the end. Anyway it doesnt do exactly what you wanted remember.Originally Posted by Deltahawk12
Exceptions would give you more detailed ability (like having many while statements) to return to the beginning of main 'if something is equal to something2', but still not a constant check, and it costs you having to put all the catch and try's in your code. .
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
Dae, I don't know what you mean by constant check, but throwing an exception will unwind the stack until the exception is caught or, if it isn't, the program is terminated.
Deltahawk12, your decision to rework the code to be more compatible with normal control constructs (such as loops) is a wise one. When exceptions are needed report non-exceptional conditions and goto statements are needed*, it often means that you have a flawed design.
Often, rewriting/redesigning code you have written is a good exercise, and despite the annoyance, it is a good way to learn.
*There are cases where they are necessary and/or good to use. These cases are fairly rare.
Cheers
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
Maybe I'm reading what he's asking for wrong, but it looks like all he's asking for is something like if(something) goto start;
So you would haveOf course, everyone hates goto, so try to avoid it.Code:int main() { start: //Do all your code here if(something) goto start; return 0;
Last edited by Dweia; 09-10-2005 at 05:44 PM.
I mean like is it possible to have something checking if the variable1 == variable2 at all times, and not just when the statement ends?Originally Posted by Zach L.
The while statement will continue processing till the end of the statement before checking if variable2 != variable1, and therefor ends up printing "test2" as well. The same thing goes for the exceptions, if you included a large amount of code in the exceptions then the exception wont occur until the end of the statement, and therefor almost quite useless (although it does provide means to use the exceptions in other functions than main, but then again you could just use a while statement in functions other than main, and so it would be the same effect). Of course I dont know much about exceptions but testing the posted one above thats what resulted.Code:int variable1; int variable2; int main { while (variable2 != variable1) { cout << "test1"; variable2 = variable1; cout << "test2"; } //finally finds out variable2 is == variable1 }
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
You can achieve the "continual testing" by means of encapsulation. If you have a class (perhaps only a wrapper class) with an overloaded operator=, for instance, then you can 'continually check'. I don't know much about Windows' messaging system, but I cannot think of anyway to do it without multi-threading (and then, it is only 'simulated' parallelism unless you actually have multiple processors). This requires a lot more design, and a lot more programming, though (and there is no standard way of doing it).
Cheers
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.