Declaring Variables Problem?

I need to declare a variable, without simply stating what it is? Is there anyway to do that? Why I need to is I am making a text game, so when the player fights I don't have to guess what weapon they have. So how would I do this? Here's the fight script part.
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
    do {
    cout << "1. Attack with " << weapon << ".\n";
    cout << "2. Use item.\n";
    cin >> choice;
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
     if( choice == 1 ) {
         dmg;
         enemydmg;
    cout << "You hit " << enemy << " with your " << weapon << " for " << dmg << " damage.\n";
        }
    else if( choice == 2 ) {
        cout << "You don't have any items.\n"; 
    }
    else {
        cout << "That wasn't a choice!\n";
         }
         
    
    cout << enemy << " hits you for " << enemydmg << "!\n"; 
    } while( enemycurrenthp >= 0 );
    
     
    
    
    
    
       
    std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    
    return EXIT_SUCCESS;
} 

Please ask if you don't understand my question. I'm very new to c++.
Last edited on
Yeah, I don't understand.
I understand.

Ideally you will have a lookup table of the available weapons
1
2
3
4
5
6
7
map<string, int> weapons;
weapons["sword"] = 2.0; // 2.0 dmg per hit
weapons["dagger"] = 1.0;

string playerWeapon = "sword";

int dmg = weapons[playerWeapon];


Now, that is kinda a crude solution. It'd be better to use some object orientated development with polymorphism here. But since you're trying to keep it simple doing it the way I've shown is probably not a bad idea. Also, you could swap the strings for enum to improve performance and reduce the possibility of spelling mistakes, but at this level it's likely not important.
When they hit I want their damage to be a random number according to their weapon. I won't always know their weapon, but every time they attack it's the same damage with this code. Help?
Oh lol, thanks Zaita.
My code allows you to do this.

int dmg = weapons[playerWeapon] * (rand() % 10);
This will do dmg of the weapon * 0-9 randomly.
Topic archived. No new replies allowed.