Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Quiting a program, C.

Options
  • 01-11-2006 1:23am
    #1
    Registered Users Posts: 9,847 ✭✭✭


    Hiya,

    Is there a piece of code in C that makes the program quit out of Dos completely.

    For example, if the user types a specific key the entire program closes rather than reverting back to C:\ or whatever.

    Thanks


Comments

  • Registered Users Posts: 9,847 ✭✭✭py2006


    ooooooooooo K!!!

    Well I know you can use the break; command to quite the program!

    But is there a command that actually exits out of DOS altogether?


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    What are you trying to do, quit your application on some input or quit the application and the dos window that ran the application?

    And also why?


  • Registered Users Posts: 9,847 ✭✭✭py2006


    silas wrote:
    What are you trying to do, quit your application on some input or quit the application and the dos window that ran the application?

    And also why?

    Well, I am working on a program that has 10 options for the user. Each option doing a seperate thing.

    If they select 10 the progam should not only quit out of dos altogether.


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    For option 10 the application just simply ends, to actually quit the dos box, would require an windows api call, which i think is a little bit too advanced for this application.

    To quit the application you can use return, exit or abort calls. However the best approach is to have a loop after input is taken in that loops until option 10 is selected, so control bypasses the loop and the program just naturally terminates by hitting the "return 0" line or the end of main().


  • Closed Accounts Posts: 583 ✭✭✭monkey tennis


    Why don't you just... not run it in DOS in the first place? Give them an icon to double-click on. This will run the application in a console, then close the window when the program exits.


  • Advertisement
  • Registered Users Posts: 885 ✭✭✭clearz


    As monkey tennis said already if the user just clicks on the exe then the program will exit automatically once finished but I took the time to show you how its done anyway. The following will close the dos window. Just integrate it into your code as needed.
    #define _WIN32_WINNT 0x0500 // Needed for GetConsoleWindow.
    #include <windows.h>
    
    int main(int argc, char* argv[])
    {
        HWND hWnd = GetConsoleWindow(); // Get a pointer to the console window
        SendMessage(hWnd, WM_CLOSE, NULL, NULL); // Send a close message to the window
        return 0;
    }
    
    


Advertisement