#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
usingnamespace std;
string userInput;
class enemy
{
public:
shortint health;
int diflevel;
shortint enemyAttack;
void level1();
};
class player
{
public:
shortint plyrHealth;
shortint attackPow;
void plyrFunctions();
};
void enemy::level1()
{
vector<string>items;
items.push_back("AR-15");
items.push_back("M1911");
items.push_back("Combat Knife");
vector<string>enemyItems;
enemyItems.push_back("AK-47");
enemyItems.push_back("Mac-10");
enemyItems.push_back("Machete");
vector<string>::const_iterator iter;
cout << "You chose level 1" << endl;
cout << "You encounter an enemy who has " << health << " health,";
cout << " and a difficulty level of " << diflevel << endl;
cout << "His weapons are: " << endl;
for(iter = enemyItems.begin(); iter < enemyItems.end(); ++iter)
{
cout << *iter << endl;
}
cout << "\nAnd your weapons are: " << endl;
for(iter = items.begin(); iter < items.end(); ++iter)
{
cout << *iter << endl;
}
}
void player::plyrFunctions()
{
cout << "\nYour health is also at " << plyrHealth << endl;
}
int main()
{
enemy enemy1;
player userPlayer;
player userPlayer2;
userPlayer2.attackPow = 0;
userPlayer.plyrHealth = 100;
enemy1.health = 100;
srand(static_cast<unsignedint>(time(0)));
enemy1.diflevel = rand() % 5 + 1;
vector<string>items;
items.push_back("AR-15 - 1");
items.push_back("M1911 - 2");
items.push_back("Combat Knife - 3");
vector<string>::const_iterator myIter;
vector<string>enemyItems;
enemyItems.push_back("AK-47");
enemyItems.push_back("Mac-10");
enemyItems.push_back("Machete");
int weaponDam = 0;
if(enemy1.diflevel < 3)
{
enemy1.enemyAttack = rand() % 110 + 1;
}elseif (enemy1.diflevel > 3)
{
enemy1.enemyAttack = rand() % 160 + 1;
}
cout << "Please choose a level: " << endl;
cout << "Level 1\nLevel 2" << endl;
cin >> userInput;
if(userInput == "1")
{
enemy1.level1();
userPlayer.plyrFunctions();
}
cout << "\nIn what way do you want to approach the enemy?" << endl;
cout << "Brute Force - 1\nStealth - 2" << endl;
cin >> userInput;
if(userInput == "1")
{
cout << "Choose a weapon you want to fight with" << endl;
for(myIter = items.begin(); myIter < items.end(); ++myIter)
{
cout << *myIter << endl;
}
}
cin >> userInput;
if(userInput == "1")
{
userPlayer2.attackPow += rand() % 110 + 50;
cout << "\nYou choose the AR-15 which has a damage value of " << userPlayer2.attackPow << endl;
cout << "You will now do " << userPlayer2.attackPow << " damage to your enemy" << endl;
cout << "\nYou and the enemy get into the a fierce firefight" << endl;
cout << "The enemy hits you a couple of times and he does " << enemy1.enemyAttack << " damage to you" << endl;
userPlayer.plyrHealth -= enemy1.enemyAttack;
if(userPlayer.plyrHealth > 0)
{
cout << "\nYou managed to survive his attack.\nYour health is now at " << userPlayer.plyrHealth << endl;
cout << "You then shootback at him and you do " << userPlayer2.attackPow << " damage to him" << endl;
cout << "His health is now at " << enemy1.health - userPlayer2.attackPow << endl;
}
if(userPlayer.plyrHealth < 0)
{
cout << "\nUnlucky! He managed to kill and he has now stole of you weapons" << endl;
enemyItems.insert(enemyItems.begin(), "AR-15");
enemyItems.insert(enemyItems.begin(), "M1911");
enemyItems.insert(enemyItems.begin(), "Combat Knife");
cout << "His items: " << endl;
for(myIter = enemyItems.begin(); myIter < enemyItems.end(); ++myIter)
{
cout << *myIter << endl;
}
}
if(enemy1.health < 0)
{
cout << "\nYou managed to kil your enemy with the AR-15" << endl;
cout << "You take all of his items" << endl;
}
}
cin.get();
cin.get();
}
So if you look at a line 130 cout << "The enemy hits you a couple of times and he does " << enemy1.enemyAttack << " damage to you" << endl; I sometimes get the random number I'd like, but sometimes I get -13108, and I've tried messing around with the different integer variables for enemyAttack e.g unsigned int, unsigned short etc. But any one I try I always get a big negative integer. I've also tried to do the same with the player variables, and the random number generator code with no success
So anyone no idea why it does this?
(Feel free to point out any other flaws you may see in my program :D)
Thanks for that :D Just a quick change to elseif (enemy1.diflevel >= 3) fixed it; now know to start looking at different places in the program other than the specific line that is not working is not working.