How can I assign a value I get from a function to an integer in c? -


i doing school assignment , don't know how proceed without knowing how asked in title. given code (by prof) run , print dice roll (1-6).

void showdice (int num) {      int diceroll;      srand(time(null));      diceroll = (rand() %6) +1;      printf("%d\n", diceroll);      } 

i know how call in main function ( showdice(0); ) , prints out. need know how int (or variable) can add total (or counter) variable.

thanks

you return it. like,

int showdice (int num) {      int diceroll;      srand(time(null));      diceroll = (rand() % 6) + 1;      printf("%d\n", diceroll);      return diceroll; } 

also, if intended num number of sides,

int showdice (int num) {      int diceroll;      srand(time(null));      diceroll = (rand() % num) + 1;      printf("%d\n", diceroll);      return diceroll; } 

finally, should call srand once (at program startup).


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -