Not sure where I went wrong..

I made a simple battle simulator program to practice my loops and such but when I run the program the human forces always win with no losses. at first I figured this was cause I was missing, turn = 'E'; after humans turn was processed however when I added the line the problem persisted.

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<iostream>
#include<cstdlib>
#include<string>
#include<random>
#include<ctime>

using namespace std;

void battleSim(int x, int y);

//initializing and seeding random number generator.
mt19937 randEng(time(NULL));
uniform_real_distribution<float> attack(0.0f, 1.0f);

int main()
{
    //variables for to store user input
    int humans, elves;

    cout << "--------Basic Human vs. Elf Battle Sim.------------ \n\n" << endl;

    //request user to enter number of humans. prompt them to reenter in case of bad entry
    while(cout << "How many humans will battle: " && cin >> humans){
        if(humans <= 0 || humans >3000){
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Invalid number please stay within the range of 1-3000" << endl;
        }else break;

    }

    //request user to enter number of elves. prompt them to reenter in case of bad entry
    while(cout << "How many elves will battle: " && cin >> elves){
        if(elves <= 0 || elves >3000){
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Invalid number please stay within the range of 1-3000" << endl;
        }else break;

    }

    cout << "\nLet the Battle BEGIN!!!! \n\n" << endl;
    cout << "Results: \n"<< endl;

    battleSim(humans, elves);

    system("PAUSE");
    return 0;
}

//x = humans, y = elves
void battleSim(int x, int y)
{
    //turn - used to keep track of the turns. atkRoll - used to store the attack result
    char turn = 'E';
    float atkRoll;

    //defined human properties curHumHP - used to hold the currently fighting humans health
    float curHumHP = 75.0f;
    float humHP = 75.0f;
    float humATK = 0.6f;
    float humDMG = 70.0f;

    //defined elf properties curElfHP - ^^^
    float curElfHP = 60.0f;
    float elfHP = 60.0f;
    float elfATK = 0.75f;
    float elfDMG = 60.0f;

    //perform battle until one army has 0 forces
    while(x > 0 && y > 0){
        //getting attack result
        atkRoll = attack(randEng);
        //Elves turn
            if(turn == 'E'){
                //checking for elves hit success or failure.
                if(atkRoll <= elfATK){
                    //damage the current opposing enemy
                    curHumHP -= elfDMG;
                    //checking current enemy health and reacting
                    if(curElfHP <= 0){
                        //reducing total forces and reseting current enemy health
                        x--;
                        curHumHP = humHP;
                    }
                }
                //changing turns
                turn = 'H';

            }else{//Humans turn
                //checking for humans hit success or fail.
                if(atkRoll <= humATK){
                    //damage the current enemy
                    curElfHP -= humDMG;
                    //check current enemy health
                    if(curElfHP <= 0){
                        //reducing total forces and reseting current enemy health
                        y--;
                        curElfHP = elfHP;
                    }
                }
                //changing turns
                turn = 'E';
            }
    }
    //Display results of battle loop
    if(x > 0){
        cout << "The Humans are victorious today. The remaining soldiers will rejoice. \n" << endl;
        cout << "Human Forces Left: " << x << endl;
    }else{
        cout << "The Elves are victorious today. The remaining soldiers will rejoice. \n" << endl;
        cout << "Elf Forces Left: " << y << endl;
    }
}
Last edited on
closed account (D80DSL3A)
Line 81. curElfHP should be curHumHP instead?
Wow thank you so much. I looked right over that so many times...
Last edited on
Topic archived. No new replies allowed.