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().
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.
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.
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
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.
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.