i keep getting a warning that i cant seem to fix




this is the warning

conversion from 'time_t' to 'unsigned int', possible loss of data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <time.h>
#include <string>
#include<iostream>
#include <stack>

using namespace std;

string int2hex(unsigned int dec)
{
  int i = 0;
  stack <int>remainder;
  string hex, temp;
  char hexDigits[] = { "0123456789abcdef" };

  if(dec == 0)
    hex = hexDigits[0];

  while (dec != 0)
  {
    remainder.push(dec % 16);
    dec /= 16;
    ++i;
  }
  
  while (i != 0)
  {
    if (remainder.top() > 15)
    {
      temp = int2hex(remainder.top());
      hex += temp;
    }
    hex.push_back(hexDigits[remainder.top()]);
    remainder.pop();
    --i;
  }
  return hex;
}


int main()
{
 
  time_t seconds;
  seconds = time (NULL);

  unsigned int dec = seconds-(365*30+7)*24*60*60;
  
  string hex = int2hex(dec);
  cout<<"dec = "<<dec<<"  and hex = "<<hex.c_str()<<endl;
  
  cout<<endl;
// give the user a chance to look things over before quitting
  
  system("pause");

  return 0;
}




On line 46 seconds, a time_t, is being converted an unsigned int so that math operations can be done on it with the other integers in the expression. time_t is bigger than an unsigned int, so it's possible that time_t's value is not representable as an unsigned int, causing possibly erroneous output. That's what the compiler is warning you about, I would guess.
but its still ok to run
unsigned int dec = (unsigned int)seconds-(365*30+7)*24*60*60;

Easy way to get rid of those warnings, type casting.
http://www.cplusplus.com/doc/tutorial/typecasting/
thanks alot i need to remember that one to
Topic archived. No new replies allowed.