Thursday, July 9, 2009

What is the syntax for ending a program in C++,eg: if(a==10){//end the whole program}?

if (a==10)


exit(0);

What is the syntax for ending a program in C++,eg: if(a==10){//end the whole program}?
It depends on the main you use.





Ex: If you use void main(), then you need not return.





If you use int main() then you should return 0 for successful completion and return 1 to indicate unsuccessful completion.





Hope this satisfied your requirement.
Reply:first of all write #include%26lt;stdlib.h%26gt; at the top of the program and other require header file also.........





then do like that





if(a==10)


exit();
Reply:if u have assigned a int or char to the main then u need to return a value by typing return(something);else if u have not assigned anything then u dont need to do anything
Reply:return
Reply:if(a==0)


{


terminate();


}





will do it, but is not the right way to do it (i.e. you might use this in a piece of panic code when you have an uncaught exception etc). In a managed exit, you need to call the destructor of each of the classes on the stack.





Or construct your programme to always test the return from a function for an error value (example: return -1; ) and pass that up the stack so the compiler can deal with it for you.


No comments:

Post a Comment