0
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>

int main()
{
    int iRandD1 = 0;
    int iRandD2 = 0;
    int totalD = 0;
    srand(time(iRandD1));

    srand(time(iRandD2));

    //use random numbers to assign values to each die

    iRandD1 = (rand() % 6) + 1;
    iRandD2 = (rand() % 6) + 1;

    if ((iRandD1 + iRandD2) == 7 || (iRandD1 + iRandD2) == 11)
    {
        printf("player wins\n");
    }

    else 
    {
        printf("%d", totalD);
    }

}

There is my code for a dice game that predicts the sum of the two dice when rolled. I include the ctype.h library for the srand(time()), I include but this error keeps coming up.

/home/ubuntu/workspace/fooling/ $ make dice
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow    dice.c  -lcs50 -lm -o dice
dice.c:10:11: error: implicit declaration of function 'time' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    srand(time(iRandD1));
          ^
1 error generated.
make: *** [dice] Error 1

any help with the solution?

1

cs50.h includes itself stdlib.h, but in C99 you have to include it yourself too.

srand() is defined in stdlib.h

time() is defined in time.h.

The links are to cppreference.com.

Simply

#include <stdlib.h>
#include <time.h>

Although if you fix that, you will get another error, because that's not the way to use time().

If you want to seed rand() with srand(), do

srand(time(NULL));

and you don't have to do it for every integer you want to generate.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .