is there an exit code for c++?
Use this code /* exit example */ #include <stdio.h> #include <stdlib.h> int main () { FILE * pFile; pFile = fopen ("myfile.txt","r"); if (pFile==NULL) { printf ("Error opening file"); exit (1); } else { /* file operations here */ } return 0; }
@lgbasallote a medal
an exit code that long? lol..could you be more specific?
i mean which is the exit code there?
@Master.RohanChakraborty can i used exit code in switch stament?
ya
@lgbasallote the whole top xplanation is perfect code
the top code is a regular code..im asking which is the exit code there
int main () { FILE * pFile; pFile = fopen ("myfile.txt","r"); if (pFile==NULL) { printf ("Error opening file"); exit (1); } else { /* file operations here */ } return 0; }
=_=
explain the code pls
lol Im bad at tat but wait
hmm maybe you copied this code that's why :p
no its given by stanford I am doing open course there and they ppl gave me this
technically use it its alright
so what does the code mean then
i just need the specific exit code
basically ERROR FREE and is okay in the matter of programming and this specific code has a perfect finishing k if u want more explanation go to OCW of stanford or MIT
i kknow it's error free -_- what i need is the exit code
ocw.mit.edu
lol. @apoorvk do you know this?
Nope. :\
hmm ok then
What exactly do you mean with exit code? I think it can have two meanings: either the return value of a program or actual C++ code with which you can exit the program. The first one is often used to indicate if a program finished successfully (usually a return value of 0) or if an error occurred (e.g. a return value of 10 if the file did not exist). If you want actual C++ code to quit your program, there are two ways. First, you can simply use a return statement. Like "return 1;". You can also use the exit() function that Master.RohanChakraborty used in his example, like "exit( 1 );". You'll need to include "cstdlib" to use the exit() function.
what does the number inside the parenthesis mean in exit()?
The number between parenthesis (and the number in the return statement) represent the exit status of a program (which I mentioned as exit code earlier). (see http://en.wikipedia.org/wiki/Exit_status )
To be clear, the exit code always refers to the status code or exit status. There is such a thing as code that can make the program exit, namely by invoking the exit function. But that is not “exit code”, as the term “exit code” has a very specific meaning. If you've been writing proper C++ programs you'll see that you always return 0 from the end of your main function. That is where the program exits. If you return something other than 0, you indicate that something went wrong with your program. That number is the exit code, and as slotema pointed out, if you call the exit function, you pass it an exit code, which can be 0 or something else depending on whether the program terminated normally or with an error.
ohhh that makes sense...so if i use return 0 then will the progam exit on its own after displaying the code? because a problem of mine is when i end it with getch(); i have to click twice before it will exit
The status code isn't displayed. It's simply available for whatever program launched yours. Usually that's a shell, and depending on the shell that code is available in different variables. For example, if you run a program and you then do “echo $?” in BASH, you will typically get the exit code. If you want to display something before exiting, you just have to use printf as your last statement.
ohhhh i see thanks so much!
Join our real-time social learning platform and learn together with your friends!