• How to end the program in a function.
    15 replies, posted
How do I end a program in another function besides int main? like for example [code] #include <iostream> using namespace std; void myfunc() { } int main(){ int a = 2; if(a == 2); myfunc(); cout << "Words"; cin.get(); return 0; } [/code] How would I make it so instead of printing "words" on the screen after the function ends, I just end the program in the myfunc?
[cpp] #include <cstdlib> void myfunc() { exit(0); } [/cpp] or alternatively: [cpp] void myfunc() { int lol = 1 / 0; } [/cpp]
It worked :] Thanks man.
[QUOTE=turb_;22868366] or alternatively: [cpp] void myfunc() { int lol = 1 / 0; } [/cpp][/QUOTE] Actually, that doesn't necessarily even throw a hardware exception. I recall someone saying that on some PowerPC processors it actually just sets the value to 0 and continues happily.
[QUOTE=esalaka;22870703]Actually, that doesn't necessarily even throw a hardware exception. I recall someone saying that on some PowerPC processors it actually just sets the value to 0 and continues happily.[/QUOTE] :byodood:
Throw the exception yourself :3
[QUOTE=turb_;22870748]:byodood:[/QUOTE] Exactly
[QUOTE=esalaka;22870703]Actually, that doesn't necessarily even throw a hardware exception. I recall someone saying that on some PowerPC processors it actually just sets the value to 0 and continues happily.[/QUOTE] I know from first hand experience that the NDS' ARM processor(s) do that :P
Because the method is "void" you don't need to return anything :) , Might help you...
[QUOTE=esalaka;22870703]Actually, that doesn't necessarily even throw a hardware exception. I recall someone saying that on some PowerPC processors it actually just sets the value to 0 and continues happily.[/QUOTE] This is probably why it's undefined behavior in C++.
[QUOTE=Loli;22985509]Because the method is "void" you don't need to return anything :) , Might help you...[/QUOTE] returning in a function =/= ending the program
[QUOTE=ZeekyHBomb;22987028]returning in a function =/= ending the program[/QUOTE] I know, but for future reference. I assumed the question was already answered.
I use void myfunc() { exit(1); }
I'd just like to say that exit(0) is an evil that shouldn't be used 90% of the time (unless you want to just say "fuck it, os, clean up my shit for me" - not usually the best choice) [code] int func(int par) { if(par > 9) return 1; else return 0; } int main() { if(func(12) == 0) return 0; else { while(true) std::cout<<"I like turtles"; } return 0; }[/code]
[QUOTE=Jallen;23075888]I'd just like to say that exit(0) is an evil that shouldn't be used 90% of the time (unless you want to just say "fuck it, os, clean up my shit for me" - not usually the best choice) [code] int func(int par) { if(par > 9) return 1; else return 0; } int main() { if(func(12) == 0) return 0; else { while(true) std::cout<<"I like turtles"; } return 0; }[/code][/QUOTE] I don't like how you are not using spaces between your operators :ohdear:
[QUOTE=Woodcutter11;23076932]I don't like how you are not using spaces between your operators :ohdear:[/QUOTE] He's only doing it for the << operator on cout, so if anything, he's just being inconsistent.
Sorry, you need to Log In to post a reply to this thread.