Numbers Appearing At End Of Program

Ok, so I'm very new to C++, just started out yesterday. I'm working on making a program that allows you to type in certain inputs. It takes the numbers you typed in, puts them into equations, then gives you an output. I looked on multiple guides online for using srand and rand, but can't seem to figure out how to fix this issue I have (which I believe is because of using the random function.) My code is below. I know it'll probably look messy, but is there anything I'm doing wrong?

When I run the program and it's a "miss," it'll show the number 4469760 the line below Miss but above the "Press any key." If I get a hit without distance, it'll show 4469760. If I get a hit with distance, it'll show up as 44697604469760. So it has to be something found in both combat() and ranged(), and the only thing I see in both is the rand().

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
  #include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

using namespace std;

      int a;//Base Strength
      int b;//Final Damage
      int w;//Weapon Bonus
      int x;//Base Defense
      int y;//Final Guard
      int u;//Armor Bonus
      int z;//Final Damage
      int j;//Distance
      int k;//Base Offense
      int r;//Range Hit Chance
      int v1;//Variable 1
      int v2;//Variable 2
      int v3;//Variable 3
      
int combat(){
    v1 = rand() %3 + 1;//Variable for attacker
    v2 = rand() %3 + 1;//Variable for defender
    b = (a*(w/100)) + a;//Calculating attacker
    y = (x*(u/100)) + x;//Calculating defender
    z = (b*v1) - (y*v2);//Determining damage dealt
    
    if (z<1)//Determines if damage is a hit or miss
       cout << "Miss" << '\n';
       else
           cout << "Damage Dealt: " << z << '\n';}
           
int ranged(){
    v3 = rand() %100 + 1;//Variable for accuracy
    r = ((100-(j*10))+k);//Calculating accuracy
    
    if (v3<=r)//Determines if you successfuly engage in combat
       cout << combat();
    else
        cout << "Miss" << '\n';}
        
int main(){
    
    cout << "Enter Base Strength: ";
    cin >> a;
    
    cout << "Enter Weapon Bonus: ";
    cin >> w;
    
    cout << "Enter Base Defense: ";
    cin >> x;
    
    cout << "Enter Armor Bonus: ";
    cin >> u;
    
    cout << "Enter Base Offense: ";
    cin >> k;
    
    cout << "Enter Distance: ";
    cin >> j;
    
    srand (time(NULL));
    
    if (j>0)
       cout << ranged() << '\n';
    else
        cout << combat() << '\n';
        
    system("pause");
    
}


Thank you for any help you guys can offer!
Last edited on
Your problem is you are cout-ing the value of the functions ranged() and combat() instead of just calling them.

Your ranged() and combat() functions appear to already be printing out the information you desire so there is no need to print again at the end of the program.

If you wanted to return the information to the cout you would need to use return in the ranged or combat function to send the value back to the main function.

If you are happy printing in the ranged and combat function you could simply swap ranged and combat to void functions (because you weren't returning a value either way) and then it won't attempt to print out the value again.

I'm not sure what prints when you cout from a function without a returned value but I know you aren't suppose to do it that way. Either return a value or make it a void function and don't print.

Here would be an example using void functions

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
 #include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

using namespace std;

      int a;//Base Strength
      int b;//Final Damage
      int w;//Weapon Bonus
      int x;//Base Defense
      int y;//Final Guard
      int u;//Armor Bonus
      int z;//Final Damage
      int j;//Distance
      int k;//Base Offense
      int r;//Range Hit Chance
      int v1;//Variable 1
      int v2;//Variable 2
      int v3;//Variable 3

void combat(){
    v1 = rand() %3 + 1;//Variable for attacker
    v2 = rand() %3 + 1;//Variable for defender
    b = (a*(w/100)) + a;//Calculating attacker
    y = (x*(u/100)) + x;//Calculating defender
    z = (b*v1) - (y*v2);//Determining damage dealt

    if (z<1)//Determines if damage is a hit or miss
       cout << "Miss" << '\n';
       else
           cout << "Damage Dealt: " << z << '\n';}

void ranged(){
    v3 = rand() %100 + 1;//Variable for accuracy
    r = ((100-(j*10))+k);//Calculating accuracy

    if (v3<=r)//Determines if you successfuly engage in combat
       combat();
    else
        cout << "Miss" << '\n';}

int main(){

    cout << "Enter Base Strength: ";
    cin >> a;

    cout << "Enter Weapon Bonus: ";
    cin >> w;

    cout << "Enter Base Defense: ";
    cin >> x;

    cout << "Enter Armor Bonus: ";
    cin >> u;

    cout << "Enter Base Offense: ";
    cin >> k;

    cout << "Enter Distance: ";
    cin >> j;

    srand (time(NULL));

    if (j>0)
       ranged();
    else
       combat();

    return 1;

}



One other thing i notice is you are doing integer division, which is fine if you understand that means any information after a decimal place is lost. IE if you did 10 / 3 the value would be 3 instead of 3.333333.

If you want to use those decimal places you would need to use a different data type like float.
Last edited on
You declare functions called combat() and void() and tell the compiler that they're each going to return an int. You don't actually return anything from these functions. Perhaps instead you should say void combat(){ and void ranged(){

When you call the functions from main, you don't have to cout the function. Instead of
1
2
3
4
    if (j>0)
       cout << ranged() << '\n';
    else
        cout << combat() << '\n';
you can say
1
2
3
4
    if (j>0)
       ranged();
    else
        combat();


If you want to make your life easier later, give your variables better names. Instead of relying on comments:
1
2
3
4
5
6
7
8
9
10
      int a;//Base Strength
      int b;//Final Damage
      int w;//Weapon Bonus
      int x;//Base Defense
      int y;//Final Guard
      int u;//Armor Bonus
      int z;//Final Damage
      int j;//Distance
      int k;//Base Offense
      int r;//Range Hit Chance 
be explicit with your variable names themselves.
1
2
3
4
5
6
7
8
9
10
      int baseStrength;
      int finalDamage;
      int weaponBonus;
      int baseDefense;
      int finalGuard;
      int armorBonus;
      int finalDamage;
      int distance;
      int baseOffense;
      int rangeHitChance;

If it seems like that's too tedious to type every time you want to use a variable, remember that your code will be read way more often than it is written. You should strive for self-documenting code.
Last edited on
Thank you very much! I saw void before online but didn't completely understand what it was used for and now I get that, too.

I wanted to use integer division because I don't want decimals to be in the hit points.

Once again, thanks a ton.

edit:
@Booradley, yeah that's probably a much better idea. I'm in the process of cleaning this up a little bit to make it easier to read and improve on. When I made this, I initially thought it would give me problems if I wrote out full words for some reason.
Last edited on
Topic archived. No new replies allowed.