I have typed this code from a book after learning about classes/objects in c++.
It runs until the user enters name for player 1, but then the error
"Conquest game classes objects(491) malloc: *** error for object 0x100007280: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug"
I dont see any pointers used in this code, nor any memory allocated with "new"(which are both covered in the next chapter of the book).
Is there a problem with the code from the book, or have I made a silly mistake. I have tried going over the code looking for typing errors, but can't see any, also I tried to solve the malloc / pointer not freed problem with google searches but couldn't see how this code used them.
Code:
#include <iostream>
#include <string>
using namespace std;
//handles each nation for each player
class Nation{
public:
int land;
int troops;
private:
string name;
int food;
int gold;
int people;
int farmers;
int merchants;
int blacksmiths;
public:
Nation (string lName);
Nation();
~Nation();
bool takeTurn();
private:
void menu(void);
};
Nation nation1;
Nation nation2;
//sets the default nation values
Nation::Nation(string lName):
name(lName),land(20), food(50), troops(15), gold(100), people(100),
farmers(0), merchants(0), blacksmiths(0){}
//a default constructor
Nation::Nation(){}
//takes a turn for player
bool Nation::takeTurn(){
cout<<"It's now" <<name<< "'s turn.\n";
people += land * 0.2;
food += farmers - people * 0.25;
gold += merchants * 20;
troops += blacksmiths;
menu();
if (nation1.land <= 0 || nation2.land<=0) {
return false;
}//if
return true;
}//bool nation
void Nation::menu(){
while (true) {
int input = 0;
cout << "Food " << food<< "\nGold"<<gold
<<"\nland "<<land<<"\nMerchants "<< merchants<<"\nTroops "
<<troops<<"\nUnemployed "<< people<<"\n";
cout<<"1) Buy Land.\n"
<<"2) Hire Farmers\n"
<<"3) Hire merchants\n"
<<"4) Hire Weaponsmiths\n"
<<"5) Attack!\n"
<<"6)Take Turn\n";
cin>>input;
switch (input) {
case 1://buys land
cout << "You buy "<<gold/20<<" sections of land\n";
land += gold/20;
gold %=20;
cout<< "You now have "<<gold<< "gold\n";
break;
case 2:
farmers += people;
cout<<"you hired " << people<< " farmers\n";
people = 0;
break;
case 3:
merchants += people;
cout<< "You hired " << people<< "merchants\n";
people = 0;
break;
case 4:
blacksmiths += people;
cout<< "You hired " << people<< "blacksmiths\n";
people = 0;
break;
case 5:
cout<< "the war wages into the night and all die\n";
if (nation1.troops < nation2.troops) {
nation2.land += 10;
nation1.land -= 10;
}//if
else if (nation1.troops > nation2.troops){
nation2.land -= 10;
nation1.land += 10;
}//else if
nation1.troops = 0;
nation2.troops = 0;
break;
case 6:
return; //ends the turn
break;
}//while
}//switch
}//menu
int main(){
string tempString;
cout<<"Welcome to the conquest\n";
cout<<"What is your name player 1?\n";
cin>>tempString;
nation1 = Nation(tempString);
cout<<"What is your name player 2?\n";
cin>>tempString;
nation2 = Nation(tempString);
while (nation1.takeTurn() && nation2.takeTurn()) {
}
return 0;
}//main