I will try my best to help at one time. My line numbers may be slightly different from yours, so feel free to look around a bit.
IN:
115 user.setRegen(void);
setRegen declared as a float :
20 class character 21 { 22 public: . . . 34 void setRegen(float reg);
So you cannot pass void . By the way, in C ++ itโs usually just not to skip anything when calling a function that takes no parameters and does not pass an explicit void . However, explicit void is fine.
The getHeal() function calculates a random amount to heal a character, but it does not actually increase the value of the health variable. You can implement healing this way, see Line 92:
87 float character::getHeal() 88 { 89 //this is what happens when you chose to heal 90 regen = rand() % 20 + 3; 91 cout << "regen value= " << regen<< ".\n"; 92 health += regen; 93 return regen; 94 } Z
You also do not reduce the enemyโs health when attacking. One way to do this is to pass the link to the adversary on getAttack() and change it there:
58 float character::getAttack(character& opponent) 59 { 60 //defines the magnitude/power of attack 61 //function shows how much damage is inflicted 62 63 64 // ouch is how much damage is done 65 roll = rand() % 20 + 1; // range between 1 &20 66 67 if (roll <= 11) 68 { 69 ouch = str - (def /2); 70 } 71 72 else if ((roll <= 17) && (roll >= 12)) 73 { 74 ouch = (str * 2) - (def / 2); 75 } 76 77 else if ((roll <= 20) && (roll >= 18)) 78 { 79 ouch = (str * 3) - (def / 2); 80 //cout << "CRITICAL HIT!!"; 81 } 82 83 opponent.health -= ouch; 84 85 return ouch; 86 87 }
You will also need to change the declaration (prototype) for getAttack() :
20 class character 21 { 22 public: . . . 32 float getAttack(character& opponent);
... and how it is called in main() :
152 case 2 : 153 154 attackDamage = user.getAttack(computer); 155 156 cout << "" <<user.name <<" did " << attackDamage << " damage to the opponent!\n"; 157 158 break;
I also noticed that the program does not loop at all. He simply takes one action, performs it, and completes. A game may be more interesting if it goes in cycles until one of the players is dead.
Last, when you use random numbers, you call srand exactly one, usually at the beginning of a program run. You call it every time character is created.
Here is a shameless plugin for one of my previous answers about using rand .
I have made several modifications for you. Here is a link to ideone with the same code as below:
// Test for hard stuff.cpp : Defines the entry point for the console application. // // Bigger proj // Constructors will make characters with rolling statistics //#include "stdafx.h" #include <iostream> #include <string> #include <stdio.h> #include <stdlib.h> #include <time.h> using namespace std; // declaring function for hit power //int power( int str, int def); int command; class character { public: character(); //~character(); string name; float str; float def; float health; // hit points float regen; // health regen amount float roll; // for random value float ouch; // amount of attack damage float getAttack(character& opponent); float getHeal(void); void setRegen(float reg); bool IsAlive() const; //void setHeal(float healAmt); private: }; character::character() { str = rand() % 30 + 5; def = rand() % 30 + 5; health = 100; //Output to check the constructor is running properly cout<< "Character has been created.\n"; } bool character::IsAlive() const { return health > 0.0f; } void character::setRegen( float reg ) { regen = reg; } float character::getAttack(character& opponent) { //defines the magnitude/power of attack //function shows how much damage is inflicted // ouch is how much damage is done roll = rand() % 20 + 1; // range between 1 &20 if (roll <= 11) { ouch = str - (def /2); } else if ((roll <= 17) && (roll >= 12)) { ouch = (str * 2) - (def / 2); } else if ((roll <= 20) && (roll >= 18)) { ouch = (str * 3) - (def / 2); //cout << "CRITICAL HIT!!"; } opponent.health -= ouch; return ouch; } float character::getHeal() { //this is what happens when you chose to heal regen = rand() % 20 + 3; cout << "regen value= " << regen<< ".\n"; health += regen; return regen; } /*character::~character() { str = 0; def = 0; health = 0; // Output to check the destructor is running properly cout << "Character has been destroyed\n"; } */ int main() { srand(time_t(NULL)); //Class objects character user, computer; //Hard code in a name for the computer player computer.name = "ZOID\n"; float attackDamage; float healthAdded; user.setRegen(42.0); //Recieve data for the user player cout<< "Please enter a name for your character:\n"; cin>> user.name; //Output name and stats to the user cout<< "\nYour name is: " << user.name << endl; cout << "here are your statistics: \n" << "strength: " << user.str << endl << "Defense: " << user.def << endl << "Health: " << user.health << endl; cout<< "oh no an oppenent appeared!!!\n"; cout<< "you will have to fight him!" << endl<< endl; cout << "opponent health: 100" << endl; while (user.IsAlive() && computer.IsAlive()) { cout << "Str: " << user.str << "\t" << "Def: " << user.def << "\t" << "Health: " << user.health << "\t" << "\n"; cout << "what would you like to do: heal (1), attack(2), or run(3).\n"; cin>> command; switch(command) { case 1 : healthAdded = user.getHeal(); cout<< ""<<user.name <<" has regenerated " << healthAdded << " health.\n"; break; case 2 : attackDamage = user.getAttack(computer); cout << "" <<user.name <<" did " << attackDamage << " damage to the opponent!\n"; break; case 3: cout<< ""<<user.name<<" got away!\n"; break; default: cout<< "Please enter a valid choice!"; } //end switch } return 0; }