Error while running

Hi!

I have some code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /*
* The Amazing Journey
* main.cpp 
*/ 
#include <cstdlib>
#include "Setup.h"
#include "SETUPDEFINE.h";

GameSetup * objSetup;

//using namespace std
int main(){
	Player P1;
	objSetup = new GameSetup;
	objSetup->Setup(P1);
	delete[] objSetup;
	return 0 ;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 *  Setup.cpp
 *  TheAmazingJourney
 *
 *  Created by Nicky on 5/14/13.
 *  Copyright 2013 __MyCompanyName__. All rights reserved.
 *
 */

#include "Setup.h"
#include <iostream>
#include <fstream>
#include "SETUPDEFINE.h"
using namespace std;
void GameSetup::Setup(Player& PLAYER){
	string GAMECHOICE = "NULL";
	cout << "***********************\n";
	cout << "****Dungeon Slasher****\n";
	cout << "***********************\n";
	sleep(1);
	string INPUT_RACE;
	srand(time(NULL));
	cout <<"New Game <N>\n";
	cout << "Load Game <L>\n";
GetMode:		
	getline(cin, GAMECHOICE);
	transform(GAMECHOICE.begin(), GAMECHOICE.end(), GAMECHOICE.begin(), ::toupper);
	if(GAMECHOICE == "N"){
		cout << "New game!\n";
		sleep(1);
		cout << "Welcome to DUNGEON SLASHER!\nThis game is a game where you have to slash through monsters to get treasure and defeat the dragon!\n";
		sleep(2);
		cout << "Type Your Race! You can be an ELF, a DWARF, a HALFLING, a HUMAN, or a HALF-ELF";
	GetRace:
		getline(cin, INPUT_RACE);
		transform(INPUT_RACE.begin(), INPUT_RACE.end(), INPUT_RACE.begin(), ::toupper);
		Player::SET_DEFAULTS(PLAYER);
		if (INPUT_RACE == "ELF")Player::SET_ELF(PLAYER);
		else if (INPUT_RACE == "DWARF")Player::SET_DWARF(PLAYER);
		else if (INPUT_RACE == "HALFLING")Player::SET_HALFLING(PLAYER);
		else if (INPUT_RACE == "HUMAN")Player::SET_HUMAN(PLAYER);
		else if (INPUT_RACE == "HALF-ELF")Player::SET_HALF_ELF(PLAYER);
	    else{
		    cout << "Oops! Your race does not exist. Try typing it again, and be carful of spelling!";
		    goto GetRace;//getline again until valid race is chosen
		};
		
	}
	else{
		cout << "Oops! Only input N, Load function is not working!!";
		goto GetMode;//getline until valid mode is chosen
	};
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 *  Setup.h
 *  TheAmazingJourney
 *
 *  Created by Nicky on 5/14/13.
 *  Copyright 2013 SaltPepper. All rights reserved.
 *
 */

#ifndef __SETUP_H_INCLUDED__
#define __SETUP_H_INCLUDED__
#include "SETUPDEFINE.h"

class GameSetup{
public:
	void Setup(Player& PLAYER);
	GameSetup(){};
	~GameSetup(){};
};
#endif//Included yay!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 *  RPGDEFINE.h
 *  TheAmazingJourney
 *
 *  Created by Nicky on 5/3/13.
 *  Copyright 2013 SaltPepper. All rights reserved.
 *
 */
//=================================
// include guard
#ifndef __RPGDEFINE_H_INCLUDED__
#define __RPGDEFINE_H_INCLUDED__
//=================================
// forward declared dependencies
//=================================
// included dependencies
//=================================
// the actual class
#define MAX_STATUS 10 
#include <cstdlib>
enum race_t{ELF, DWARF, HALFLING, HUMAN, HALF_ELF};
enum condition_t {HEALTHY, POISONED, WITHER, CLUMSY};
enum magetype_t {FIRE, WATER, EARTH, AIR, DEATH, LIGHT};
enum potion_t{STRONG, FAST, POISON, INSTANTKILL, MAGICAL};
enum weapon_t {SHORT_SWORD, LONG_SWORD, DAGGER, BOW, CROSSBOW, STAFF, WAND};
enum enchantments_t{POISONS, BURN, LIGHTNING, WITHERING, ONEHITKILL, AIRCHANT, NONE};
enum status_t {MELEE, RANGED, WIZARD};
enum weight_t {FEATHER, MEDIUM, HEAVY};
using namespace std;
#include <fstream>
class Player{
public:	
	int MAG;
	int STR;
	int DEX;
	race_t RACE;
	potion_t POTIONEFFECTS;
	condition_t STATUS;
	magetype_t MAGESTATUS;
	weapon_t WEAPON;
	int LEVEL;
	int WEAPONLEVEL;
	enchantments_t ENCHANTMENTS;
	status_t TYPE;
	int WEAPON_INT;
	int ENCHANT_INT;

static void Player::SET_DEFAULTS(Player who){
	who.STR = rand()%7+1;
	who.DEX = rand()%7+1;
	who.MAG = rand()%7+1;
	who.LEVEL = rand()%7+1;
	who.WEAPONLEVEL = rand()%5+1;
	who.STATUS = HEALTHY;
	if(who.STR<4) who.STR++;
	if(who.MAG<4) who.MAG++;
	if(who.DEX<4) who.DEX++;
	if(who.LEVEL<4) who.LEVEL++;
	if(who.WEAPONLEVEL<4) who.WEAPONLEVEL++;
};
static void Player::SET_ELF(Player& who){
	if(who.STR < MAX_STATUS&&who.STR >= 2)who.STR--;
	if(who.DEX + 2 < MAX_STATUS)who.DEX+=2;
	if(who.MAG + 1 < MAX_STATUS)who.MAG++;
	if(who.LEVEL < MAX_STATUS)who.LEVEL--;
	if(who.WEAPONLEVEL < MAX_STATUS)who.WEAPONLEVEL--;
	who.WEAPON_INT = rand()%4+1;
	if (who.WEAPON_INT == 1) who.WEAPON = LONG_SWORD;
	else if (who.WEAPON_INT == 2 || 3) who.WEAPON = BOW;
	else if (who.WEAPON_INT == 4) who.WEAPON = STAFF;
};
static void Player::SET_DWARF(Player &who){
	if(who.STR < MAX_STATUS)who.STR++;
	if(who.DEX < MAX_STATUS)who.DEX--;
	if(who.MAG < MAX_STATUS)who.MAG-=2;
	if(who.WEAPONLEVEL < MAX_STATUS)who.WEAPONLEVEL+= 2;
};
static void Player::SET_HALFLING(Player &who){
	if(who.STR < MAX_STATUS)who.STR--;
	if(who.DEX < MAX_STATUS)who.DEX+=2;
	if(who.MAG < MAX_STATUS)who.MAG++;
	if(who.LEVEL < MAX_STATUS)who.LEVEL--;
	if(who.WEAPONLEVEL < MAX_STATUS)who.WEAPONLEVEL--;
	
};
static void Player::SET_HUMAN(Player& who){
	
};
static void Player::SET_HALF_ELF(Player& who){
	if(who.STR < MAX_STATUS)who.STR--;
	if(who.DEX < MAX_STATUS)who.DEX++;
};
};
#endif // __MYCLASS_H_INCLUDED__ 

After inputting race, I get this error:

malloc: *** error for object 0x14db4c: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

Please help!
It means exactly what it says. Somewhere along the line you attempted to "delete" or "free" a non-pointer. For instance, on line 16 in your main function, you attempt to "delete" a pointer to an array even though objsetup only points to one object. A simple delete would do.

On a side note, try to modify your code design to use loops such as while, do/while, or for instead of using goto's.
Topic archived. No new replies allowed.