Help me with this easy code please :)

I recently start to learn some C++ by watching some tutorials on YouTube, I learnt the random number generator and "coocked up" a game with it, I need help with the else part from the while loo[ it seems to just use the else loop while the aspirine int reaches 0 also some improvements and tips for my code are welcome :)

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
  #include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int aspirine = 3;
int main() 
{
	int humans, zombies, enter, aux, max;
	cout<<""<<endl;

	
	 cout<<"                                                 HUMANS VS ZOMBIES"<<endl<<endl<<endl<<endl;
	 cout<<"PRESS ENTER TO BEGIN"<<endl;
	 cin.get();
	
	cout<<""<<endl;
	cout<<""<<endl;
	cout<<"INPUT THE AMOUNT OF ENEMIES"<<endl;
	cin>>zombies;
	
	
	cout<<"You hear a lot of noise outside and you are going to check out what's happening"<<endl<<endl;
	cin.get();
	cin.get();
	
	cout<<"All of a sudden you get attacked by "<<zombies<<" zombies."<<endl;
    cin.get();
    
    cout<<"You get scared and run back inside your home and take your gun"<<endl;
    cin.get();
    
    cout<<"After you catch your breath you go outside with the gun and try to take them out to defend your home and kids"<<endl;
    cin.get();
    
    cout<<"There is a 10% chance to miss and not kill the zombie"<<endl;
    cin.get();
    
    
    cout<<"Press enter to fire your gun"<<endl;
    cin.get();
    
    srand(time(0));
    aux = rand()%100+1;
    int chance=10;
   while (zombies != 0 && chance <= 100 && aspirine !=0){
	if (aux<chance)
    {   
    cout<<"You missed and the zombie bit you, good thing you had an Aspirine to stop the infection"<<endl;
    aspirine = aspirine-1;
    cout<<"You only have "<<aspirine<<" Aspirine left."<<endl;
    }
    else 
    {
    zombies = zombies - 1;
    cout<<"You killed the zombie, only "<<zombies<<" left."<<endl;
    cout<<"You became tired after killing this zombie so your chance of missing increased by 5%"<<endl;
    cin.get();
    chance = chance+5;
    } 
    }
	
	return 0;

}
This is your if else statement
if (aux<chance)

It has nothing to do with your while statement, it's only inside the while loop.
I think if you insert this code you'll be able to figure out what you need.

1
2
3
   while (zombies != 0 && chance <= 100 && aspirine !=0){
cout << "A= " << aux << " C = " << chance << endl;
	if (aux<chance)


Hello guys,

Just thinking here: aux is a randomly generated number, so what are the chances that aux would be less than chances the first few times through the while loop?

Andy
Topic archived. No new replies allowed.