Application refusing to exit

This is a discussion on Application refusing to exit within the C++ Programming forums, part of the General Programming Boards category; Hey again, I have another problem. I wrote a function to hande my menu switch statement: ResourceMgr.cpp Code: ... void ...

  1. #1
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156

    Application refusing to exit

    Hey again, I have another problem.
    I wrote a function to hande my menu switch statement:

    ResourceMgr.cpp
    Code:
    ...
    void ResourceMgr::vComputeMainMenuChoice()
    {
    	std::cin >> m_iChoice; /*Wait for the user input(choice)*/
    	switch(m_iChoice) /*Pass the choice variable into a switch statement*/
    	{
    	case 1: /*If the user entered "1"...*/
    		/*Do this*/
    		break;
    	case 2: /*If the user entered "2"...*/
    		iExitApp(); /*...do this*/
    		break;
    	default: /*If the user didn't enter "1" or "2"...*/
    		vDisplayErrorMessage();
    		iExitApp();
    		break;
    	}
    }
    ...
    ... Basically I want my application to exit where I use my function iExitApp():

    ResourceMgr.cpp
    Code:
    ...
    int ResourceMgr::iExitApp()
    {
    	vDisplayExitMessage();
    	return 0;
    }
    ...
    ... to test this, I added an output statement at the end of main() to make sure it was working.... and guess what, it doesn't! It continues to do the rest of the statements in main() and then exits... I don't understand :S

    Main.cpp
    Code:
    int main() /*define the main function*/
    {
    	/*Instances of incorporated classes*/
    	ResourceMgr ResourceManager; /*Create an instance of ResourceMgr*/
    
    	ResourceManager.vComputeMainMenuChoice();
    
    	std::cout << "Test" << std::endl;
    
    	return 0; /*Return "All Good"*/
    }
    ...My program outputs "Test" to the console and then exits..... what's the problem?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Why should it exit? Unless you are willing to use exit() / abort() (or throw an exception) - but these are for forced exit and not something to use under normal conditions - a function call cannot make the application exit. It returns to where it was called from. Only in main, returning ends the program.

    Instead you should structure the code to use return values to indicate that program should exit:

    Code:
    //returns false when exit option was selected
    bool ResourceMgr::vComputeMainMenuChoice()
    {
    	std::cin >> m_iChoice; /*Wait for the user input(choice)*/
    	switch(m_iChoice) /*Pass the choice variable into a switch statement*/
    	{
    	case 1: /*If the user entered "1"...*/
    		/*Do this*/
    		break;
    	case 2: /*If the user entered "2"...*/
    		return false;
    		break;
    	default: /*If the user didn't enter "1" or "2"...*/
    		vDisplayErrorMessage();
    		return false;
    		break;
    	}
            return true;
    }
    
    int main() /*define the main function*/
    {
        /*Instances of incorporated classes*/
        ResourceMgr ResourceManager; /*Create an instance of ResourceMgr*/
    
        if (!ResourceManager.vComputeMainMenuChoice()) {
            ResourceManager.vDisplayExitMessage();
            return 0;
        }
    
        std::cout << "Test" << std::endl;
    
        return 0; /*Return "All Good"*/
    }
    Last edited by anon; 06-16-2009 at 04:07 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Thanks anon, that did the trick, makes sense when you explain it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  2. Problem with com application
    By amardon in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2005, 05:50 AM
  3. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  4. Application Exit
    By gvector1 in forum C# Programming
    Replies: 2
    Last Post: 10-23-2003, 12:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21