Ali Appleby
Ali Appleby

Reputation: 419

How to exit a console app in C using switch case?

For example, if I made a simple switch-case how would I make the console app close depending on the input? I don't want to use to use break; and skip the loop, I want to close the console altogether.

char choice; 

printf("Run random function \n");
printf("Exit \n");

choice = getchar();
fflush(stdin);

switch(choice)
{

            case '1':
                 //randomFunction();

            case '2':
                 //I want this case to exit the console the console
}

Upvotes: 1

Views: 2835

Answers (2)

P0W
P0W

Reputation: 47814

Simply use exit (EXIT_SUCCESS);

Ref -exit

Upvotes: 1

Edward Clements
Edward Clements

Reputation: 5132

You could call exit() passing the return value as a parameter, this return value can be checked by the calling process / batch file:

exit(EXIT_SUCCESS); // = 0 = (standard) success

or

exit(EXIT_FAILURE); // = 1 = (standard) failure

or

exit(123); // return a specific value

Documentation for MS-Visual Stuidio

Upvotes: 0

Related Questions