30

I made a simple program that allows the user to pick a number of dice then guess the outcome... I posted this code before but with the wrong question so it was deleted... now I cannot have any errors or even warnings on this code but for some reason this warning keeps popping and I have no clue how to fix it... "warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data"

#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>

using namespace std;

int  choice, dice, random;

int main(){
    string decision;
    srand ( time(NULL) );
    while(decision != "no" || decision != "No")
    {
        std::cout << "how many dice would you like to use? ";
        std::cin >> dice;
        std::cout << "guess what number was thrown: ";
        std::cin >> choice;
         for(int i=0; i<dice;i++){
            random = rand() % 6 + 1;
         }
        if( choice == random){
            std::cout << "Congratulations, you got it right! \n";
            std::cout << "Want to try again?(Yes/No) ";
            std::cin >> decision;
        } else{
            std::cout << "Sorry, the number was " << random << "... better luck next  time \n" ;
            std::cout << "Want to try again?(Yes/No) ";
            std::cin >> decision;
        }

    }
    std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    return 0;
}

This is what I am trying to figure out, why am I getting this warning: warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data

Gal Appelbaum
  • 1,905
  • 6
  • 21
  • 33

4 Answers4

70

That's because on your system, time_t is a larger integer type than unsigned int.

  • time() returns a time_t which is probably a 64-bit integer.
  • srand() wants an unsigned int which is probably a 32-bit integer.

Hence you get the warning. You can silence it with a cast:

srand ( (unsigned int)time(NULL) );

In this case, the downcast (and potential data loss) doesn't matter since you're only using it to seed the RNG.

Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • 1
    +1 this is only correct answer in its entirety (and wordings). – Nawaz Feb 12 '12 at 05:16
  • 2
    Ugh, C-style casts don't belong in C++ programs. – aib Feb 12 '12 at 05:31
  • 2
    @aib They are shorter. And they [are fine for numerical casts.](http://stackoverflow.com/a/1255015/922184) – Mysticial Feb 12 '12 at 05:35
  • Worked it like a charm for me. And I guess you could use size_t instead of unsigned int, isn't it? Thanks! – Fabiano Feb 06 '13 at 16:11
  • 2
    @Fabiano You *could*. But bare in mind that `srand()` takes `unsigned int` as a parameter. If `unsigned int` and `size_t` happen to be different sizes on your machine (as is usually the case on x64), then you will get a similar warning about potential loss of data. So it's better to stick with just `unsigned int`. – Mysticial Feb 06 '13 at 17:41
10

This line involves an implicit cast from time_t which time returns to unsigned int which srand takes:

srand ( time(NULL) );

You can make it an explicit cast instead:

srand ( static_cast<unsigned int>(time(NULL)) );
Pubby
  • 51,882
  • 13
  • 139
  • 180
2

time() returns a time_t, which can be 32 or 64 bits. srand() takes an unsigned int, which is 32 bits. To be fair, you probably won't care since it's only being used as a seed for randomization.

Community
  • 1
  • 1
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

This line involves an implicit cast from time_t which time returns to unsigned int which srand takes:

srand ( time(NULL) );

You can make it an explicit cast instead:

srand ( static_cast<unsigned int>(time(NULL)) );
fedorqui
  • 275,237
  • 103
  • 548
  • 598