uninitialized variable

i have a problem with the variable "damage" in here. It turns up as some value like -214555.
void Pokemon::attack(Pokemon a,int input)
{
if(input>4||m[input-1]==10)
{cout << "The move you chose is invalid." << endl;}
else
{cout << "You used " << mname[m[input-1]] << "." << endl;
int damage;
double accuracy=1.0;
if(spcl[m[input]]==false&&pwr[m[input-1]]!=0)
{double damage=((2*double(level)+10)/250)*(atk/a.dfns)*pwr[m[input-1]]*((rand()%15+85)/100);}
if(spcl[m[input-1]]==true&&pwr[m[input-1]]!=0)
{double damage=((2*double(level)+10)/250)*(stat[3]/a.stat[4])*pwr[m[input-1]]*((rand()%15+85)/100);}
if(stpwr[2][m[input-1]]!=0)
{accuracy*=stpwr[2][m[input-1]];}
if(stpwr[0][m[input-1]]!=0)
{atk*=stpwr[0][m[input-1]];}
if(stpwr[1][m[input-1]]!=0)
{dfns*=stpwr[1][m[input-1]];}
if(rand()%100/100<=accuracy*acc[m[input-1]])
{a.hp-=damage;}
cout << "Opponent's HP is " << a.hp << "." << endl;
}
}
void Pokemon::react(Pokemon a)
{int aic;
do
{aic=rand()%4;}
while(m[aic]==10);
cout << "The opponent used " << mname[m[aic]] << "." << endl;
int damage;
double accuracy=1.0;
if(spcl[m[aic]]==false&&pwr[m[aic]]!=0)
{double damage=((2*double(level)+10)/250)*(atk/a.dfns)*pwr[m[aic]]*((rand()%15+85)/100);}
if(spcl[m[aic]]==true&&pwr[m[aic]]!=0)
{double damage=((2*double(level)+10)/250)*(stat[3]/a.stat[4])*pwr[m[aic]]*((rand()%15+85)/100);}
if(stpwr[2][m[aic]]!=0)
{accuracy*=stpwr[2][m[aic]];}
if(stpwr[0][m[aic]]!=0)
{atk*=stpwr[0][m[aic]];}
if(stpwr[1][m[aic]]!=0)
{dfns*=stpwr[1][m[aic]];}
if(rand()%100/100<=accuracy*acc[m[aic]])
{a.hp-=damage;}
else
{cout << "Your move missed." << endl;}
cout << "Your HP is " << a.hp << "." << endl;
}
Last edited on
Uh... Yeah... Good luck with that.
Try using a debugger. I'm not going to dig through all that.
Which "damage" variable anyway? There seem to be a lot of them.
And boy oh boy, it might be possible to write an entire book about good programming principles using this as a field case.
In particular, try to move functionality (such as recurring calculations (or even non-recurring)) inside their own functions. Functions should generally be short and only do one particular task (and do it well).
Some more white space would also help readability. Give the curly braces their own line, at least.
I hope this helps someone... After looking through this, I feel like you really need to rethink your design thelateryears (embrace OOP!) This is going to be a bitch to debug in like three days. Also, please format your code with code tags <>
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
double whateverThisDoes(/*I assume*/double someParam) {
	// return ( (2 * double(level) + 10) / 250 ) * someParam * pwr[m[input-1]] * ( (rand()%15 + 85) / 100 );
	return (2.0 * level + 10) / 250
		* someParam
		* pwr[m[input-1]]
		* ( (rand() % 15 + 85) / 100 );
}

void Pokemon::attack(Pokemon a, int input)
{
	if( input > 4 || m[input-1] == 10 )
		cout << "The move you chose is invalid." << endl;
	else
		cout << "You used " << mname[m[input-1]] << "." << endl;
	int damage;
	double accuracy = 1.0;
	// if( spcl[m[input]] == false && pwr[m[input-1]] != 0 )
	if( !spcl[m[input]] && pwr[m[input-1]] != 0 )
		// double damage = whateverThisDoes( atk / a.dfns );
		damage = whateverThisDoes( atk / a.dfns );
	// if( spcl[m[input-1]] == true && pwr[m[input-1]] != 0 )
	if( spcl[m[input-1]] && pwr[m[input-1]] != 0 )
		// double damage = whateverThisDoes( stat[3] / a.stat[4] );
		damage = whateverThisDoes( stat[3] / a.stat[4] );
	if( stpwr[2][m[input-1]] != 0 )
		accuracy *= stpwr[2][m[input-1]];
	if( stpwr[0][m[input-1]] != 0 )
		atk *= stpwr[0][m[input-1]];
	if( stpwr[1][m[input-1]] != 0 )
		dfns *= stpwr[1][m[input-1]];
	if( rand() % 100 / 100 <= accuracy * acc[m[input-1]] )
		a.hp -= damage;
	//You probably wanted some else if's somewhere up there. =^..^=
	cout << "Opponent's HP is " << a.hp << "." << endl;
}
//What is this extra } for?
// }

