Function irresponsibilities.

Hello, I'm trying to make myself a very very basic game, and so far so good. But since i haven't gotten that "far" itsn't not very great either...

Anyway, the problem's at line 20, whereas i want it to cout my little message. It just doesn't, for some reason which i cannot figure out by myself. So i figured you guys would be able to stupidify me and solve this one in the blink of an eye! :)

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

using namespace std;

const int maxmonsters = 3, minmonsters = 1;
const string smonsternames[maxmonsters] = {"Gargoyle", "Minotaur", "Rabbit"};

void menuchoice()
{
    cout <<"Which monster would you like to fight?\n"
    << "1) Gargoyle\n"
    << "2) Minotaur\n"
    << "3) Rabbit\n";
}

int gargoylefight(int imcheck)
{
    int ixp = 50;
    cout << "You're battling the " << smonsternames[imcheck] << "\n";
    return (ixp);
}


int main()
{
    int ixp = 0;
    bool bqcheck = 1;
    int imcheck;
     cout << "1 to start, 0 to quit: ";
      cin >> bqcheck;
      if (bqcheck == 0)
      return 0;
      menuchoice();
      cin >> imcheck;
      if (!(cin >> imcheck) || imcheck < minmonsters || imcheck > maxmonsters)
      cout << "Not eligible number";
      if (imcheck == 1)
      gargoylefight(imcheck);
      //else if (imcheck == 2)
      //minotaurfight(imcheck);
      //else if (imcheck == 3)
      //lamefight(imcheck);
      system("pause");
      return 0;
    }


Feel free to point out any other errors/areas where things could be improved (as long as it doesn't get too complicated) :)

Thanks in advance!
line 35 and 36: you are trying to get input twice. Also, array indexes start at 0 so you have to decrease imcheck by one:
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 <string>

using namespace std;

const int maxmonsters = 3, minmonsters = 1;
const string smonsternames[maxmonsters] = {"Gargoyle", "Minotaur", "Rabbit"};

void menuchoice()
{
    cout <<"Which monster would you like to fight?\n"
    << "1) Gargoyle\n"
    << "2) Minotaur\n"
    << "3) Rabbit\n";
}

int gargoylefight(int imcheck)
{
    int ixp = 50;
    cout << "You're battling the " << smonsternames[imcheck] << "\n";
    return (ixp);
}


int main()
{
    int ixp = 0;
    bool bqcheck = 1;
    int imcheck;
     cout << "1 to start, 0 to quit: ";
      cin >> bqcheck;
      if (bqcheck == 0)
      return 0;
      menuchoice();
     // cin >> imcheck; // you don't need this
      if (!(cin >> imcheck) || imcheck < minmonsters || imcheck > maxmonsters)
      cout << "Not eligible number";
      if (imcheck == 1)
      gargoylefight(imcheck-1);
      //else if (imcheck == 2)
      //minotaurfight(imcheck-1);
      //else if (imcheck == 3)
      //lamefight(imcheck-1);
      system("pause");
      return 0;
    }
Aha, so the conditional expression inside the if acts as the actual cin >> imcheck?

Doh, I completely missed the array element... hehe

Thanks!
Last edited on
Aha, so the conditional expression inside the if acts as the actual cin >> imcheck?

Yes.
Alright, new problem.

This time i decided to experiment with separate function calls to check the players and the enemies hp and such. I've also got another function that is supposed to decrease the enemies hp, but it appears that it's not changed, even though i have referenced 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
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
void status(int php, int& ehp )
{
    cout <<"You have " << php << " hp.\n";
    cout <<"Your enemy has " << ehp << " hp.\n";
    cout <<"Hit enemy \n" << "(H)ead, (C)hest, (L)eg";
}

void hitenemy(int& ehp, char hitchoice)
{
    int pdmg;
    srand(time(NULL));
    switch (hitchoice)
    {
    case 'h':
        case 'H':
            pdmg = rand() % 5 + 1;
           cout << "You hit your enemy for " << pdmg << " damage.\n";
           ehp -= pdmg;
           break;
    case 'c':
        case 'C':
        pdmg = rand() % 4 + 2;
        cout << "You hit your enemy for " << pdmg << " damage.\n";
        ehp -= pdmg;
        break;
    case 'l':
        case 'L':
            pdmg = rand() % 3 + 2;
            cout << "You hit your enemy for " << pdmg << " damage.\n";
            ehp -= pdmg;
            break;
    default:
        cout <<"Error: invalid option.";
        break;
    }
}
        
            

int ifight(int imcheck)
{
    srand ( time(NULL) );
    char hitchoice;
    int ixp = 50;
    int edmg, ehp;
    int php = 10;
    cout << "You're battling the " << smonsternames[imcheck] << ".\n";
    if (imcheck == 0)
    {
        while ( ehp > 0 && php > 0)
        {
            edmg = rand() % 3 + 1;
            ehp = 15;
            cout << smonsternames[imcheck] << " hits you for " << edmg << " damage.\n";
            php -= edmg;
            status(php, ehp);
            cin >> hitchoice;
            hitenemy(ehp, hitchoice);
        }
        if (php < 1)
        {
            cout << "You have died.";
        }
        else if (ehp < 1)
        {
            cout <<"You have slain your enemy and gain " << ixp << " experience.";
        }
    }
    return (ixp);
}
It works for me. Maybe you're passing wrong arguments to void hitenemy(int& ehp, char hitchoice), can't tell cause you did no post your main()
Hehe, sorry. The main is in an above post... I probably should have included it in this one too though.

It's compiling fine, yeah - but the enemies hp is not decreasing :/
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
#include <iostream>
using namespace std;

void hitenemy(int& ehp, char hitchoice)
{
    int pdmg;
    srand(time(NULL));
    switch (hitchoice)
    {
    case 'h':
        case 'H':
            pdmg = rand() % 5 + 1;
           cout << "You hit your enemy for " << pdmg << " damage.\n";
           ehp -= pdmg;
           break;
    case 'c':
        case 'C':
        pdmg = rand() % 4 + 2;
        cout << "You hit your enemy for " << pdmg << " damage.\n";
        ehp -= pdmg;
        break;
    case 'l':
        case 'L':
            pdmg = rand() % 3 + 2;
            cout << "You hit your enemy for " << pdmg << " damage.\n";
            ehp -= pdmg;
            break;
    default:
        cout <<"Error: invalid option.";
        break;
    }
}


int main() {

    int enemy_hp = 100; //assuming this is the enemy's hitpoint
    char attack_type = 'c';

    cout << "HP: " << enemy_hp << endl;
    hitenemy(enemy_hp, attack_type);
    cout << "HP: " << enemy_hp << endl;
    return EXIT_SUCCESS;
}
HP: 100
You hit your enemy for 2 damage.
HP: 98
That's a good idea, but it's not currently what i'm after... I'd prefer to locate the problem as to why it's behaving as it is. :/
o.O ?
Apologies, i didn't mean to sound ungreatful... It's a nice solution, and i will use it if this gets to the point where i can't use the solution i'm looking for.

Basically, i'm looking for a solution as to why the enemies hp is not decreasing, a solution that provides me with a simple change to the code within the functions (preferably without touching main) because i must have missed something essential here...

Thanks for the help though :)
Post your whole code then.
Here it is, all in one piece.


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

