while loop

Fellow! I'm stuck here. Currently, all this code right now doing is basically letter the user enter the damage and subtracting 5. Moreover, when it comes back to the loop, it asks me to enter in another damage. My problem is that i'm trying use the previous damage value too and subtract it by 5. For instance, I enter in Damage: 125. Total health remaining: 120. It asks me again for damage: 10. Total health: 5. I'm trying to save the total damage value from the first cycle and when I enter in another damage value, I want it to do "120-10-5." I'm a beginner here, so can anyone please give me a basic solution to this. :)

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
                  int health=5;
		int damage;
		bool flag=false;
		int newdamage=0;

		while (!flag)
		{
		
			cout<< "Enter Damage: " ;
			cin>>damage;
			damage=damage-health;
			cout<<"\n\n\tHealth Remaining: " << damage;

			if (damage<=0)
			{
					cout<<"you are dead";
					flag= true;
			}
			else (damage>=0);
			{cout<<"YOUR ALIVE!";}

				cout<<"Total damage" <<damage;
		
		}

		cout<<"you lost!";
You don't need any conditions for the "else" statement.

It can just be like this:

1
2
3
4
5
6

else{

     cout << "Your ALIVE!" << endl;
     cout << "Total damage " << damage << endl;
}



I'm not sure how you want to set up the damage thing, but if your goal is to get something like "120-10-5", then I'd try something like this:

1
2
3
4
5

health = health - damage;//so if the starting health was 120,
//and the damage was 10 the first round and 10 the second round, 
//the health would be 120-10-10 = 100;


In general, you want to be changing and outputting the variable "health", not "damage".
Last edited on
Thank you bingo1 :) Makes more sense now :)
Topic archived. No new replies allowed.