void Pokemon::react(Pokemon a)
{
	int aic;
	do {
		aic = rand() % 4;
	} while( m[aic] == 10 );
	cout << "The opponent used " << mname[m[aic]] << "." << endl;
	int damage;
	double accuracy = 1.0;
	// if( !spcl[m[aic]] && pwr[m[aic]] != 0 )
	//	damage = whateverThisDoes( atk / a.dfns );
	// if( spcl[m[aic]] && pwr[m[aic]] !=0 )
	//	damage = whateverThisDoes( stat[3] / a.stat[4] );
	if( pwr[m[aic]] != 0 )
		damage = whateverThisDoes( spcl[m[aic]] ? stat[3] / a.stat[4] : atk / a.dfns );
	if( stpwr[2][m[aic]] != 0 )
		accuracy *= stpwr[2][m[aic]];
	if( stpwr[0][m[aic]] != 0 )
		atk *= stpwr[0][m[aic]];
	if( stpwr[1][m[aic]] !=0 )
		dfns *= stpwr[1][m[aic]];
	if( rand() % 100 / 100 <= accuracy * acc[m[aic]] )
		a.hp -= damage;
	//You probably wanted some else if's somewhere up there too.
	else
		cout << "Your move missed." << endl;
	cout << "Your HP is " << a.hp << "." << endl;
}
//Déjà vu 
Last edited on
closed account (z05DSL3A)
thelateryears,
Making you code more readable will help you find the errors...
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
void Pokemon::attack(Pokemon a,int input)
{
    if( (input>4) || (m[input-1] == 10))
    {
        cout << "The move you chose is invalid." << endl;
    }
    else
    {
        cout << "You used " << mname[m[input-1]] << "." << endl;
        int damage;
        double accuracy=1.0;
        if(spcl[m[input]]==false&&pwr[m[input-1]]!=0)
        {
            double damage=((2*double(level)+10)/250)*(atk/a.dfns)*pwr[m[input-1]]*((rand()%15+85)/100);
        }
        if(spcl[m[input-1]]==true&&pwr[m[input-1]]!=0)
        {
            double damage=((2*double(level)+10)/250)*(stat[3]/a.stat[4])*pwr[m[input-1]]*((rand()%15+85)/100);
        }
        if(stpwr[2][m[input-1]]!=0)
        {
            accuracy*=stpwr[2][m[input-1]];
        }
        if(stpwr[0][m[input-1]]!=0)
        {
            atk*=stpwr[0][m[input-1]];
        }
        if(stpwr[1][m[input-1]]!=0)
        {
            dfns*=stpwr[1][m[input-1]];
        }
        if(rand()%100/100<=accuracy*acc[m[input-1]])
        {
            a.hp-=damage;
        }
        cout << "Opponent's HP is " << a.hp << "." << endl; 
    }
}

On line 34, the damage used is the one defined on line 10, this is never initialized or assigned a value so will be 'junk' when you use it.
you are all very funny...
If by "funny" you meant "helpful", then sure.
here is my edited version;char* intopkmn(int);
char* intopkmn(int c)
{return pkmn[c];}
void Pokemon::attack(Pokemon a,int input)
{
double damage;
if(input>4||m[input-1]==10)
{cout << "The move you chose is invalid." << endl;}
else
{cout << "You used " << mname[m[input-1]] << "." << endl;
double accuracy=1.0;
if(pwr[m[input-1]]!=0)
{damage=dmgcalc(spcl[m[input-1]],a,input-1);}
if(stpwr[2][m[input-1]]!=0)
{accuracy*=stpwr[2][m[input-1]];}
if(stpwr[0][m[input-1]]!=0)
{atk*=stpwr[0][m[input-1]];}
if(stpwr[1][m[input-1]]!=0)
{dfns*=stpwr[1][m[input-1]];}
if(rand()%100/100<=accuracy*acc[m[input-1]])
{a.hp-=damage;}
cout << "Opponent's HP is " << a.hp << "." << endl;
}
}
void Pokemon::react(Pokemon a)
{int aic;
do
{aic=rand()%4;}
while(m[aic]==10);
cout << "The opponent used " << mname[m[aic]] << "." << endl;
double damage;
double accuracy=1.0;
if(pwr[m[aic]]!=0)
{damage=dmgcalc(spcl[m[aic]],a,aic);}
if(stpwr[2][m[aic]]!=0)
{accuracy*=stpwr[2][m[aic]];}
if(stpwr[0][m[aic]]!=0)
{atk*=stpwr[0][m[aic]];}
if(stpwr[1][m[aic]]!=0)
{dfns*=stpwr[1][m[aic]];}
if(rand()%100/100<=accuracy*acc[m[aic]])
{a.hp-=damage;}
else
{cout << "Your move missed." << endl;}
cout << "Your HP is " << a.hp << "." << endl;
}

