if statement and else statement both being executed?

Hi, I started working with the tutorial section of this website about 3 weeks ago, and I try to make a program every day. I like games, therefore I usually make simple programs that have something to do with common rpg stats. I finished the chapter about functions about 2 days ago, and decided to make a program using each "type" of function. However, This particular program has me stumped seeing as how it works without problems if a != yes, but it displays both : "the players damage remains constant" and "the players damage is doubled", even though it shows the correct value for the variable damage. This has lead me to believe that both the if and else statements are being executed, which I actually did not even think was possible.

If you got this far, thank you for taking the time to read my post. Any help would be much appreciated.

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
#include <iostream>
#include <cstdlib>


using namespace std;

bool itemcheck (string a)
{

    if (a == "yes")
    return true;


}

int main()
{
    int strength;
    cout << "enter your strength: ";
    cin >> strength;

    string a;
    cout << "does player have silver horn? ";
    cin >> a;

    int damageamplifier = itemcheck(a);

    int damage = strength * 1.5;

    if (damageamplifier == true)
    {

    damage = damage*2;
    cout << "the players damage has been doubled " << endl;

    }

    else;
    cout << "the players damage remains constant" << endl;


    cout << "the player does this much damage: " << damage << endl;

    system("PAUSE");
    return 0;
}
Remove the semicolon after your else.

Edit: I don't like how you formatted your code. Here's a neater version of it:

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
#include <iostream>
#include <cstdlib>

using namespace std;

bool ItemCheck(string s)
{
    if (s == "yes")
    {
        return true;
    }

    return false;
}

int main()
{
    int strength;
    cout << "Enter your strength: ";
    cin >> strength;

    string silverHorn;
    cout << "Do you have silver horn? ";
    cin >> silverHorn;
    
    int damage = strength * 1.5;

    if (ItemCheck(silverHorn))
    {
        damage *= 2;
        cout << "Your damage has been doubled." << endl;
    }
    else
    {
        cout << "You remain constant." << endl;
    }

    cout << "You do this much damage: " << damage << endl;

    system("PAUSE");

    return 0;
}
Last edited on
Wow! Thank you so much for your fast and accurate response! I have to say, I really appreciate this forum and great users like you who are willing to help a beginner out.
You are welcome. :-)
Topic archived. No new replies allowed.