EDIT: I've got a while loop in my code, just now when player 1 goes to have his/her second attack the hp is reset to 99,

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    int p1attack, p2attack, p1hp, p2hp;
    char attack;
    
    p1hp = 99;
    p2hp = 99;
    
    system("TITLE Game.");
    
    while(p1hp > 0 && p2hp > 0)
    {
   
    cout << "Player 1 (HP:" << p1hp << ") Press 'A' to attack : ";
    cin >> attack;
    
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p1attack = rand() % 99 + 1; 
         cout << "You hit " << p1attack << "!";
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    }
    p2hp -= p1attack;
    
    cout << endl << endl << "Player 2 (HP:" << p2hp << ") Press 'A' to attack : ";
    cin >> attack;
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p2attack = rand() % 99 + 1; 
         cout << "You hit " << p2attack << "!" << endl;
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    
    p1hp -= attack;
    }
    

}
getch();
}

Hey Black Magic i have solved your problem.

Well just examine That you have typed this code in

p1hp -= p2attack;

Line 54. Which probarbly doesnt get executed because there are break; statements above it.

So You actually need to paste the code in between Line 46 and line 47 so that the value gets changed.

So here is the total program with the adjustment.

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    int p1attack, p2attack, p1hp, p2hp;
    char attack;
    
    p1hp = 99;
    p2hp = 99;
    
    system("TITLE Game.");
    
    while(p1hp > 0 && p2hp > 0)
    {
   
    cout << "Player 1 (HP:" << p1hp << ") Press 'A' to attack : ";
    cin >> attack;
    
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p1attack = rand() % 99 + 1; 
         cout << "You hit " << p1attack << "!";
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    }
    p2hp -= p1attack;
    
    cout << endl << endl << "Player 2 (HP:" << p2hp << ") Press 'A' to attack : ";
    cin >> attack;
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p2attack = rand() % 99 + 1; 
         p1hp -= p2attack;
         cout << "You hit " << p2attack << "!" << endl;
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    
    
    }
    

}
getch();
}

Or, if you'd like to keep in consistent with the code for player 1's attack, you could simply put it outside of the switch statement but inside the while loop (like line 57 or 58 in Sky Diploma's program). If you do do that though, remember to take away that same code from line 47.

I actually put the code in 47 Keeping in mind that there is an option for an attacker not to attack also. right

For example if the user enters something other than "A".

I actually put the code in 47 Keeping in mind that there is an option for an attacker not to attack also. right

For example if the user enters something other than "A".

Hmm.... yes you're very correct. In fact, the same thing should be done to the player1 code, because if p2hp -= p1attack; is called and p1attack hasn't been initialized, you'll get not so optimal results.

So either change it, or in the beginning of the loop set p1attack and p2attack to 0.

Thanks guys, now i was wondering now if i should make it so the other player cannot make your hp a negative number, how could this be done? Please reply.

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    int p1attack, p2attack, p1hp, p2hp;
    char attack;
    
    p1hp = 99;
    p2hp = 99;
    
    system("TITLE Game.");
    
    while(p1hp > 0 && p2hp > 0)
    {
   
    cout << "Player 1 (HP:" << p1hp << ") Press 'A' to attack : ";
    cin >> attack;
    
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p1attack = rand() % 50 + 1; 
         cout << "You hit " << p1attack << "!";
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    }
    p2hp -= p1attack;
    
    cout << endl << endl << "Player 2 (HP:" << p2hp << ") Press 'A' to attack : ";
    cin >> attack;
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p2attack = rand() % 50 + 1; 
         p1hp -= p2attack;
         cout << "You hit " << p2attack << "!" << endl << endl;
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    
    
    }
    

}
getch();
}

Right after p2hp -= p1attack; , add:

if(p2hp < 0)
    p2hp = 0;

You may also try making the health values unsigned, though the above method should work well enough

Hey Black Magic i think you will need to end the game once a person's hp goes into negatives .So try using the || Operator instead of && in Line 16

This article has been dead for over six months. Start a new discussion instead.