Problem with Addition in my code.

Good afternoon. I am make a simple super hero battle code for fun. I have a function and the main with two int variables. The variables(superheros) get points based on strikes I programmed in. However, the variable with the most points adds correct Exp: 4+3+2 = 9. But the variable with lesser points does not exp: 2+6+9 = 269. Any Advice?

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

using namespace std;

int superBattle(int super1, int super2)
{
    if(super1>super2)
    {
        cout<<"Batman wins! "<< super1 << " to " << super2;
    }
    else if(super2>super1)
    {
        cout<<"Arrow wins! " << super2 << " to " << super1;
    }
}
int main()
{
    const int punch = 3;
    const int kick = 4;
    const int takedown = 10;
    const int knockOut = 1000;
    const int block = 1;

    int Batman = 0;
    int Arrow = 0;

    cout<<"Arrow you're out of line!\n";
    cout<<"Go Back to Gotham Bat!\n";
    cout<<"Not without you Arrow.\n";

    Batman = Batman + punch + kick + takedown + takedown;

    cout<<"Give up Arrow, and come with me.\n";
    cout<<"No way Bat! I saved your city!\n";

    Arrow = Arrow + kick + kick + takedown;

    cout<<superBattle(Batman, Arrow);

    cout<<"You don't scare me Bat!\n";
    cout<<"I SHOULD! I am done wasting time!\n";

    return 0;
}
Hint: Think about the return value of your superbattle function. Is it returning anything? What is the function signature saying its returning?
Last edited on
So... it needs to return an int. I have tried 0, and return (super1, super2), and return super1, super2. I get different answers, all have issues with the second number. The closest I got was the second number is being multiplied by 10. Updated code below. Thanks for the insight.

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

using namespace std;

int superBattle(int super1, int super2)
{
    if(super1>super2)
    {
        cout<<"Batman wins! "<< super1 << " to " << super2;
    }
    else
    {
        cout<<"Arrow wins! " << super2 << " to " << super1;
    }
            return 0;
}
int main()
{
    const int punch = 3;
    const int kick = 4;
    const int takedown = 10;
    const int knockOut = 50;
    const int block = 1;

    int Batman = 0;
    int Arrow = 0;

    cout<<"\tWe find our heroes in a dark alley that lies in the center of Starling City.\n";
    cout<<"Normally this time of night the streets are rampant with crooks, murderers, and those who seek to do harm.\n";
    cout<<"Tonight however even the scum of the city hides. Two of the greatest heroes are in a heated battle... Against each other!\n\n\n\n";



    cout<<"Arrow you're out of line!\n";
    cout<<"Go Back to Gotham Bat!\n";
    cout<<"Not without you Arrow.\n\n\n\n";

    cout<<"Batman strikes first, using he's training and experience.\n";
    cout<<"An upper cut to Arrow's chin, followed by a kick to his gut! Giving the opportunity for a leg sweep. Arrow is down.\n\n\n\n";

    Batman = Batman + punch + kick + takedown;

    cout<<"Give up Arrow, and come with me.\n";
    cout<<"No way Bat! I saved your city!\n\n\n\n";
    cout<<"But The Batman isn't the only one that has training. Arrow moves fast with a double roundhouse kick.\n";
    cout<<"Arrow sees the opportunity for a tackle and takes it. The Bat is down.\n";

    Arrow = Arrow + kick + kick + takedown + block + block;

    cout<<"As Both Heroes stand in the alley, out of breath, on this unusually hot night. The verbal battle begins.\n\n\n";

    cout<<"You had no right! yells the Bat. You killed him, In my city.\n";
    cout<<"He was going to destroy your city Bat!\n";
    cout<<"I had it under control, the toxic gas was already contained, and the bomb disarmed.\n";
    cout<<"So you wanted to let him free to do it again... is that it Bat. Keep you employed!?\n\n";

    cout<<"You think that is why I do this, uh Arrow!\n";
    cout<<"You watch your tongue Oliver!\n\n";

    cout<<"You don't scare me Bat!\n";
    cout<<"I SHOULD! I am done wasting time!\n\n\n";

    Batman = Batman +punch + punch + kick + punch;
    Arrow = Arrow + block + block + punch;

    cout<<"The Battle rages, punches and kicks are thrown and blocked. Both heroes have met there equal.\n\n";

    Arrow = Arrow + punch + kick + block + takedown;
    Batman =Batman + punch + knockOut;

    cout<<"Suddenly there is silence. One hero stands and another lies on the ground.\n";
    cout<<"The shadows hide the victor. Until the red and blue police lights cast the shadows out.\n";
    cout<<"Revealing ...\n\n";
    cout<<superBattle(Batman, Arrow);


    return 0;
}











don't call the function with cout. Just call the function.


cout<<superBattle(Batman, Arrow);

^^^^^^^^^^^^

that says "call the function, then write the result"

the result is 0, as the function is coded to return 0.

so if the function itself printed 10 and returned, it prints 10 and 0, or 100 which looks like X 10 but isn't really what is happening.

just say

superBattle(Batman, Arrow);

Last edited on
So... it needs to return an int
.

False :) It does not need to return anything, because you're simply printing statements from the function. Nothing needs to be returned to the caller (main).
Topic archived. No new replies allowed.