I have a problem in which I have to return the name of a winner in a fight between two fighters.
The class for fighter is as follows:
class Fighter
{
private:
std::string name;
int health;
int damagePerAttack;
public:
Fighter(std::string name, int health, int damagePerAttack)
{
this->name = name;
this->health = health;
this->damagePerAttack = damagePerAttack;
}
~Fighter() { };
std::string getName()
{
return name;
}
int getHealth()
{
return health;
}
int getDamagePerAttack()
{
return damagePerAttack;
}
void setHealth(int value)
{
health = value;
}
};
I wrote a function that should return the name of the winner.
std::string declareWinner(Fighter* fighter1, Fighter* fighter2,
std::string firstAttacker)
{
// Your code goes here. Have fun!
if(firstAttacker==fighter1->getName())
{
while(fighter1->getHealth()!=0&&fighter2->getHealth()!=0)
{
fighter2->setHealth(fighter2->getHealth()-fighter1->getDamagePerAttack());
if(fighter2->getHealth()<=0)
{
return fighter1->getName();
}
fighter1->setHealth(fighter1->getHealth()-fighter2->getDamagePerAttack());
if(fighter1->getHealth()<=0)
{
return fighter2->getName();
}
}
}
else if(firstAttacker==fighter2->getName())
{
while(fighter1->getHealth()!=0&&fighter2->getHealth()!=0)
{
fighter1->setHealth(fighter1->getHealth()-fighter2->getDamagePerAttack());
if(fighter1->getHealth()<=0)
{
return fighter2->getName();
}
fighter2->setHealth(fighter2->getHealth()-fighter1->getDamagePerAttack());
if(fighter2->getHealth()<=0)
{
return fighter1->getName();
}
}
}
}
This satisfies all my needs, but it throws SIGILL signal, and i do not know what i did wrong. How should I deal with it?
In some conditions, it is possible that function runs to the end and exits without returning a value, that corrupts stack and can lead to SIGILL. As a safe measure you can, for example, add return statement to the end of function.
std::string declareWinner(Fighter* fighter1, Fighter* fighter2,
std::string firstAttacker)
{
// Your code goes here. Have fun!
if(firstAttacker==fighter1->getName())
{
while(fighter1->getHealth()!=0&&fighter2->getHealth()!=0)
{
fighter2->setHealth(fighter2->getHealth()-fighter1->getDamagePerAttack());
if(fighter2->getHealth()<=0)
{
return fighter1->getName();
}
fighter1->setHealth(fighter1->getHealth()-fighter2->getDamagePerAttack());
if(fighter1->getHealth()<=0)
{
return fighter2->getName();
}
}
}
else if(firstAttacker==fighter2->getName())
{
while(fighter1->getHealth()!=0&&fighter2->getHealth()!=0)
{
fighter1->setHealth(fighter1->getHealth()-fighter2->getDamagePerAttack());
if(fighter1->getHealth()<=0)
{
return fighter2->getName();
}
fighter2->setHealth(fighter2->getHealth()-fighter1->getDamagePerAttack());
if(fighter2->getHealth()<=0)
{
return fighter1->getName();
}
}
}
return "No winner"; <= Add before exiting function
}
I also noticed there's a redundancy and possible logical errors in code.
I would rewrite it like this (without changing function signature):
std::string declareWinner(Fighter* fighter1, Fighter* fighter2,
std::string firstAttacker)
{
Fighter *first;
Fighter *second;
if(firstAttacker == fighter1->getName()) {
first = fighter2;
second = fighter1;
} else if (firstAttacker == fighter2->getName()) {
first = fighter1;
second = fighter2;
} else {
// Bad call parameters
return "Bad call"; // Throw exception maybe?
}
// Simulating fighting
do {
std::swap(second,first);
second->setHealth(second->getHealth() - first->getDamagePerAttack());
} while (second->getHealth() > 0);
return first->getName();
}
Related
I have 3 classes in my program that interact with each other and contain each other's instances:
class Inventory
{
public:
// Increment Data Members
void incrementHerbs() { herbs++; }
void incrementHealth() { health++; }
void incrementGold() { gold++; }
// Getters
int getHerbs() { return herbs; }
int getHealth() { return health; }
int getGold() { return gold; }
private:
int herbs = 0;
int health = 3;
int gold = 0;
};
class Player
{
public:
void setRow(int row) { this->rowCoordinate = row; }
void setCol(int col) { this->colCoordinate = col; }
int getRow() { return rowCoordinate; }
int getCol() { return colCoordinate; }
Inventory getBag() { return Bag; }
private:
int rowCoordinate;
int colCoordinate;
Inventory Bag;
};
class Board
{
public:
int getNumRows() { return numRows; }
int getNumCols() { return numCols; }
Player getPlayer() { return User; }
private:
int numRows;
int numCols;
char** maze;
Player User;
};
I am only instantiating a Board object in the main function. At a point in my program, I want to be able to increment the herb count in the inventory class through that object.
I have tried doing:
Board board;
board.getPlayer().getBag().incrementHerbs();
This call compiles without any errors but when I print out the herb count afterwards, the herb count is still the same.
It did not increment. What can be going wrong and what can I do?
What can be going wrong and what can I do?
In your Player class, your getBag() function returns a copy of Inventory (i.e. member Bag).
Inventory getBag() { return Bag; }
//^^^^^^----> is copy!
You need to return the reference in order to modify it
Inventory& getBag() { return Bag; }
//^^^^^^^^
The same issue with the Board's function getPlayer()
Player getPlayer() { return User; }
//^^^^----> is copy!
you need
Player& getPlayer() { return User; }
//^^^^^^
And here's a demo.
I'm trying to create a Monopoly game in C++ and I've been messing with object-oriented-programming, the problem happens with the classes "Game" and "Player", I would like to know how to use "Game"'s functions inside "Player" and "Player"'s functions inside "Game", but I've been getting a compiler error saying that the class is not defined.
Switching class positions won't work (obviously) but I tried anyways.
Code (reduced and minimized to the Game and Player classes):
namespace Monopoly {
typedef enum { normal, train, company, incometax, luxurytax, start, chancecard, chestcard, jail } type;
class Game {
private:
bool running = false;
int turn = 1;
int currentPlayerID;
int startingMoney = 1000;
std::vector<Player> players;
public:
// Functions
void createPlayer() {
++currentPlayerID;
Player newPlayer(currentPlayerID, startingMoney);
players.push_back(newPlayer);
++currentPlayerID;
}
void createPlayers(int playerAmount) {
for (int i = 0; i <= playerAmount; ++i) {
createPlayer();
}
}
Player getPlayer(int index) {
Player p = players[index];
return p;
}
};
class Player {
private:
int playerID;
int money;
std::vector<int> propertiesOwned;
void addProperty(int id) {
this->propertiesOwned.push_back(id);
}
public:
// Constructor
Player(int pID, int sMoney) {
this->playerID = pID;
this->money = sMoney;
}
// Functions
Player payMoney(int payAmount, unsigned int destinationID, Game engine) {
this->money -= payAmount;
if (destinationID > 0) {
// Checks if you're paying to a player or bank
bool playerFound = false;
for (int i = 0; i <= engine.getPlayerAmount(); ++i) {
if (engine.getPlayer(i).getID() == destinationID) {
playerFound = true;
break;
}
}
if (playerFound) {
// Player was found
engine.getPlayer(destinationID).giveMoney(payAmount);
return;
}
else {
std::cout << "\nERROR: Invalid player ID at function payMoney\n";
return;
}
}
else {
// You're paying to the bank
}
return;
}
void buyProperty(int id, int price, Game engine) {
payMoney(price, 0, engine);
addProperty(id);
}
void giveMoney(int payMoney) {
this->money += payMoney;
}
// Returns
inline int getMoney() { return this->money; }
inline int getID() { return this->playerID; }
inline auto getProperties(int index) {
auto p = propertiesOwned[index];
return p;
}
inline int getPropertyAmount() {
int amount = std::size(propertiesOwned);
return amount;
}
};
}
I expected the classes to run the other classes function normally, but it seens like that in C++, classes are defined in certain order, and you can only access classes (in a class) declared before the class you're using, feedback and alternatives that fix this would help
You are correct that in C++ declaration order matters, and that is the cause of your errors, however there are a few other issues with the code.
Firstly, you should swap the order that Game and Player are defined. This will make it easier, as Player relies on Game fewer times than Game relies on Player.
Next, add a forward declaration for Game before the definition of Player:
class Game;
This tells the compiler that a class named Game exists and allows you to use it in scenarios where it doesn't need to know the contents (i.e. definition) of the class.
Next, make payMoney and buyProperty accept their engine parameter by reference instead of by value by changing the parameter specifier to Game &engine. This is important for two reasons. First, passing by value can only be done if you have already defined the type, which we have not (we've only declared it). Second, passing by value creates a copy of the object, which in this case means a completely new vector of completely new Player objects, and the changes will not synchronize back to the old object. See here for a better explanation of references.
Next, you need to extract the definition of payMoney to after the definition of Game. The reason is that while the parameter list of payMoney no longer relies on the definition of Game, the code in the function body does (because it calls functions on the engine object). See the end for what this looks like.
This fixes all the problems with declaration/definition order. You also should make payMoney return void as its return value is never provided and never used, pick a consistent type for IDs (either int or unsigned int, not a mix), and add the getPlayerAmount to Game.
Here's what the final code could look like:
namespace Monopoly {
typedef enum { normal, train, company, incometax, luxurytax, start, chancecard, chestcard, jail } type;
class Game;
class Player {
private:
int playerID;
int money;
std::vector<int> propertiesOwned;
void addProperty(int id) {
this->propertiesOwned.push_back(id);
}
public:
// Constructor
Player(int pID, int sMoney) {
this->playerID = pID;
this->money = sMoney;
}
// Functions
void payMoney(int payAmount, int destinationID, Game &engine);
void buyProperty(int id, int price, Game &engine) {
payMoney(price, 0, engine);
addProperty(id);
}
void giveMoney(int payMoney) {
this->money += payMoney;
}
// Returns
inline int getMoney() { return this->money; }
inline int getID() { return this->playerID; }
inline auto getProperties(int index) {
auto p = propertiesOwned[index];
return p;
}
inline int getPropertyAmount() {
int amount = std::size(propertiesOwned);
return amount;
}
};
class Game {
private:
bool running = false;
int turn = 1;
int currentPlayerID;
int startingMoney = 1000;
std::vector<Player> players;
public:
// Functions
void createPlayer() {
++currentPlayerID;
Player newPlayer(currentPlayerID, startingMoney);
players.push_back(newPlayer);
++currentPlayerID;
}
void createPlayers(int playerAmount) {
for (int i = 0; i <= playerAmount; ++i) {
createPlayer();
}
}
Player getPlayer(int index) {
Player p = players[index];
return p;
}
int getPlayerAmount() {
int amount = players.size();
return amount;
}
};
void Player::payMoney(int payAmount, int destinationID, Game &engine) {
this->money -= payAmount;
if (destinationID > 0) {
// Checks if you're paying to a player or bank
bool playerFound = false;
for (int i = 0; i <= engine.getPlayerAmount(); ++i) {
if (engine.getPlayer(i).getID() == destinationID) {
playerFound = true;
break;
}
}
if (playerFound) {
// Player was found
engine.getPlayer(destinationID).giveMoney(payAmount);
return;
}
else {
std::cout << "\nERROR: Invalid player ID at function payMoney\n";
return;
}
}
else {
// You're paying to the bank
}
return;
}
}
Side note: it's technically better C++ to use size_t instead of int for variables storing the size of vectors, as that is what the size functions return (and it's an unsigned integer type whereas int is signed), but that's not especially important.
I have a small problem with inheritance in my code - it suddenly stops working when I add identical code in another "if" statement in my loop.
Here is the code I use for "main.cpp":
#include "bibl.h"
using namespace std;
int main()
{
int o;
Vec2 test;
while(1)
{
cout<<"1. Add item and show."<<endl<<"2. Show."<<endl;
cin>>o;
if(o==1)
{
InhItem a("Test",100);
test.addItem(&a);
cout<<"This show works:"<<endl;
test.getVec1(0)->getItem(0)->Show();//This code works.
}
else if(o==2)
{
cout<<"This show doesn't work:"<<endl;
test.getVec1(0)->getItem(0)->Show();//This doesn't.
}
}
system("pause");
return 0;
}
And the code for "bibl.h":
#ifndef TEST1
#define TEST1
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Item
{
protected:
string im;
string theme;
public:
Item(string im=" ", string theme=" "):im(im),theme(theme)
{
}
~Item()
{
}
string getTheme()
{
return theme;
}
virtual void Show()
{
}
};
class InhItem:public Item
{
int tst;
public:
InhItem(string im=" ", int tst=0):Item(im),tst(tst)
{
}
~InhItem()
{
}
void Show()
{
cout<<tst<<endl;
}
};
class Vec1
{
vector<Item*> Vec1a;
string theme;
public:
Vec1(string theme=" "):theme(theme)
{
}
~Vec1()
{
}
void addToVec1a(Item *item)
{
Vec1a.push_back(item);
}
string getTheme()
{
return theme;
}
Item *getItem(int p)
{
return Vec1a[p];
}
};
class Vec2
{
vector<Vec1*> Vec2a;
public:
Vec2()
{
}
~Vec2()
{
}
void addToVec2(Vec1 *vec1)
{
Vec2a.push_back(vec1);
}
void addItem(Item *item)
{
for(int i=0;i<=Vec2a.size();i++)
{
if(i==Vec2a.size())
{
addToVec2(new Vec1(item->getTheme()));
Vec2a[i]->addToVec1a(item);
break;
}
else if(Vec2a[i]->getTheme().compare(item->getTheme())==0)
{
Vec2a[i]->addToVec1a(item);
break;
}
}
}
Vec1 *getVec1(int r)
{
return Vec2a[r];
}
};
#endif
When I try to use the 2nd "if" after adding the item with 1st, it doesn't show - in 1st "if" test.getVec1(0)->getItem(0)->Show(); works, but in another it doesn't.
What is the cause of this problem and how can I fix it?
This is the code
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
class FIXML {
private: Order Order_object = new Order();
public:
Order getOrder_object()
{
return Order_object;
}
void setOrder_object(Order Order_object)
{
this->Order_object = Order_object;
}
};
class Order {
public:
string ClOrdID = "123456";
string Side = "2";
string TransactTm = "2001-09-11T09:30:47-05:00";
string OrdTyp = "2";
string Px = "93.25";
string Acct = "26522154";
Hdr Hdr_object = Hdr();
Instrmt Instrmt_object = Instrmt();
OrdQty OrdQty_object = OrdQty();
public:
string getClOrdID()
{
return ClOrdID;
}
string getSide()
{
return Side;
}
string getTransactTm()
{
return TransactTm;
}
string getOrdTyp()
{
return OrdTyp;
}
string getPx()
{
return Px;
}
string getAcct()
{
return Acct;
}
Hdr getHdr_object()
{
return Hdr_object;
}
Instrmt getInstrmt_object()
{
return Instrmt_object;
}
OrdQty getOrdQty_object()
{
return OrdQty_object;
}
void setClOrdID(string ClOrdID)
{
this->ClOrdID = ClOrdID;
}
void setSide(string Side)
{
this->Side = Side;
}
void setTransactTm(string TransactTm)
{
this->TransactTm = TransactTm;
}
void setOrdTyp(string OrdTyp)
{
this->OrdTyp = OrdTyp;
}
void setPx(string Px)
{
this->Px = Px;
}
void setAcct(string Acct)
{
this->Acct = Acct;
}
void setHdr_object(Hdr Hdr_object)
{
this->Hdr_object = Hdr_object;
}
void setInstrmt_object(Instrmt Instrmt_object)
{
this->Instrmt_object = Instrmt_object;
}
void setOrdQty_object(OrdQty OrdQty_object)
{
this->OrdQty_object = OrdQty_object;
}
};
class Hdr {
private:
string Snt = "2001-09-11T09:30:47-05:00";
string PosDup = "N";
string PosRsnd = "N";
string SeqNum = "521";
Sndr Sndr_object = Sndr();
Tgt Tgt_object = Tgt();
public:
string getSnt()
{
return Snt;
}
string getPosDup()
{
return PosDup;
}
string getPosRsnd()
{
return PosRsnd;
}
string getSeqNum()
{
return SeqNum;
}
Sndr getSndr_object()
{
return Sndr_object;
}
Tgt getTgt_object()
{
return Tgt_object;
}
void setSnt(string Snt)
{
this->Snt = Snt;
}
void setPosDup(string PosDup)
{
this->PosDup = PosDup;
}
void setPosRsnd(string PosRsnd)
{
this->PosRsnd = PosRsnd;
}
void setSeqNum(string SeqNum)
{
this->SeqNum = SeqNum;
}
void setSndr_object(Sndr Sndr_object)
{
this->Sndr_object = Sndr_object;
}
void setTgt_object(Tgt Tgt_object)
{
this->Tgt_object = Tgt_object;
}
};
class Sndr {
private:
string ID = "AFUNDMGR";
public:
string getID()
{
return ID;
}
void setID(string ID)
{
this->ID = ID;
}
};
class Tgt {
private:
string ID = "ABROKER";
public:
string getID()
{
return ID;
}
void setID(string ID)
{
this->ID = ID;
}
};
class Instrmt {
private:
string Sym = "IBM";
string ID = "459200101";
string IDSrc = "1";
public:
string getSym()
{
return Sym;
}
string getID()
{
return ID;
}
string getIDSrc()
{
return IDSrc;
}
void setSym(string Sym)
{
this->Sym = Sym;
}
void setID(string ID)
{
this->ID = ID;
}
void setIDSrc(string IDSrc)
{
this->IDSrc = IDSrc;
}
};
class OrdQty {
private:
string Qty = "1000";
public:
string getQty()
{
return Qty;
}
void setQty(string Qty)
{
this->Qty = Qty;
}
};
return 0;
}
All the classes I've declared, whether it's Order, Tgt, Sndr. Whenever I make a new instance of these classes, I get the error "Error: identifier classname is undefined"
Thanks in advance
Try declaring them (a) before you use them, and (b) outside of any function:
#include <iostream>
class Test
{
public:
Test() { std::cout << "Test!" << std::endl; }
};
int main()
{
Test t;
}
Once you finish reordering them based on which classes are used by which, it may end up like this:
class OrdQty {
// ...
};
class Instrmt {
// ...
};
class Sndr {
// ...
};
class Tgt {
// ...
};
class Hdr {
// ...
};
class Order {
// ...
};
class FIXML {
// ...
};
int main()
{
return 0;
}
Once you finish that, you'll find that this line is incorrect:
private: Order Order_object = new Order();
You can't initialize a member of a class like this. You'll need to do this in the constructor, copy constructor, and assignment operator, and then clean it up in the destructor.
new to the try/catch thing and need to use it for a program. For some reason though my program crashes when it catches the obj. The sample program I wrote just reads in a number and then tests to see if its below 10 or above 20. The point is to use two user defined classes to throw and catch. The above 20 obj gets thrown and catches just fine. But the below 10, if its thrown will crash the program. Why is this? here is my code
#include <iostream>
#include <cstdlib>
using namespace std;
class above20
{
public:
above20()
{
msg = "Number is above 20.";
};
string getmsg()
{
cout<<msg<<endl;
};
private:
string msg;
};
class below10
{
public:
below10()
{
msg = "Number is below 10";
};
string getmsg()
{
cout<<msg<<endl;
};
private:
string msg;
};
int main ()
{
int num;
try
{
cout<<"Enter Number between 10 and 20:"<<endl;
cin >> num;
if (num > 20)
{
throw above20 ();
}
}
catch (above20 obj)
{
obj.getmsg();
}
try
{
if (num < 10)
{
throw below10 ();
}
}
catch (below10 obj)
{
obj.getmsg();
}
system ("pause");
return (0);
}
Are you sure this compiles? Did you omit something in your copy paste? The getmsg() methods don't return anything.
---edit ---
Try this:
void getmsg()
{
cout<<msg<<endl;
};
You have a lot of poor syntax in your code. The error you get is because you declare getMsg with a return value and do not return (UB - you are lucky it even compiles!).
To fix all of your issues: http://ideone.com/1qGAuR
You have a few errors in your code like having a function that doesn’t return anything despite saying that it should in the signature:
string getmsg()
{
cout<<msg<<endl;
};
should really be:
void getmsg()
{
cout<<msg<<endl;
};
or
string getmsg()
{
return string(msg);
};
Putting aside those bugs for a second, from a design point of view inheriting from one exception base class is cleaner. I would usually inherit from std::runtime_error but you could define your own if you wanted. For example :
#include <iostream>
#include <cstdlib>
using namespace std;
class above_below_exception
{
public:
virtual string getmsg() =0;
};
class above20 : public above_below_exception
{
public:
above20()
{
msg = "Number is above 20.";
};
string getmsg()
{
return string(msg);
};
private:
string msg;
};
class below10 : public above_below_exception
{
public:
below10()
{
msg = "Number is below 10";
};
string getmsg()
{
return string(msg);
};
private:
string msg;
};
int main ()
{
int num;
try
{
cout<<"Enter Number between 10 and 20:"<<endl;
cin >> num;
if (num > 20)
{
throw above20();
}
if (num < 10)
{
throw below10();
}
}
catch (above_below_exception& obj)
{
cout << obj.getmsg();
}
return (0);
}
A potential fix for your code:
#include <iostream>
#include <cstdlib>
using namespace std;
class above20 : public std::exception
{
public:
above20()
{
msg = "Number is above 20.";
}
virtual ~above20 () throw ()
{
}
string getmsg ()
{
cout << msg << endl;
return msg;
}
private:
string msg;
};
class below10 : public std::exception
{
public:
below10()
{
msg = "Number is below 10";
}
virtual ~below10 () throw ()
{
}
string getmsg()
{
cout << msg << endl;
return msg;
}
private:
string msg;
};
int main ()
{
int num;
try
{
cout<<"Enter Number between 10 and 20:"<<endl;
cin >> num;
if (num > 20)
{
throw above20 ();
}
}
catch (above20 &obj)
{
cout << obj. getmsg () << endl;
}
try
{
if (num < 10)
{
throw below10 ();
}
}
catch (below10 obj)
{
obj.getmsg();
}
system ("pause");
return (0);
}