Oct 7, 2012 at 2:24pm UTC
Can somebody help me fix the issue from getting data from the class to be used in the program?
Part 1 of code (whats needed to understand)
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
void pause(int dur);//pauses the program for a set number of seconds
//MY VARIABLES
string PlayerAge;
string PlayerName;
string PlayerGender;
string CharacterDetails;
string OpponentCharacterDetails;
string Continue;
string PlayAgain;
long Earnings = 10;
long Money = 0;
long AmountExp = 10;
long TotalExp = 0;
long NewLevel = 0;
long LevelUp = 10;
long SkillPoints = 3;
long TotalSkillPoints = 0;
int TypeOfCharacter;
class Monster
{
public :
// constructor
Monster(){}
~Monster(){}
//Memeber Methods
void DisplayStats()
{
cout << "\n\n\t--------Monster Stats--------" ;
cout << "\n\tYou sent out a " << Type << " type.\n" ;
cout << "\n\tAttack:" << Attack;
cout << "\n\tDefense:" << Defense;
cout << "\n\tHealth:" << Health;
cout << "\n\tAdvantage:" << Advantage;
cout << "\n\tDisadvantage:" << Health;
cout << "\n\t-------------------------------\n\t" ;
}
//Accessor Methods
string GetType() { return Type; }
void SetType(string x) { Type = x; }
int GetAttack() { return Attack; }
void SetAttack(int x) { Attack = x; }
int GetDefense() { return Defense; }
void SetDefense(int x) { Defense = x; }
int GetHealth() { return Health; }
void SetHealth(int x) { Health = x; }
string GetAdvantage() { return Advantage; }
void SetAdvantage(string x) { Advantage = x; }
string GetDisadvantage() { return Disadvantage; }
void SetDisadvantage(string x) { Disadvantage = x; }
string GetMonsterName() { return MonsterName; }
void SetMonsterName(string x) { MonsterName = x; }
private :
string Type;
int Attack;
int Defense;
int Health;
string Advantage;
string Disadvantage;
string MonsterName;
};
class OpponentMonster
{
public :
// constructor
OpponentMonster(){}
~OpponentMonster(){}
//Memeber Methods
void ODisplayStats()
{
cout << "\n\n\t--------Monster Stats--------" ;
cout << "\n\tYour opponent sent out a " << OType << " type.\n" ;
cout << "\n\tAttack:" << OAttack;
cout << "\n\tDefense:" << ODefense;
cout << "\n\tHealth:" << OHealth;
cout << "\n\tAdvantage:" << OAdvantage;
cout << "\n\tDisadvantage:" << OHealth;
cout << "\n\t-------------------------------\n\t" ;
}
//Accessor Methods
string OGetType() { return OType; }
void OSetType(string x) { OType = x; }
int OGetAttack() { return OAttack; }
void OSetAttack(int x) { OAttack = x; }
int OGetDefense() { return ODefense; }
void OSetDefense(int x) { ODefense = x; }
int OGetHealth() { return OHealth; }
void OSetHealth(int x) { OHealth = x; }
string OGetAdvantage() { return OAdvantage; }
void OSetAdvantage(string x) { OAdvantage = x; }
string OGetDisadvantage() { return ODisadvantage; }
void OSetDisadvantage(string x) { ODisadvantage = x; }
string OGetMonsterName() { return OMonsterName; }
void OSetMonsterName(string x) { OMonsterName = x; }
private :
string OType;
int OAttack;
int ODefense;
int OHealth;
string OAdvantage;
string ODisadvantage;
string OMonsterName;
};
void YourFireType()
{
Monster * YourFireType = new Monster;
YourFireType->SetType("fire" );
YourFireType->SetAttack(8);
YourFireType->SetDefense(1);
YourFireType->SetHealth(40);
YourFireType->SetAdvantage("Nature" );
YourFireType->SetDisadvantage("Water" );
YourFireType->SetMonsterName("YourFireType" );
YourFireType->DisplayStats();
system("PAUSE" );
}
void YourWaterType()
{
Monster * YourWaterType = new Monster;
YourWaterType->SetType("water" );
YourWaterType->SetAttack(8);
YourWaterType->SetDefense(1);
YourWaterType->SetHealth(40);
YourWaterType->SetAdvantage("Fire" );
YourWaterType->SetDisadvantage("Nature" );
YourWaterType->SetMonsterName("YourWaterType" );
YourWaterType->DisplayStats();
system("PAUSE" );
}
void YourNatureType()
{
Monster * YourNatureType = new Monster;
YourNatureType->SetType("nature" );
YourNatureType->SetAttack(8);
YourNatureType->SetDefense(1);
YourNatureType->SetHealth(40);
YourNatureType->SetAdvantage("Water" );
YourNatureType->SetDisadvantage("Fire" );
YourNatureType->SetMonsterName("YourNatureType" );
YourNatureType->DisplayStats();
system("PAUSE" );
}
void OpponentFireType()
{
OpponentMonster * OpponentFireType = new OpponentMonster;
OpponentFireType->OSetType("fire" );
OpponentFireType->OSetAttack(8);
OpponentFireType->OSetDefense(1);
OpponentFireType->OSetHealth(40);
OpponentFireType->OSetAdvantage("Nature" );
OpponentFireType->OSetDisadvantage("Water" );
OpponentFireType->OSetMonsterName("OpponentFireType" );
OpponentFireType->ODisplayStats();
system("PAUSE" );
}
void OpponentWaterType()
{
OpponentMonster * OpponentWaterType = new OpponentMonster;
OpponentWaterType->OSetType("water" );
OpponentWaterType->OSetAttack(8);
OpponentWaterType->OSetDefense(1);
OpponentWaterType->OSetHealth(40);
OpponentWaterType->OSetAdvantage("Fire" );
OpponentWaterType->OSetDisadvantage("Nature" );
OpponentWaterType->OSetMonsterName("OpponentWaterType" );
OpponentWaterType->ODisplayStats();
system("PAUSE" );
}
void OpponentNatureType()
{
OpponentMonster * OpponentNatureType = new OpponentMonster;
OpponentNatureType->OSetType("nature" );
OpponentNatureType->OSetAttack(8);
OpponentNatureType->OSetDefense(1);
OpponentNatureType->OSetHealth(40);
OpponentNatureType->OSetAdvantage("Water" );
OpponentNatureType->OSetDisadvantage("Fire" );
OpponentNatureType->OSetMonsterName("OpponentNatureType" );
OpponentNatureType->ODisplayStats();
system("PAUSE" );
}
void YourType()
{
cout << "What character do you want?\n\n" ;
cout << "There is a fire type who specializes with fire attacks. (fire)\n" ;
cout << "There is a water type who specializes with water attacks. (water)\n" ;
cout << "There is a nature type who specializes with nature attacks. (nature)\n\n" ;
cout << "Please enter the type of the monster you would like:" ;
cin >> CharacterDetails;
system("CLS" );
if (CharacterDetails == "fire" )
{
YourFireType();
}
else if (CharacterDetails == "water" )
{
YourWaterType();
}
else {
YourNatureType();
}
pause(10);
system("CLS" );
}
void OponentType()
{
/* rand example: guess the number */
#include <stdio.h>
/* initialize random seed: */
srand ( time(NULL) );
/* generate secret number: */
TypeOfCharacter = rand() % 3 + 1;
if (TypeOfCharacter == 1)
{
OpponentFireType();
}
else if (TypeOfCharacter == 2)
{
OpponentWaterType();
}
else
{
OpponentNatureType();
}
}
Last edited on Oct 7, 2012 at 2:27pm UTC
Oct 7, 2012 at 2:28pm UTC
Call your 'get' functions in the main function.
1 2
Monster myMonster;
int health = myMonster.OGetHealth(); // returns OHealth
Last edited on Oct 7, 2012 at 2:29pm UTC
Oct 7, 2012 at 2:41pm UTC
i am confused at what you are getting at, can you help modify the code please, and the parts of the code you change, color-code it red
Oct 7, 2012 at 3:00pm UTC
My problem is that the data from the "Class Monster, called in void Your
YourFireType, YourWaterType, YourNatureType" and "Class OpponentMonster, called in OpponentFireType, OpponentWaterType, OpponentNatureType", is undefined in the rest of the program, for example
1 2 3 4 5 6 7 8
int TypeAdvantages()
{
//Fire vs Fire
if (SetType == "fire" && OSetType == "Fire" )
{
Attack = Attack + 0;
OAttack = OAttack + 0;
}
or
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
if (SetHealth > 0)
{
{
SetDamage = SetAttack - OSetDefence;
if (SetDamage <=1)
{
SetDamage = 1;
}
cout << "You attacked and dealt " << SetDamage <<" Damage\n" ;
OSetHealth = OSetHealth - SetDamage;
cout << "Your opponents health is " << OSetHealth << "\n\n" ;
}
if (OSetHealth <= 0)
{
cout <<"You Win!" << "\n\n" ;
}
pause(5);
if (OSetHealth > 0)goto OpponentAttack;
}
}
OpponentAttack:
system("CLS" );
{
if (OSetHealth > 0)
{
{
OSetDamage = OSetAttack - SetDefence;
if (OSetDamage <=1)
{
OSetDamage = 1;
}
cout << "Your opponent attacked and dealt " << OSetDamage <<" Damage\n" ;
SetHealth = SetHealth - OSetDamage;
cout << "Your health is " << SetHealth << "\n\n" ;
}
if (SetHealth <= 0)
{
cout <<"You Lose :(\n\n" ;
}
pause(5);
if (SetHealth > 0)goto YouAttack;
}
}
system("CLS" );
}[code]
Last edited on Oct 7, 2012 at 3:00pm UTC
Oct 7, 2012 at 3:02pm UTC
_ No encapsulation (all those getters/setters)
_ You don't understand what objects and classes are (¿how is `Monster' different than `OpponentMonster'?).
({Your,Opponent}*Type)
_ Repeating code
_ Hard-coding
_ Leaks (¿why are you using new
?)
_ All your global belong to us
_ system() (¿are you programming in batch?)
_ A pause() function that fries the processor
_ Looping with goto
_ All functions work with side effects
_ Calling undeclared functions
^ Try to fix those.
A method is a message send to an object, so you need an object to send the message to.