Help with my program

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace std;


string userInput;

class enemy
{
	public:
    		
			short int health;
    		int diflevel;
			short int enemyAttack;
    		void level1();
};

class player
{
	public:
			short int plyrHealth;
			short int 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<unsigned int>(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;
	}else if (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)
Consider the following code at line 88:
1
2
3
4
5
6
7
if(enemy1.diflevel < 3)
	{
		enemy1.enemyAttack = rand() % 110 + 1;
	}else if (enemy1.diflevel > 3)
	{
		enemy1.enemyAttack = rand() % 160 + 1;
	}

What if enemy1.diflevel is 3? enemy1.enemyAttack will be uninitialized.
Thanks for that :D Just a quick change to else if (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.
Topic archived. No new replies allowed.