Jump to content
  • Advertisement
Sign in to follow this  
MSalley

Untitled

This topic is 4711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to create a function that ends a program, but if I create one of the void type it won't let me use return 0, because it's of type void. How should I do it?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by MSalley
I want to create a function that ends a program, but if I create one of the void type it won't let me use return 0, because it's of type void. How should I do it?


There are ways to do this, but I feel you're taking the wrong approach. The simplest way to do that:


bool closeApplication = false;

int main() {
while(!closeApplication() {
doStuff();
}

return 0;
}


void functions are used when a return value is not necessary. They do not end execution.

Share this post


Link to post
Share on other sites
I agree with xMcBaiNx, it's easiest to exit from the main-function by simply returning the value 0 with the return-function. If one really wants to end the program elsewhere though there's a standard function which terminates the program:
exit(0); //return the value 0 indicating it's a normal exit
If an error occured one can return the value 1 instead:
exit(1);

Example program:


#include <iostream>

void endProgram(); //end-function prototype

int main()
{
endProgram();

std::cout <<"This is a normal exit.\n";
return 0; //return 0 - normal exit
}

//end-function definition
void endProgram()
{
//any cleaning up could be done here such as deleting allocated memory:

std::cout <<"This is an exit by calling the exit function.\n";
exit(0); //0 - normal exit
}



\Jimmy H

Share this post


Link to post
Share on other sites
It's also OK* to end a void main() with simply return; (no value).






*I don't recommend or advocate writing void main(). Ever. int main(int argc, char** argv) is so natural to type.

Share this post


Link to post
Share on other sites
Quote:
Original post by Boku San
It's also OK* to end a void main() with simply return; (no value).


No it's not, because void main() is never OK to type (it was allowed in C89 IIRC, but it's not legal in C++ (and I think it might not be legal C99 either)). Recent compilers will give you an error message.

There are two legal forms for main:

1) int main () / int main ( void ) //these mean the same thing... return an int, accept no arguments
2) int main ( int argc , char * argv [] )

main is a very weird function in that:

1) You can't overload it. You're not allowed to.
2) If it reaches the end of the function without a return statement, it's assumed to return 0, rather than invoking undefined behavior. That is, this is legal and has defined behavior:
#include <iostream>
using namespace std;
int main () {
cout << "I like candy" << endl;
//no return statement.
}

Share this post


Link to post
Share on other sites
Well, this is what I think you're trying to do (I might be totally wrong, but...): you're trying to make a function like this:

void exitProgram()
{
// This is supposed to exit the program!
return 0;
}

int main()
{
// Do stuff

exitProgram();
}


Right? In that case, this doesn't work because 'return 0;' is not a universal statement to end the program. Instead, the 'return' statement just exits out of the function you put it in. Therefore, putting return; in exitProgram() makes the program exit exitProgram() but not main(). When you exit main() (with the return statement), the program ends.

If you want a function to exit the program, try exit(). You can use it like this:

int main()
{
exit( 0 ); // Instead of return 0
}


You need to include <cstdlib.h> I think.

Hope that helps!

Share this post


Link to post
Share on other sites
Pretty much, but I'm calling exitProgram() from another function(not main.) I used the exit(0) thing, It's cool. Thanks everyone for the help!!! Out.

Share this post


Link to post
Share on other sites
Quote:
Original post by fyhuang

int main()
{
exit( 0 ); // Instead of return 0
}



You can also use

#include <cstdlib>

exit(EXIT_SUCCESS);
// or
exit(EXIT_FAILURE);


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!