CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 18

Threaded View

  1. #1
    Join Date
    Jan 2009
    Posts
    72

    Access Violation Error Help

    I keep getting this message that I cant figure out how to fix.

    Here is the error:

    First-chance exception at 0x6380f8bc (msvcr90d.dll) in Final.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.
    Unhandled exception at 0x6380f8bc (msvcr90d.dll) in Final.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.

    And the symbols are not loading for: kernel32.dll and ntdll.dll.

    What did I do wrong? ( I know that it has something to do with strlen and the buffer in the GetPet function.)

    (if my code looks funny it is bucause it is an assignment for college. SOme of the things I have done here are a requirment; sorry. And I know that it has some C in it. This is something that I have been informed by my instructor as part of the learning process; vectors, structs, blah, blah, will come later next term.)

    Here is my code:
    Code:
    #define _CRTDBG_MAP_ALLOC
    #include < iostream >
    #include < iomanip >
    #include < cctype >
    #include < cstring >
    using std::cout;
    using std::cin;
    using std::endl;
    using std::setw;
    using namespace std;
    
    //Variables
    const int MAX = 60;
    const int CAT = 20;
    const int DOG = 35;
    int menu_choice;
    char again = 'Y';
    
    char ** petName = new char * [12];
    
    char ** ownName = new char * [12];
    
    char ** petType = new char * [12];
    
    int animalcount = 0;
    int catCount = 0;
    int dogCount = 0;
    int totalCount = 0;
    int count[60] = {0};
    int * arrayCount = &count[0];
    int catCost = 0;
    int dogCost = 0;
    int overallCost = 0;
    int input = 0;
    
    
    //Functions
    void MenuChoice( int &menu_choice );
    void ProcessMenu( int &menu_choice );
    void GetPet( char **& petName, char **& ownName, char **& petType );
    void DisplayAll();
    void RemovePet( char **& petName, char **& ownName, char **& petType );
    void TotalCat( int &catCount, int &catCost );
    void TotalDog( int &dogCount, int &dogCost );
    void TotalDay( int &overallCost, int &catCost, int &dogCost, int &totalCount );
    void AvgCap( int * &arrayCount );
    char Exit( char again );
    
    
    
    int main()
    {
    	//Test for Leaks.
    	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    
    	//Function Pointers
    	void (*fn_ptr_Menu)( int & );
    	fn_ptr_Menu = MenuChoice;
    	void (*fn_ptr_Process)( int & );
    	fn_ptr_Process = ProcessMenu;
    	
    
    
    	while( again == 'Y' )
    	{
    		fn_ptr_Menu( menu_choice );
    
    		fn_ptr_Process( menu_choice );
    	}
    	
    
    
    	//Delete Memory.
    	for( int i = 0; i < 12; i++)
    	{
    		delete[] petName[i];
    		delete[] ownName[i];
    		delete[] petType[i];
    	}
    	delete[] petName;
    	delete[] ownName;
    	delete[] petType;
    
    
    	system( "pause" );
    
    	return 0;
    }
    /***********************************************************************************************************************
    *Name:			MenuChoice
    *Parameters:	&menu_choice
    *Return:		none
    *Purpose:		This function is to display the menu to the user.
    ***********************************************************************************************************************/
    void MenuChoice( int &menu_choice )
    {
    	cout << "\t\t\t\n\nMenu\n\n" << endl;
    	cout << "1) Board a Critter " << endl;
    	cout << "2) Remove a Critter " << endl;
    	cout << "3) Display all Critters " << endl;
    	cout << "4) Count all Critters and Calculate Daily Proceeds " << endl;
    	cout << "5) Exit " << endl;
    	cout << "\n\nPlease enter a menu choice, 1 to 5: " ;
    	cin >> menu_choice;
    }
    /***********************************************************************************************************************
    *Name:			ProcessMenu
    *Parameters:	&menu_choice
    *Return:		none
    *Purpose:		This function is to process the menu choice.
    ***********************************************************************************************************************/
    void ProcessMenu( int &menu_choice )
    {
    	//Function Pointer
    	void (*fn_ptr_disAll)();
    	fn_ptr_disAll = DisplayAll;
    
    	if( menu_choice < 1 || menu_choice > 5 )
    	{
    		cout << "\n\nPlease enter a number between 1 and 5! " << endl;
    	}
    	else
    	{
    		switch ( menu_choice)
    		{
    		case 1 ://Get Pet.
    			{
    				fn_ptr_disAll();
    				GetPet( petName, ownName, petType );
    				break;
    			}
    		case 2://Remove Pet
    			{
    				fn_ptr_disAll();
    				RemovePet( petName, ownName, petType );
    				break;
    			}
    		case 3://Display all info
    			{
    				fn_ptr_disAll();
    				break;
    			}
    		case 4://Display totals and avg
    			{
    				fn_ptr_disAll();
    				TotalCat( catCount, catCost );
    				TotalDog( dogCount, dogCost );
    				TotalDay( overallCost, catCost, dogCost, totalCount) ;
    				AvgCap(arrayCount);
    				break;
    			}
    		case 5://Exit the program
    			{
    				again = Exit( again );
    				break;
    			}
    		}
    	}
    }
    /***********************************************************************************************************************
    *Name:			GetPet
    *Parameters:	petName, ownName, petType
    *Return:		none
    *Purpose:		This function gets the pet info from the user and gets the counts.
    ***********************************************************************************************************************/
    void GetPet( char **& petName, char **& ownName, char **& petType )
    {
    	
    	char buffer[256] = {0};
    
    	if( animalcount == 12)
    	{
    		cout << "\n\nYou cannot board anymore animals! You are at maximum capacity! " << endl;
    	}
    	else
    	{
    
    		cout << "Which cage number do you want to add the critter to? ( 1 to 12) ";
    		cin >> input;
    
    		if( input < 1 || input > 12 )
    		{
    			cout << "Please input a number between 1 and 12! " << endl;
    
    		}
    		else
    		{
    			int k = input - 1;
    			char * str = 0;
    
    			//Get input for the Pet Name, Owner name, and Pet Type.
    			cout << "\n\nPlease input Pet Name: ";
    			cin.ignore( cin.rdbuf()->in_avail() );
    			cin.getline( buffer, MAX );
    			cin.clear();
    			cin.ignore( cin.rdbuf()->in_avail() );
    
    			str = new char[strlen( buffer ) + 1];
    			strcpy( str, buffer );
    			petName[k] = str;
    
    
    
    			cout << "\n\nPlease input Owner Name: ";
    			cin.ignore( cin.rdbuf()->in_avail() );
    			cin.getline( buffer , MAX );
    			cin.clear();
    			cin.ignore( cin.rdbuf()->in_avail() );
    
    			str = new char[strlen( buffer ) + 1];
    			strcpy( str, buffer );
    			ownName[k] = str;
    
    
    
    			cout << "\n\nPlease input critter type: ";
    			cin.ignore( cin.rdbuf()->in_avail() );
    			cin.getline( buffer, MAX );
    			cin.clear();
    			cin.ignore( cin.rdbuf()->in_avail() );
    
    			str = new char[strlen( buffer ) + 1];
    			strcpy( str, buffer );
    			petType[k] = str;
    			
    
    
    			//Add to the counts.
    			animalcount++;
    			totalCount ++;
    	
    			//Add count to the avg array function helper.
    			* arrayCount = totalCount;
    			arrayCount++;
    
    			//cat count.
    			int result;
    
    			 _strlwr( *petType );
    			result = strcmp( *petType, "cat" );
    		
    			if( result == 0 )
    			{
    			
    				catCount++;
    		
    			}
    
    			//dog count.
    			int result1;
    
    			_strlwr( *petType );
    			result1 = strcmp( *petType, "dog" );
    		
    			if( result1 == 0 )
    			{
    				dogCount++;
    			
    			}	
    		
    
    			cout << "\n\nCritter " << input << ", has been added! " << endl;
    
    		}
    	}
    }
    /***********************************************************************************************************************
    *Name:			DisplayAll
    *Parameters:	none
    *Return:		none
    *Purpose:		This function displays all pet infomation to the user.
    ***********************************************************************************************************************/
    void DisplayAll()
    {
    	cout.setf( ios::left );
    	cout << setw(10) << "Cage Num." << setw(20) << "Pet Name" << setw(20) << "Owner's Name" <<  setw(20) << "Pet Type" << endl;
    	for( int i = 0; i < 12; i++ )
    	{
    		
    		cout << setw(10) << i+1 << setw(20) << petName[i] << setw(20) << ownName[i] << setw(20) << petType[i] << endl;
    	}
    
    }
    /***********************************************************************************************************************
    *Name:			RemovePet
    *Parameters:	petName, ownName, petType
    *Return:		none
    *Purpose:		This function displays all pet infomation to the user.
    ***********************************************************************************************************************/
    void RemovePet( char **& petName, char **& ownName, char **& petType )
    {
    
    		cout << "Which cage number do you want to remove the critter from? ( 1 to 12) ";
    		cin >> input;
    	
    		int j = input - 1;
    		//Delete memory
    		delete [] petName[j];
    		delete [] ownName[j];
    		delete [] petType[j];
    		
    		//Correct the count
    		animalcount--;
    		
    		cout << "\n\nCritter " << input << ", has been removed. " << endl;
    
    }
    /***********************************************************************************************************************
    *Name:			Exit
    *Parameters:	again
    *Return:		none
    *Purpose:		This function exits the program.
    ***********************************************************************************************************************/
    char Exit( char again )
    {
    	again = 'n';
    
    	return again;
    }
    /***********************************************************************************************************************
    *Name:			TotalCat
    *Parameters:	int &catCount, int &catCost
    *Return:		none
    *Purpose:		This function finds the  cat costs and displays.
    ***********************************************************************************************************************/
    void TotalCat(  int &catCount, int &catCost )
    {
    	
    	catCost = catCount * CAT;
    
    	cout << "\n\nThe Amount of Cats today is: " << catCount << ". " << endl;
    	cout << "The Sum of today's Cat earnings is: " << catCost << ". " << endl;
    }
    
    /***********************************************************************************************************************
    *Name:			TotalDog
    *Parameters:	petType
    *Return:		none
    *Purpose:		This function finds the dog costs and displays.
    ***********************************************************************************************************************/
    void TotalDog( int &dogCount, int &dogCost )
    {
    
    	dogCost = dogCount * DOG;
    
    	cout << "\n\nThe Amount of Dogs today is: " << dogCount << ". " << endl;
    	cout << "The Sum of today's Dog earnings is: " << dogCost << ". " << endl;
    }
    /***********************************************************************************************************************
    *Name:			TotalDay
    *Parameters:	int &overallCost, int &catCost, int &dogCost, int &animalcount
    *Return:		none
    *Purpose:		This function finds the total for the day.
    ***********************************************************************************************************************/
    void TotalDay( int &overallCost, int &catCost, int &dogCost, int &totalCount )
    {
    	overallCost = catCost + dogCost;
    
    	cout << "\n\nThe Total Amount of Critters today is: " << totalCount << ". " << endl;
    	cout << "Total amount of animals right now: " << animalcount << ". " << endl;
    	cout << "The Sum of today's  earnings is: " << overallCost << ". " << endl;
    }
    /***********************************************************************************************************************
    *Name:			AvgCap
    *Parameters:	none
    *Return:		none
    *Purpose:		This function finds the total avg for the day of animals.
    ***********************************************************************************************************************/
    void AvgCap( int * &arrayCount )
    {
    	int avg = 0;
    	int sum = 0;
    	arrayCount = &count[0];
    
    	for( int i = 0; i < totalCount; i++, arrayCount++ )
    	{
    		sum += * arrayCount;
    	}
    
    	avg = sum / totalCount;
    
    		cout << "\n\nThe Average Amount of Critters today is: " << avg << ". " << endl;
    	
    }
    Last edited by Shadow_Govt; June 10th, 2009 at 10:43 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured