So for an assignment I am supposed to have a battle between a hero and creature and see who wins by generating random attacks and those attacks only work if they are less than or equal to the agility of the attacker or whatever, you will see in the program what is happening better. But my problem is that I can't seem to cin the random number and use it. This is what I did:
#include<cstdlib>
#include<time.h>
#include <iostream>
using namespace std;
int main() {
int heroHealth;
int vileMonsterHealth;
int heroStrength;
int vileMonsterStrength;
int heroAgility;
int vileMonsterAgility;
int attack;
cout << "Please enter the Health, Strength, and Agility of the Hero :";
cin >> heroHealth >> heroStrength >> heroAgility;
cout << "Please enter the Health, Strength, and Agility of the Vile Monster:";
cin >> vileMonsterHealth >> vileMonsterStrength >> vileMonsterAgility;
while (vileMonsterHealth >=0 && heroHealth >= 0) {
srand(time(NULL));
cout << (rand () % 100) << endl;
cin >> attack;
if (attack <= 10*heroAgility) {
vileMonsterAgility -= attack;
}
cout << (rand () % 100) << endl;
cin >> attack;
if (attack <= 10*vileMonsterAgility) {
heroAgility -= attack;
}
}
return 0;
}
The console outputs: (the first numbers are numbers I entered and the 54 is the random generated number)
Please enter the Health, Strength, and Agility of the Hero : 87 8 7
Please enter the Health, Strength, and Agility of the Vile Monster: 90 64 72
54
First of all please use the code embeds, this is much easier to read your code after.
Then, your program does exactly what you told him to do.
What do you want exactly? Cin a random number?! Wtf? If you generate a random number, then there's no need to cin it. attack = rand()%100;
By the way, srand(time(NULL)) does not need to be in the loop, declare it at the very beggining of your program.
If I'm not answering your question , then please be more specific ;)