using namespace std;

const int maxmonsters = 3, minmonsters = 1;
const string smonsternames[maxmonsters] = {"Gargoyle", "Minotaur", "Rabbit"};

void menuchoice()
{
    cout <<"Which monster would you like to fight?\n"
    << "1) Gargoyle\n"
    << "2) Minotaur\n"
    << "3) Rabbit\n";
}

void status(int php, int& ehp )
{
    cout <<"You have " << php << " hp.\n";
    cout <<"Your enemy has " << ehp << " hp.\n";
    cout <<"Hit enemy \n" << "(H)ead, (C)hest, (L)eg";
}

void hitenemy(int& ehp, char hitchoice)
{
    int pdmg;
    srand(time(NULL));
    switch (hitchoice)
    {
    case 'h':
        case 'H':
            pdmg = rand() % 5 + 1;
           cout << "You hit your enemy for " << pdmg << " damage.\n";
           ehp -= pdmg;
           break;
    case 'c':
        case 'C':
        pdmg = rand() % 4 + 2;
        cout << "You hit your enemy for " << pdmg << " damage.\n";
        ehp -= pdmg;
        break;
    case 'l':
        case 'L':
            pdmg = rand() % 3 + 2;
            cout << "You hit your enemy for " << pdmg << " damage.\n";
            ehp -= pdmg;
            break;
    default:
        cout <<"Error: invalid option.";
        break;
    }
}
        
            

int ifight(int imcheck)
{
    srand ( time(NULL) );
    char hitchoice;
    int ixp = 50;
    int edmg, ehp;
    int php = 10;
    cout << "You're battling the " << smonsternames[imcheck] << ".\n";
    if (imcheck == 0)
    {
        while ( ehp > 0 && php > 0)
        {
            edmg = rand() % 3 + 1;
            ehp = 15;
            cout << smonsternames[imcheck] << " hits you for " << edmg << " damage.\n";
            php -= edmg;
            status(php, ehp);
            cin >> hitchoice;
            hitenemy(ehp, hitchoice);
        }
        if (php < 1)
        {
            cout << "You have died.";
        }
        else if (ehp < 1)
        {
            cout <<"You have slain your enemy and gain " << ixp << " experience.";
        }
    }
    return (ixp);
}



int main()
{
    int ixp = 0;
    bool bqcheck = 1;
    int imcheck;
     cout << "1 or higher to start, 0 to quit: ";
      if (!(cin >> bqcheck))
      cout << "Not eligible number";
      if (bqcheck == 0)
      return 0;
      menuchoice();
      if (!(cin >> imcheck) || imcheck < minmonsters || imcheck > maxmonsters)
      cout << "Not eligible number";
      if (imcheck == 1)
      ifight(imcheck-1);
      //else if (imcheck == 2)
      //minotaurfight(imcheck);
      //else if (imcheck == 3)
      //lamefight(imcheck);
      system("pause");
      return 0;
    }
1
2
3
4
while ( ehp > 0 && php > 0) //as long as both player and enemy are alive
{
    ehp = 15;               //restore the enemy's health
}
You... You... You did NOT just do that.

my god. Thank you.

So, i guess if i ever should consider killing myself due to abnormal amounts of stupidity - this would be the most practical time.

And once again, thank you.
Topic archived. No new replies allowed.