assissstance need for referencing

i am trying to write a simple turn based rpg game in void attack of course this is where they attack each other, in the code i set characters attack and subtract that from the targets health but when the code runs it adds the total can someone give me some advice about this

//header file
#pragma once
#include<iostream>
#include<stdio.h>
#include <string>
using namespace std;

class characters
{
string name;
float health;
float strength;
float speed;

public:
characters(string n,float h, float st, float sp)
{
name = n;
health = h;
strength = st;
speed = sp;
}
void print()
{
cout<<name<<endl;
cout<<"health"<<" "<<health<<endl;
cout<<"power"<<" "<<strength<<endl;
cout<<"speed"<<" "<<speed<<endl;
cout<<endl;
}
void attack(characters target) //problem is here
{

cout<<name<<"'s attack did "<<characters::strength<<" damage to "<<target.name<<endl;
characters::strength-=target.health;
cout<<target.name<<" health is now "<<target.health - characters::strength<<endl;
return target.health - characters::strength ;
}

};
class fighter1:public characters
{
public:
fighter1(string n,float h,float st,float sp):characters(n, h, st, sp)
{
}
};
class fighter2:public characters
{
public:
fighter2(string n,float h,float st, float sp):characters(n, h, st, sp)
{
}
};



//cpp file

#include "hdr.h"
#include <iostream>
#include <stdio.h>
using namespace std;
void main()
{
fighter1 player1("neji",10.f,5.f,4.f);
fighter2 player2("nile",10.f,5.f,5.f);
player1.print();
player2.print();
player1.attack(player2);
player2.attack(player1);

}
1
2
3
4
5
6
7
8
void attack(characters target) //problem is here
{

cout<<name<<"'s attack did "<<characters::strength<<" damage to "<<target.name<<endl;
characters::strength-=target.health;
cout<<target.name<<" health is now "<<target.health - characters::strength<<endl;
return target.health - characters::strength ;
}


You declare attack as having no return value, but then you try to return a value?

Also, the minus operator alone does not affect anything. I think you want something like this for line 7 above:
target.health -= characters::strength;
Last edited on
that work thanl you
Topic archived. No new replies allowed.