Using an Array in a while loop

I’ve been working on this for away, it’s a test program for a turn based combat system. Everything is working well except the if statement. It’s supposed to use the next number after you chose again, but it’s always stuck on the first number. If you have a more effective method, that would be much appreciated.

#include <iostream> #include <cmath> #include <string> #include <cstdlib> #include <iomanip>  using namespace std;  int main() {     int item;     int potion[] = { 20, 15, 10, 5 };     int p = 0;     int battle;     int health = 100;     int attack = 25;     int ehealth;     float eattack = 20;     int magic = 50;     ehealth = 100;     cout << "1 attack, 2 attack with magic, 3 Guard attack, 4 Use items" << endl;     while (ehealth > 0) {         cin >> battle;         switch (battle) {         case 1: {             cout << "You did " << attack << " damage!\n" << endl;             ehealth = ehealth - attack;             break;         }         case 2: {             cout << "You used magic doing " << magic << " damage!\n" << endl;             ehealth = ehealth - magic;             break;         }         case 3: {             cout << "You guard against the the attack!\n" << endl;             health = health - (eattack / 10);             break;         }         case 4: {             cout << "Pick an item.\n 1. potion\n" << endl;             cin >> item;             if (item == 1) {                 cout << "You recovered " << potion[p] << " Hp" << endl;                 health = health + potion[p];             }             break;         }         }         cout << "The enemy attacks!\n" << endl;         health = health - eattack;         cout << "Enemy Health: " << ehealth << endl;         cout << "Your Health: " << health << endl;     }     return 0; }  
Professor Asked on October 25, 2020 in Android langue.
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.