I got a problem with my code. How would i generate 2 'random' integers?
1 2 3 4 5 6
do{
srand(time(NULL));
int Attack = rand() % 10 + 1;
int Heal = rand() % 10 + 1;
int dragonAttack = rand() % 20 + 1; // How would i make this integer different from the other 2?
cout << "Attack - Heal" << endl;
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
srand(time(NULL)); //srand outside the loop
int i = 1;
do
{
int attack = rand() % 10+1; //Set the random number as usual
int heal = rand() % 10+1; //Set the random number as usual
int dragAtk = rand() % 20 + 1; //Set the random number as usual
cout << "Attack: " << attack << endl;
cout << "Heal: " << heal << endl;
cout << "Dragon Attack: " << dragAtk << endl;
i++;
cout << "\n" << endl;
}while(i < 3);
return 0;
}
The problem is that the srand should be inside the main() function. It can't be outside. Call it at the beginning of your main() function. Also for the condition of the while loop it should be: while(dragonHP > 0 || playerHP > 0);
This is because you want to end the loop when ONE of the HP reaches zero. If you put while(dragonHP != 0 && playerHP != 0); then it will keep looping until BOTH of them are EXACTLY zero. Say for example you have 5 HP left and the dragon hits you with a 10. You will have -5 HP and the loop won't end.
One more thing, it should be if(choice == "Attack" || choice == "attack"). You never know if the user types in ALL lower case.
Thank you! I totally forgot about || and the lowercase. However.
When i attack the dragon, the dragon will redirect the same damage at me and i'll end up with something like this http://prntscr.com/2e2xkk. I want the dragons attack to be randomized seperately. How would i do that?