and
class Pokemon
{protected:
int stat[6];
int species;
int level;
int iv;
int m[4];
public:
int hp;
int atk;
int dfns;

Pokemon(int a,int b)
{cout << "Making Pokemon" << endl;
iv=(rand()%31);
species=a;
level=b;
stat[0]=(((iv)+basestat[species-1][1]+50)*level)/50+10;
for(int i=5;i>0;i--)
{stat[i]=(((iv)+basestat[species-1][i])*level)/50+5;}
hp=stat[0];
atk=stat[1];
dfns=stat[2];
m[0]=10;
m[1]=10;
m[2]=10;
m[3]=10;
if(species==2)
{m[0]=0;
m[1]=2;
}
else
{m[0]=1;}
if(species==3&&level>3)
{m[1]=3;}
if(species==1&&level>2)
{m[1]=2;}
if(level>6)
{m[2]=2*species+2;}
if(level>8&&(species==1||species==2))
{m[3]=2*species+3;}
if(level>9&&species==3)
{m[3]=9;}
}
void showstats()
{for(int i=1;i<7;i++)
{cout << "Stat " << i << ": " <<stat[i-1] << endl;}
}
void showmoves()
{for(int i=1;i<5;i++)
{cout << "Move " << i << ": " << mname[m[i-1]] << endl;}
}
void attack(Pokemon,int);
void react(Pokemon);
double Pokemon::dmgcalc(bool c,Pokemon d,int n)
{if(c)
{return ((2*double(level)+10)/250)
*(stat[3]/d.stat[4])
*pwr[m[n]]
*((rand()%15+85)/100);
}
else
{return ((2*double(level)+10)/250)
*(atk/d.dfns)
*pwr[m[n]]
*((rand()%15+85)/100);
}
}
}
The damage still is 0 r is not geting subtracted from the hp
Ok. thelateryears, you need to learn to use the code tags. they're on the right-hand side of the text box that you type into when you leave a comment. Also, you need to format your code better; give curly braces their own line, and if you aren't going to do that, atleast space things out, so that certain things line up, like the following:

1
2
3
4
5
6
7
int x = 0, y = 9;
if ( x == 0 && y == 8) {  cout << "HELLO!"       << endl; }
if ( x == 9 && y == 9) {  cout << "GOODBYE!"     << endl; }
if ( x == 0 && y == 9) {  cout << "GOOD MORNING!"<< endl; }
if ( x == 9 && y == 0) {  cout << "GOOD NIGHT!"  << endl; }
//notice all of the variables match up, if statements match up, the "<< endl;"'s match up, etc.


just please, for the love of god, make your code easier to read :)

extra note: Don't use classes where they aren't really needed; classes can sometimes make things confusing... just make stand-alone functions. I don't mean to be mean, but I just HATE it when people make their code hard to read and understand. But... You're coding for Pokemon, so I want you to keep going :)
Last edited on
desoxena wrote:
Don't use classes where they aren't really needed; classes can sometimes make things confusing...
I completely disagree! For any program that requires this much code to do something relatively small, classes can make things a whole lot easier to explain, code, debug, and modify. I mean in my opinion (although there is a saying about those) a Pokemon is practically begging to be represented with a class having all these stats and what not.
closed account (z05DSL3A)
thelateryears wrote:
The damage still is 0 r is not geting subtracted from the hp

Without looking to hard*, in Pokemon::dmgcalc() you have *(stat[3]/d.stat[4]), stat looks to be an int[] so the likely result of the afore mentioned code would be to multiply by zero, thus making the overall result zero.

