Can I crash the program ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can I crash the program ?

Example: if the user inputs something i don't want he puts and i want the program to crash or exit

19th Jul 2016, 5:55 PM
Peter Müller
Peter Müller - avatar
8 Answers
+ 2
decent exit.... return 0; crash exit ..... 0/0; 😊
20th Jul 2016, 2:27 PM
Mukul Kumar
Mukul Kumar - avatar
0
Return 0; from main. It's the most ideal way to do it, always opt for it unless absolutely necessary to use a different method.
19th Jul 2016, 6:35 PM
Cohen Creber
Cohen Creber - avatar
0
Yeah. You can always crash your code. For that, you need to expect the undesired values and always exit your program
19th Jul 2016, 7:03 PM
chaitanya guruprasad
chaitanya guruprasad - avatar
0
That's not crashing... try to access memory you didn't allocate like 1000x. That should *really* force a proper operating system to end your program immediately.
19th Jul 2016, 7:55 PM
Stefan
Stefan - avatar
0
Return 0; is not crashing, no. But he stated he wanted a crash ~or~ exit, which the safest and most efficient way would be to return 0 from main. Besides, I'm not sure why someone would want to cause an intentional crash over something much better.
19th Jul 2016, 8:03 PM
Cohen Creber
Cohen Creber - avatar
0
So here you have a crashing solution :-) Actually posted a game in this forum a while ago that crashes with a 10% probability, if the uses loses the challenge. :-)
19th Jul 2016, 8:15 PM
Stefan
Stefan - avatar
0
don't works so maybe because i return twice ( at the end again) else if(y != 1 && y != 0){ cout << "error" << endl; return 0; } bonus question where did it return if i write return 0; ?
19th Jul 2016, 9:02 PM
Peter Müller
Peter Müller - avatar
0
You return the "exit code" (the value behind the return in main) to the operating system. Btw the error might be the "else" in front of the "if" this is only valid C++, if you already wrote an if clause.
19th Jul 2016, 9:58 PM
Stefan
Stefan - avatar