|
-
June 10th, 2009, 12:35 AM
#3
Re: Access Violation Error Help
Alright. been messing with it a little bit and got most of it to work. I am leaking memory somewhere; cant figure it out. Something about a normal block line 155? Whenever I try to exit the program I get a heap assertion error.
Here is my new 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 * [256];
char ** petTemp = 0;
char ** ownName = new char * [256];
char ** ownTemp = 0;
char ** petType = new char * [256];
char ** typeTemp = 0;
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' )
{
MenuChoice( menu_choice );
ProcessMenu( menu_choice );
}
//Delete Memory.
for( int i = 0; i < animalcount; i++)
{
delete[] petName[i];
delete[] ownName[i];
delete[] petType[i];
}
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.
{
GetPet( petName, ownName, petType );
break;
}
case 2://Remove Pet
{
DisplayAll();
RemovePet( petName, ownName, petType );
break;
}
case 3://Display all info
{
DisplayAll();
break;
}
case 4://Display totals and avg
{
DisplayAll();
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 )
{
//buffer.
char buffer[256] = {0};
//temp arrays
petTemp = new char * [animalcount + 1];
ownTemp = new char * [animalcount + 1];
typeTemp = new char * [animalcount + 1];
//addresses for temp
for( int i = 0; i < animalcount; i++ )
{
petTemp[i] = petName[i];
ownTemp[i] = ownName[i];
typeTemp[i] = petType[i];
}
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;
//delete old information
delete [] petName;
delete [] ownName;
delete [] petType;
//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() );
petTemp[k] = new char[strlen( buffer ) + 1];
strcpy( petTemp[k], buffer );
cout << "\n\nPlease input Owner Name: ";
cin.ignore( cin.rdbuf()->in_avail() );
cin.getline( buffer , MAX );
cin.clear();
cin.ignore( cin.rdbuf()->in_avail() );
ownTemp[k] = new char[strlen( buffer ) + 1];
strcpy( ownTemp[k], buffer );
cout << "\n\nPlease input critter type: ";
cin.ignore( cin.rdbuf()->in_avail() );
cin.getline( buffer, MAX );
cin.clear();
cin.ignore( cin.rdbuf()->in_avail() );
typeTemp[k] = new char[strlen( buffer ) + 1];
strcpy( typeTemp[k], buffer );
//add the new info.
petName = petTemp;
ownName = ownTemp;
petType = typeTemp;
//Add to the counts.
animalcount++;
totalCount ++;
//Add count to the avg array function helper.
* arrayCount = totalCount;
arrayCount++;
//cat count.
_strlwr( *petType );
char * breaker[10]= {0};
* breaker = petType[k];
int result;
result = strcmp( * breaker, "cat" );
int result1;
result1 = strcmp( * breaker, "dog" );
if( result == 0 )
{
catCount++;
}
if( result1 == 0 )
{
//dog count.
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 < totalCount; 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 old information
delete [] petName[j];
delete [] ownName[j];
delete [] petType[j];
strcpy( petName[j], "Empty" );
strcpy( ownName[j], "Empty" );
strcpy( petType[j], "Empty" );
//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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|