Program skips if statements

For some reason the if statements at lines 52 and 109 are ignored. Also in the battle function, the enemyH = enemyH - atk; does nothing. How should i fix 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
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
115
116
117
void battle()
{
    int enemyAttack;
    string enemy;
    string attack;
    int enemyH;
    string eAtkType;
    bool win;
    int health;
    int enemyType;
    health = 10;
    enemyH = 10;
    srand (time(0));
    enemyType = rand() % 3 + 1;
    switch (enemyType)
    {
        case 1:          //Fire Type
            cout<<"An Angry Red Panda appeared!";
            fired = 2;
            earthd = 1;
            waterd = 3;
            enemy = "Angry Red Panda";
            eAtkType = "Fire";
            enemyAttack = enemyFire;
            break;
        case 2:          //Water Type
            cout<<"An Angry Blue Turtle appeared!";
            fired = 1;
            earthd = 3;
            waterd = 2;
            enemy = "Angry Blue Turtle";
            eAtkType = "Water";
            enemyAttack = enemyWater;
            break;
        case 3:          //Earth Type
            cout<<"An Angry Green Mole appeared!";
            fired = 3;
            earthd = 2;
            waterd = 1;
            enemy = "Angry Green Mole";
            eAtkType = "Earth";
            enemyAttack = enemyEarth;
            break;
    }
    cout<<"\n You've entered battle!\n ";
    do
    {
        cout<<"\n Health:"<<health;
        cout<<"\n Enemy Health:"<<enemyH;
        cout<<"\n Attack "<<enemy<<"?";
        cin>>attack;
        if (attack == "yes")
        {
            enemyH = enemyH - atk;
        }
        else if (attack == "no")
        {
            cout<<"\n You skipped a turn.";
        }
        if (enemyH > 0)
        {
            cout<<"\n Enemy used "<<eAtkType<<".";
            health = health - enemyAttack;
        }
        else
            cout<<"\n Enemy Fainted!";
    }
    while (health > 0 || enemyH > 0);
    if (health > 0)
        win = true;
    else
        win = false;
    return;
}


void name()
{
    char nameAns;
    string pet;
    cout<<"Which pet do you want?";
    cout<<"\n Red Panda";
    cout<<"\n Blue Turtle";
    cout<<"\n Green Mole \n";
    cin>>pet;
    if (pet == "red panda")
    {
        atk = fired;
        enemyFire = 2;
        enemyWater = 3;
        enemyEarth = 1;
    }
    else if (pet == "blue turtle")
    {
        atk = waterd;
        enemyFire = 2;
        enemyWater = 3;
        enemyEarth = 1;
    }
    else if (pet == "green mole")
    {
        atk = earthd;
        enemyFire = 2;
        enemyWater = 3;
        enemyEarth = 1;
    }
    cout<<"Would you like to name your pet? Y/N \n";
    cin>>nameAns;
    if (nameAns == 'y')
    {
        cout<<"What will you name it?\n";
        cin>>petName;
        cout<<"\n Your pet's name is now "<<petName;
    }
    else
        petName = pet;
    return;
Last edited on
I've not actually tested/parsed the code but I'm sure you must clear the buffer with the cin.ignore(); function, before the cin statements, which come before the if statements.

Aceix.
neither of those seemed to work :/

if it helps, here is the beginning of the code.

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
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
void name();
void battle();
string petName;
int enemyFire;
int enemyWater;
int enemyEarth;
int atk;
int fired;
int earthd;
int waterd;
bool win;
int main()
{
    int played;
    played = 0;
    bool victory;
    char begin;
    string play;
    cout<<"Welcome to Combat System V2.0";
    do
    {
        cout<<"\n\n Begin? Y/N";
        cin>>begin;
        if (played == 0 && (begin == 'y' || begin == 'Y'))
            {
            name();
            battle();
            played = played + 1;
            }
        victory = win;
        if (victory == true)
        {
            cout<<"You Won!";
        }
        else 
        {
            cout<<"You Lost.";
        }
        cout<<"Play Again?";
        cin >> play;
    }
    while (play == "Yes" || play == "yes");
    return 0;
}
Last edited on
Your line 54, the:

enemyH = enemyH - atk;

can be replaced with:

enemyH -= atk;

and perform the same functionality. With that change, it should work as expected, though I am not 100% sure.

As for your other problem, the one concerning the disregard for the two if statements, you use cin in order to obtain a name of a pet. However, all of the pets have a name which includes a space. When you use cin, it only goes up to the first instance of you hitting the space key, so the second half of the pet name is still in the "input buffer." When you get to the next cin statement, that previous input is included, thus nulling out the if statements. You can test this by placing an else statement, which consists of an error message. I would recommend using getline instead of cin in all instances of cin in your code.
Topic archived. No new replies allowed.