Thread: Need help on how to exit a program properly.

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    3

    Need help on how to exit a program properly.

    I have a problem with my program and that is it won't exit properly. My aim is to have the user input "q" or "Q" for the program to exit. But instead of exiting immediately when Q is inputted. The program just continues and any further input can't be inputted.

    This is my code(It is required for the user to only input Q/q to exit the program):
    Code:
    #include<stdio.h>#include<conio.h>
    #include<stdlib.h>
     
    void printmenu();
    void bubble_sortASC(int[], int);
    void bubble_sortDSC(int[], int);
    void selection_sortASC(int[], int);
    void selection_sortDSC(int[], int);
    void insertion_sortASC(int[], int); 
    void insertion_sortDSC(int[], int);
    
    
    void main() {
       int arr[30], num, i, choice;
       char order, order2, order3;
     
       printf("\nEnter no of elements :");
       scanf("%d", &num);
     
       printf("\nEnter array elements :");
       for (i = 0; i < num; i++)
          scanf("%d", &arr[i]);
       
        printmenu();
       
       
       
       do{
           
       scanf("%d",&choice);
    
    
       
       switch(choice){
           case 1:
               printf("Input 'A' for Ascending and 'D' for Descending:");
               scanf(" %c",&order);
               
            switch(order){
                case 'a':
                    printf("Chosen Ascending Order.\n");
                    bubble_sortASC(arr, num);
                    break;
                case 'A':
                    printf("Chosen Ascending Order.\n");
                    bubble_sortASC(arr, num);
                    break;
                case 'D':
                    printf("Chosen Descending Order.\n");
                    bubble_sortDSC(arr, num);
                    break;
                case 'd':
                    printf("Chosen Descending Order.\n");
                    bubble_sortDSC(arr, num);
                    break;
                    
            }
            printmenu();
        break;
        case 2:
               printf("Input 'A' for Ascending and 'D' for Descending:");
               scanf(" %c",&order2);
               
            switch(order2){
                case 'a':
                    printf("Chosen Ascending Order.\n");
                    selection_sortASC(arr, num);
                    break;
                case 'A':
                    printf("Chosen Ascending Order.\n");
                    selection_sortASC(arr, num);
                    break;
                case 'D':
                    printf("Chosen Descending Order.\n");
                    selection_sortDSC(arr, num);
                    break;
                case 'd':
                    printf("Chosen Descending Order.\n");
                    selection_sortDSC(arr, num);
                    break;
            }
            printmenu();
            
            
               
           break;
           case 3:
               printf("Input 'A' for Ascending and 'D' for Descending:");
               scanf(" %c",&order3);
               
            switch(order3){
                case 'a':
                    printf("Chosen Ascending Order.\n");
                    insertion_sortASC(arr, num);
                    break;
                case 'A':
                    printf("Chosen Ascending Order.\n");
                    insertion_sortASC(arr, num);
                    break;
                case 'D':
                    printf("Chosen Descending Order.\n");
                    insertion_sortDSC(arr, num);
                    break;
                case 'd':
                    printf("Chosen Descending Order.\n");
                    insertion_sortDSC(arr, num);
                    break;
                }
                printmenu();
      /* default:
               printf("Something went wrong. Your input may have been invalid.");
               break; */
        }
            }while(choice != 'Q' && choice != 'q');
    
    
               
        getch();
    
    
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main()
    main returns int.

    > while(choice != 'Q' && choice != 'q')
    You made choice an int, so typing in letters isn't going to work.


    Also, you could really do with some more functions.
    Code:
    switch(choice){
        case 1:
            doBubbleChoice(arr,num);
            break;
        case 2:
            doSelectionChoice(arr,num);
            break;
        case 3:
            doInsertionChoice(arr,num);
            break;
    Your main would be a manageable length, not 100+ lines.

    Also, use case fallthough.
    Code:
                case 'a':
                case 'A':
                    printf("Chosen Ascending Order.\n");
                    insertion_sortASC(arr, num);
                    break;
    Or
    Code:
    switch(tolower(order3)) {  // ctype.h
                case 'a':  // tolower('A') == 'a'
                    printf("Chosen Ascending Order.\n");
                    insertion_sortASC(arr, num);
                    break;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2019
    Posts
    3
    Thank you for the really helpful information. I intentionally didn't add in the function definitions as I don't see it relevant to my question. Is there really no other way to make Q as the exit? I find that odd since this is a homework activity and that my prof really did add in the instruction to make Q as the exit/quit.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You need to make choice a char type then.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exit loop not working properly
    By melodia in forum C Programming
    Replies: 3
    Last Post: 11-21-2010, 06:04 PM
  2. How to exit program???
    By spottedzebra in forum C Programming
    Replies: 10
    Last Post: 06-21-2010, 04:43 PM
  3. Cannot get my program to exit properly (switch related)
    By Skarjak in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2005, 10:14 AM
  4. Program uses a lot of memory and doesnt exit properly
    By TJJ in forum Windows Programming
    Replies: 13
    Last Post: 04-28-2004, 03:13 AM
  5. Before your program does exit(0);
    By ycode in forum C Programming
    Replies: 1
    Last Post: 04-02-2003, 11:47 PM

Tags for this Thread