Accumulator difficulties

The code is nowhere near finished but, my accumulator isn't working for health and boss_health and I was wondering why
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
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

int boss_attack(int health);
int player_attack(int boss_health);

int main() { 
	unsigned short int boss_health = 100;
	unsigned short int health = 100;
	string boss = "'Prepare to die.' - the boss";
	string menu = "\n What would you like to do now?";
	string choice_one = "\n1. Attack";
	string choice_two = "\n2. Defend";
	string choice_three = "\n3. Pass";
	string c_menu = "\nYour choice: ";
	string UI_MENU = menu + choice_one + choice_two + choice_three + c_menu;

	cout << "\t\tWelcome to Battle Masters.\n";
	cout << "\tYou can enter 'quit' at any time to exit the game.\n\n";
	cout << "You have 100 health. You are about to fight a boss.\n";
	cout << boss;
	boss_attack(health);
	m1:
	cout << UI_MENU;
	string answer;
	cin >> answer;
	while (health != 0 && boss_health != 0) {
		if (answer == "1" || answer == "attack" || answer == "Attack") {	
			player_attack(boss_health);
			boss_attack(health);
			goto m1;
	}
	}
	if (answer == "quit") {
		return 0;
	}
	return 0;
}

int boss_attack(int health) {

	srand(static_cast<unsigned int>(time(0)));
	int boss_norm = (rand() % 5) + 10;
	cout << "\nThe boss attacks you for: " << boss_norm << " damage";
	health = health - boss_norm;
	cout << "\nYou now have " << health << " health.";
	
	return health, boss_norm;
}
int player_attack(int boss_health) {

	srand(static_cast<unsigned int>(time(0)));
	int player_norm = (rand() % 5) + 15;
	cout << "You hit the boss for: " << player_norm;
	boss_health = boss_health - player_norm;
	cout << "\nThe boss now has: " << boss_health << " health ";
	return boss_health, player_norm;
}
Why do people post code that doesn't compile? I don't understand...
player_attack and boss_attack return an int. 'boss_health, player_norm' is not an int.
I see 'goto'. I doubt you actually need it.
boss_attack(health) does, effectively, absolutely nothing. What you might want is
health = boss_attack(health);
call srand only once at the beginning of the program. Remove the calls in boss_attack and player_attack.
Find better learning resources.
By the way, "something is not working" is very vague. The plain fact, that your Code doesn't compile with a sane C++ Compiler would be a little more specific. Posting the error messages with your code would be optimal. (After you looked for the error, and googled the error message and still couldn't solve the problem).
If you actually are interested in making this program work and I hear from you again, I'll gladly help with the rest
The thing is, is that is does compile for me so I figured it would compile for everybody and I figured it out actually but thanks for the tips :)
Last edited on
You are passing your parameters by value, meaning that the functions receive a copy of the variable fed to it and changes made to those parameters in the function are not passed on. Also, you seem to be confused about what the comma operator does.

return health, boss_norm; is functionally equivalent to return boss_norm;

Since you don't do anything with the return values, it is hard to see the point of having them anyway.

You should only seed the random number generator once per program invocation.

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
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

void boss_attack(int& health);
void player_attack(int& boss_health);

int main() 
{

    srand(static_cast<unsigned int>(time(0)));

    int boss_health = 100;
    int health = 100;

    const string boss = "'Prepare to die.' - the boss";
    const string menu = "\n What would you like to do now?";
    const string choice_one = "\n1. Attack";
    const string choice_two = "\n2. Defend";
    const string choice_three = "\n3. Pass";
    const string c_menu = "\nYour choice: ";
    const string UI_MENU = menu + choice_one + choice_two + choice_three + c_menu;

    cout << "\t\tWelcome to Battle Masters.\n";
    cout << "\tYou can enter 'quit' at any time to exit the game.\n\n";
    cout << "You have 100 health. You are about to fight a boss.\n";

    cout << boss;
    boss_attack(health);
    while (health > 0 && boss_health > 0)
    {
        cout << UI_MENU;

        string answer;
        cin >> answer;

        if (answer == "1" || answer == "attack" || answer == "Attack")
        {
            player_attack(boss_health);
            boss_attack(health);
        }
        else if (answer == "quit")
            return 0;
        else
            cout << "Invalid input.\n";
    }

    if (health > 0 && health > boss_health)
        cout << "\nYou came out ahead in this fight.\n";
    else
        cout << "\nThis is a sad state of affairs.\n";
}

void boss_attack(int& health) 
{
    int boss_norm = (rand() % 5) + 10;
    cout << "\nThe boss attacks you for: " << boss_norm << " damage";
    health -= boss_norm;
    cout << "\nYou now have " << health << " health.";
}
void player_attack(int& boss_health) 
{
    int player_norm = (rand() % 5) + 15;
    cout << "You hit the boss for: " << player_norm;
    boss_health -= player_norm;
    cout << "\nThe boss now has: " << boss_health << " health ";
}
Topic archived. No new replies allowed.