* your coding style is appalling, I can see why you are having problems. Use whitespaces, format your code, use better names, post your code with code tags [code][/code] etc.
your coding style is appalling


Grey Wolf was a bit harsh with that phrase... but he has a point. You really need to practice code structure. Basically, make it look nice, line things up, WHITE-SPACE.
wow dude....wow. Are you copy and pasting into the submit box? Or is your code already written like that?
grey wolf is not that harsh at all- but too much whitespace makes it hard to follow along the program for me.
Put it this way- would you like it if i put all my code on one line?
I write my c0ode like that already- the edit box erases all initial spaces.
Last edited on
ok--what i got out of this is that
1. All of you are obsessed about whitespace
2. i need more doubles
If that's the case, then why not write it like this?
1
2
3
char*intopkmn(int c){return pkmn[c];}void Pokemon::attack(Pokemon a,int input){double damage;if(input>4||m[input-1]==10){cout<<"The move you chose is invalid."<<endl;}else{cout<<"You used "<<mname[m[input-1]]<<"."<<endl;double accuracy=1.0;if(pwr[m[input-1]]!=0){damage=dmgcalc(spcl[m[input-1]],a,input-1);}if(stpwr[2][m[input-1]]!=0){accuracy*=stpwr[2][m[input-1]];}if(stpwr[0][m[input-1]]!=0){atk*=stpwr[0][m[input-1]];}if(stpwr[1][m[input-1]]!=0){dfns*=stpwr[1][m[input-1]];}if(rand()%100/100<=accuracy*acc[m[input-1]]){a.hp-=damage;}cout<<"Opponent's HP is "<<a.hp<<"."<<endl;}}void Pokemon::react(Pokemon a){int aic;do{aic=rand()%4;}while(m[aic]==10);cout<<"The opponent used "<<mname[m[aic]]<<"."<<endl;double damage;double accuracy=1.0;if(pwr[m[aic]]!=0){damage=dmgcalc(spcl[m[aic]],a,aic);}if(stpwr[2][m[aic]]!=0){accuracy*=stpwr[2][m[aic]];}if(stpwr[0][m[aic]]!=0){atk*=stpwr[0][m[aic]];}if(stpwr[1][m[aic]]!=0){dfns*=stpwr[1][m[aic]];}if(rand()%100/100<=accuracy*acc[m[aic]]){a.hp-=damage;}else{cout<<"Your move missed."<<endl;}cout<<"Your HP is "<<a.hp<<"."<<endl;}

class Pokemon{protected:int stat[6];int species;int level;int iv;int m[4];public:int hp;int atk;int dfns;Pokemon(int a,int b){cout<<"Making Pokemon"<<endl;iv=(rand()%31);species=a;level=b;stat[0]=(((iv)+basestat[species-1][1]+50)*level)/50+10;for(int i=5;i>0;i--){stat[i]=(((iv)+basestat[species-1][i])*level)/50+5;}hp=stat[0];atk=stat[1];dfns=stat[2];m[0]=10;m[1]=10;m[2]=10;m[3]=10;if(species==2){m[0]=0;m[1]=2;}else{m[0]=1;}if(species==3&&level>3){m[1]=3;}if(species==1&&level>2){m[1]=2;}if(level>6){m[2]=2*species+2;}if(level>8&&(species==1||species==2)){m[3]=2*species+3;}if(level>9&&species==3){m[3]=9;}}void showstats(){for(int i=1;i<7;i++){cout<<"Stat "<<i<<": "<<stat[i-1]<<endl;}}void showmoves(){for(int i=1;i<5;i++){cout<<"Move "<<i<<": "<<mname[m[i-1]]<<endl;}}void attack(Pokemon,int);void react(Pokemon);double Pokemon::dmgcalc(bool c,Pokemon d,int n){if(c){return((2*double(level)+10)/250)*(stat[3]/d.stat[4])*pwr[m[n]]*((rand()%15+85)/100);}else{return((2*double(level)+10)/250)*(atk/d.dfns)*pwr[m[n]]*((rand()%15+85)/100);}}}

By the way, use [code][/code] tags, not [tt][/tt] tags.
thelateryears wrote:
i have a problem with the variable "damage" in here.
thelateryears wrote:
All of you are obsessed about whitespace

Are you beginning to see the correlation here? You came to us asking for help, we told you how to avoid problems in the future. We aren't obsessing, we are trying to help you. I could care less how you write your code, just don't ask me to debug it!
ok---you seem quite offended
Topic archived. No new replies allowed.