Reputation: 8280
I am learning C++ and made a script, a very basic player attacking monster type thing. But i get undefined identifier issues =/
I don't quite understand why, I come from a PHP background so its probably me not totally adapted to C++ strict ways but I'm hoping some one could explain.
The errors:
main.cpp(30): error C2065: 'miss' : undeclared identifier
main.cpp(31): error C2065: 'hit' : undeclared identifier
main.cpp(43): error C2065: 'pmiss' : undeclared identifier
main.cpp(46): error C2065: 'phit' : undeclared identifier
main.cpp(47): error C2065: 'phit' : undeclared identifier
main.cpp(49): error C2065: 'mmiss' : undeclared identifier
main.cpp(52): error C2065: 'mhit' : undeclared identifier
main.cpp(53): error C2065: 'mhit' : undeclared identifier
My script:
#include <iostream>
int player_health = 10;
int monster_health = 10;
void main(){
do{
std::cout << "#####################################" << std::endl;
std::cout<< "Pick you're weapon and hit enter!" << std::endl;
std::cout << "Press k for knife or b for Bat!" << std::endl;
std::cout <<""<<std::endl;
std::cout << "Player Health: "<< player_health << std::endl;
std::cout << "Monster Health: "<< monster_health << std::endl;
std::cout << "###################################"<< std::endl;
std::cout<<""<<std::endl;
char user_weapon;
std::cin >> user_weapon;
if(user_weapon == 'k' || user_weapon == 'b'){
if(user_weapon == 'k'){
int miss = 5;
int hit = 5;
} else if(user_weapon == 'b'){
int miss = 2;
int hit =3;
}
if((rand() % miss) < 3){
int phit = (rand()% hit);
} else {
bool pmiss = true;
}
//monster
if((rand() % 5) < 3){
int mhit = (rand()%3);
} else {
bool mmiss = true;
}
if(pmiss){
std::cout << "Player missed monster!"<<std::endl;
}else{
monster_health = monster_health - phit;
std::cout << "Player hit monster for " << phit << "!" << std::endl;
}
if(mmiss){
std::cout << "Monster missed player!"<<std::endl;
} else{
player_health = player_health - mhit;
std::cout << "Monster hit for " << mhit << "!" <<std::endl;
}
} else {
std::cout << "Invalid input , try either k or b" << std::endl;
}
}while(player_health >0 || monster_health > 0);
std::cout << "###################################"<< std::endl;
std::cout<<""<<std::endl;
if(player_health < 0 && monster_health < 0){
std::cout << "It's a draw!" << std::endl;
} else if (player_health > monster_health){
std::cout<<"Player Wins!" << std::endl;
} else {
std::cout<<"Monster Wins!" << std::endl;
}
std::cout << "###################################"<< std::endl;
std::cout<<""<<std::endl;
}
If you run the script you should get the same errors as I did.
Upvotes: 1
Views: 5167
Reputation: 178451
if(user_weapon == 'k'){
int miss = 5;
int hit = 5;
} else if(user_weapon == 'b'){
int miss = 2;
int hit =3;
}
if((rand() % miss) < 3){
You are defining miss
in the if
's scope, and then using it out of the scope, so in: if((rand() % miss) < 3){
, you get that miss
is undefined - because it is defined only in the scope where you declared it.
Note that in c++, you cannot do it, there is static scoping in c++.
You should define miss
before the if
scope, and only assign a value to it there.
The same also applies for other variables, such as hit
and pmiss
, of course.
Upvotes: 5