Aug 5, 2013 at 4:08pm UTC
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;
}
Aug 5, 2013 at 11:55pm UTC
Can someone tel me the use of static_cast<unsigned int> in this code?
Aug 6, 2013 at 4:24am UTC
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.
Aug 6, 2013 at 5:33am UTC
The cast is used to suppress a warning.
Aug 6, 2013 at 10:38pm UTC
Wouldnt it be simpler to just use srand(time(0)) instead of using the static_cast thing?
Aug 6, 2013 at 11:58pm UTC
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.
Aug 7, 2013 at 3:18am UTC
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.