Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exiting a dos based program?

Status
Not open for further replies.

FURY

Programmer
Aug 9, 2001
17
GB
ok if u making a program that just displays text in a dos window what code do u use say if u wanted a option on a menu for exit?
 
You could use switch statement:

...
printf("1: Option\n");
printf("2: Option\n");
printf("3: Option\n");
printf("4: Option\n");
printf("5: Exit \n");

printf("Enter your choice\n");
char option;
scanf("%c",&option);

switch(option)
{
case '5':
printf("You chose to exit, press a button");
option = getch();
exit(0);
...
}
Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
can u tell me why i keep getting this error im really new to c, also im trying to call a function from a switch statement to display another menu can u help

Call to function 'exit' with no prototype in function main
 
you need to include the file that exit() is defined in...

make sure you call exit(0) with an integer, and post your code for more help.

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
ok u stated "you need to include the file that exit() is defined in..." can u give me an example
 
//File main.cpp

#include <stdlib.h>

void main(void){
exit(0);
}

//End file main.cpp

Just like aphrodita said, this is a complete example for you.

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Only don't return void from main() and if you're writing C code, make sure you save your files with a .c extension.

Another option is to use return instead of exit():

int main(void)
{
return 0;
}

Russ
bobbitts@hotmail.com
 
Hi,
I would like to put some light on use of exit(0) with exit(interger>0). 0 passed to exit(0) means normal termination of a program. Also means that the program has successfully exited its termination whereas exit(1) means some error happened in the execution, and the program has forcefully exited(Abnormal termination).
Code:
#include<stdlib.h>
#include<stdio.h>
   void main()
   {
      FILE *fp;
      
      fp = fopen(&quot;myfile.dat&quot;,&quot;r&quot;);
      if(fp==NULL)
      {
          printf(&quot;Unable to open file&quot;);
          exit(1);
      }
      else
      {
          printf(&quot;File openned successfully&quot;);
          exit(0);
      }
    }


Hope this helps in better understanding. :)

Hemant.
 
another void main()... :(

To shed a bit more light, the only portable arguments to exit() are 0, EXIT_SUCCESS and EXIT_FAILURE. The C standard leaves any other arguement value, including 1 (which may or may not equal EXIT_FAILURE), undefined.

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top