Thread: Problem with srand(time(NULL))

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Problem with srand(time(NULL))

    Hai! I want to make a Concentration game using C. But, I have a some error when I try to compile it. it says: "Conversion may lose significant digits in function main()". the error is in: "srand(time(NULL));"

    This is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
     char cYesNo = '\0';
     int iResp1 = 0;
     int iResp2 = 0;
     int iResp3 = 0;
     int iElapsedTime = 0;
     int iCurrentTime = 0;
     int iRandomNum = 0;
     int i1 = 0;
     int i2 = 0;
     int i3 = 0;
    
     srand(time(NULL));
    
      printf("\nPlay a game of Concentration? (y or n): ");
      scanf("%c", &cYesNo);
    
      if (cYesNo == 'y' || cYesNo == 'Y') {
         i1 = rand() % 100;
         i2 = rand() % 100;
         i3 = rand() % 100;
    
         printf("\nConcentrate on the next three numbers\n");
         printf("\n%d\t%d\t%d\n", i1, i2, i3);
    
         iCurrentTime = time(NULL);
         do{
              iElapsedTime = time(NULL);
          } while((iElapsedTime - iCurrentTime) < 3);//end do while loop
    
         system("clear");
    
         printf("\nEnter each # separated with one space: ");
         scanf("%d%d%d", &iResp1, &iResp2, &iResp3);
    
         if(i1 == iResp1 && i2 == iResp2 && i3 == iResp3)
             printf("\nCongratulations!\n");
        else
             printf("\nSorry, correct numbers were %d %d %d\n", i1, i2, i3); 
    
      }//end if
    }//end main function
    Can someone help me to solve this error? I'm using Turbo C++ 4.5. Thanks!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) you need to #include <time.h>

    2) you need to get a more up to date compiler... TC++ hasn't been maintained in years and has always been terribly non-standard.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Thanks, It solve my problem. And thanks for the recomendation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP with srand(time()( in Code::Blocks (C language)
    By Italikus in forum C Programming
    Replies: 4
    Last Post: 06-22-2011, 02:06 PM
  2. NULL returns every time?
    By yildizabdullah0 in forum C Programming
    Replies: 2
    Last Post: 05-26-2011, 04:29 AM
  3. srand(time()) returns segmentation fault
    By withoutn in forum C Programming
    Replies: 9
    Last Post: 07-03-2007, 01:59 PM
  4. srand(NULL)
    By Welshy in forum C++ Programming
    Replies: 3
    Last Post: 03-25-2005, 02:11 PM
  5. srand(time(NULL));
    By vin0_oce in forum C Programming
    Replies: 4
    Last Post: 09-28-2002, 08:34 PM

Tags for this Thread