Margherita
Margherita

Reputation: 73

Exiting a program ..alternative (C Language)

This is the code I use for a list of options in my program.

Of course, if the numbers of options grows (more than 9) I have to change the while condition taking a different number.

What I'd like to avoid is to resort to a two digit number.

How could I make the user to press the 'ESC' key to exit the program? or any other key or different solution if any available....

   while (option < 9)
    {
    system("clear");

            printf("+------------------------------------------+\n");
            printf("|   xxxxxxxxxxxxxx             |\n");
            printf("+------------------------------------------+\n");
            printf("                                        \n");
            printf("********************************************\n");
            printf("|         Operations                   |\n");
            printf("********************************************\n");
            printf("| 1) Option 1                              |\n");
            printf("| 2) Option 2                              |\n");
            printf("| 3) Option 3                              |\n");
            printf("| 9) EXIT                                  |\n");
            printf("+------------------------------------------+\n");

            printf("Option -->: ");
            scanf("%d", &option);

            if (option==1) func1(conn);
            if (option==2) func2(conn);
            if (option==3) func3(conn);
    } 
            mysql_close (conn);

        system("clear");

        exit (EXIT_SUCCESS);

}

Thanks Mauro

Upvotes: 0

Views: 552

Answers (1)

K-ballo
K-ballo

Reputation: 81349

Read a char instead of an int (or better yet, a key press). Then if the so read char is between '0' and '9' turn it into an integer and use it as option; otherwise make your loop terminate, return from main, call exit or whatever way you prefer to end execution.

Upvotes: 1

Related Questions