#include <iostream>
#include <string>
#include <cstdlib>
#include <windows.h>
usingnamespace std;
int oppHP = 100;
int yourHP = 100;
int dmg = 10;
void attack()
{
oppHP = oppHP - dmg;
}
void Oppattack()
{
yourHP = yourHP - dmg;
}
void space()
{
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
}
void update()
{
system("CLS");
}
int main()
{
while((yourHP > 0) || (oppHP > 0)) // main problem seems to stem from here
{
cout << "Your hp: " << yourHP << " " << "Enemy hp: " << oppHP;
space();
cout << "Attack? (y/n): ";
char input;
cin >> input;
if (input == 'y')
{
attack();
update();
}
elseif (input == 'n')
{
cout << "You get atttacked instead!" << endl;
Sleep(500); // cin.get not recognized for some reason, used sleep instead
Oppattack();
update();
}
else
{
cout << "Invalid response";
Sleep(500);
system("CLS");
}
}
if (yourHP == 0) // works fine
{
cout << "Game over. You lose";
Sleep(500);
return 0;
}
elseif (oppHP == 0) // isn't acknowledged and goes into negative value
{
cout << "Game over, you win!";
Sleep(500);
return 0;
}
}
The first thing I did was review the section on operators here and what I notice the description state that the right hand side expression wasn't evaluated. Is there any way to offset this? I'm trying to keep the loop going until either value hits 0