Need Help with this mini game

I need help with this program it wont work and I don't know how to fix it can anyone help please?


// minigame
#include <iostream>
#include <ctime>

using namespace std;

int main(){
int d6 ;
int ML ;

srand(static_cast<unsigned int>(time(0)));
d6 = rand % 10+1; // Error
cout << d6;
ML = rand % 10+1; //Error
cout << ML;
if(d6 > ML){
cout << "You defeated the goblin!" << endl << endl;}
if(d6 < ML){
cout << "You have been defeated by the goblin!" << endl << endl;
}
return 0;
}
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
#include <iostream>
#include <cstdlib>  // Necessary for rand()
#include <ctime>

using namespace std;

int main(){
    int d6;
    int ML;

    srand(static_cast<unsigned int>(time(0)));
    d6 = rand() % 10 + 1; // Changed
    cout << d6;
    ML = rand() % 10 + 1; // Changed
    cout << ML;
    if (d6 > ML){
        cout << "You defeated the goblin!" << endl << endl;
    }
    if (d6 < ML){
        cout << "You have been defeated by the goblin!" << endl << endl;
    }
    if (d6 == ML)
        cout << "You and the goblin have been defeated by entropy!\n\n";
    return 0;
}
Can someone tel me the use of static_cast<unsigned int> in this code?
I believe it is used or the level of "randomness" of the numbers chosen, in your case, 1-10.

I would just use srand(time(0));

easier and simple.
The cast is used to suppress a warning.
Wouldnt it be simpler to just use srand(time(0)) instead of using the static_cast thing?
Simpler yes, but as circe said it will generate a warning.

srand expects an unsigned int, but time(0) returns type time_t, hence the static cast to resolve the type mismatch.
Thx I got it cire. I'm gonna try and make this into a simple text based RPG just to get the basics of C++ and then try and add graphics and an engine and make it into a little game.
Topic archived. No new replies allowed.