Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7600287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:52:29+00:00 2026-05-30T22:52:29+00:00

I am learning C++ and made a script, a very basic player attacking monster

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:52:30+00:00Added an answer on May 30, 2026 at 10:52 pm
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently learning PHP. I've made a simple script @ http://hash.techho.me , only thing
I am learning Python and am making a Q&A script. I made one function
While learning objective-c, I've made a basic mac shopping list app with 3 interface
I am learning to program for the Android. I've made a couple of basic
Learning about notifyAll made me question something about notify: in a typical situation we
I learning Perl and doing a home made project to my family (a subscription
I'm learning how to use threads in Android, and to do that I've made
I am learning jQuery UI. I downloaded a code in which the author made
I am learning C++ and I have a question. I made a class in
I'm currently learning iPhone applications development, and I made some online tutorials to learn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.