Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (lgbasallote):

is there an exit code for c++?

OpenStudy (anonymous):

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; }

OpenStudy (anonymous):

@lgbasallote a medal

OpenStudy (lgbasallote):

an exit code that long? lol..could you be more specific?

OpenStudy (lgbasallote):

i mean which is the exit code there?

OpenStudy (anonymous):

@Master.RohanChakraborty can i used exit code in switch stament?

OpenStudy (anonymous):

ya

OpenStudy (anonymous):

@lgbasallote the whole top xplanation is perfect code

OpenStudy (lgbasallote):

the top code is a regular code..im asking which is the exit code there

OpenStudy (anonymous):

int main () { FILE * pFile; pFile = fopen ("myfile.txt","r"); if (pFile==NULL) { printf ("Error opening file"); exit (1); } else { /* file operations here */ } return 0; }

OpenStudy (lgbasallote):

=_=

OpenStudy (lgbasallote):

explain the code pls

OpenStudy (anonymous):

lol Im bad at tat but wait

OpenStudy (lgbasallote):

hmm maybe you copied this code that's why :p

OpenStudy (anonymous):

no its given by stanford I am doing open course there and they ppl gave me this

OpenStudy (anonymous):

technically use it its alright

OpenStudy (lgbasallote):

so what does the code mean then

OpenStudy (lgbasallote):

i just need the specific exit code

OpenStudy (anonymous):

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

OpenStudy (lgbasallote):

i kknow it's error free -_- what i need is the exit code

OpenStudy (anonymous):

ocw.mit.edu

OpenStudy (lgbasallote):

lol. @apoorvk do you know this?

OpenStudy (apoorvk):

Nope. :\

OpenStudy (lgbasallote):

hmm ok then

OpenStudy (anonymous):

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.

OpenStudy (lgbasallote):

what does the number inside the parenthesis mean in exit()?

OpenStudy (anonymous):

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 )

OpenStudy (shadowfiend):

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.

OpenStudy (lgbasallote):

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

OpenStudy (shadowfiend):

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.

OpenStudy (lgbasallote):

ohhhh i see thanks so much!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!