I am just learning how to work with multiple files instead of putting all my code in 1.

When I have the delete lines that are marked it seems to crash the program. I must be over looking something simple here on why it is crashing.

// golf.cpp -- Definitions for prototypes defined in golf.h
//

#include "stdafx.h"
#include "golf.h"
#include <iostream>
#include <cstring>

// non-interactive version:
// function sets golf structure to provided name, handicap
// using values passed as arguments to the function
void setgolf(golf & g, const char * name, int hc)
{
	strcpy(g.fullname, name);
	g.handicap = hc;
}

// interactive version:
// function solicits name and handicap from user
// and sets the members of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf(golf & g)
{
	int hc;
	char *name = new char [40];
	std::cout << "Please input a name: ";
	std::cin >> name;
	std::cout << "Please input your handicap: ";
	std::cin >> hc;
	strcpy(g.fullname, name);
	g.handicap = hc;
	std::cout << "Pass 1" << std::endl;
	if (name = "")
	{
		//delete [] name; // Causes issues
		std::cout << "Pass 3" << std::endl;
		return 0;
	}
	//delete [] name; // Causes issues
	std::cout << "Pass 4" << std::endl;
	return 1;
}

// function resets handicap to new value
void handicap(golf & g, int hc)
{
	g.handicap = hc;
}

// function displays contents of golf structure
void showgolf(const golf & g)
{
	std::cout << "Full name is : " << g.fullname << std::endl;
	std::cout << "Handicap is: " << g.handicap << std::endl;
}

You could replace char* name with std::string name so that you do not have to allocate any memory for that variable. If that doesn't fix the problem then the problem is caused by something else in your program, such as buffer overflow somewhere else that is destroying the heap.

This article has been dead for over six months. Start a new discussion instead.