Jump to content

To fix, or not to fix this 'warning'.... how to?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
14 replies to this topic

#1
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
I'm getting this warning

Quote

1> warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data



for this code chunk

int genRandom()

{

    int r;

    srand(time(NULL));

    r = rand() % 100 + 1;

    

    return r;

}


that int is assigned, why is it telling me this nonsense?

#2
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
It is referring to the time() function inside the srand() function.

srand() takes the argument int, time() returns a variable of type_t. I don't understand enough about what you are doing or why you use these functions, but if you want this to occur, you may need to explicitly cast the variable.

srand(static_cast<int>time(NULL));

You may need () brackets round the time function return you are casting. This is just to let the compiler know you are aware that it is casting a variable to type int and this is the behaviour you want to occur.

#3
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Just use a normal cast.
srand((int)time(NULL));

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

Hawk1 said:

I'm getting this warning





for this code chunk

int genRandom()

{

    int r;

    srand(time(NULL));

    r = rand() % 100 + 1;

    

    return r;

}


that int is assigned, why is it telling me this nonsense?
Don't call srand with every rand call, it doesn't do what you think it does.
Question 13.17

#5
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
thanks! ill have to try that

#6
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts

Aereshaa said:

Just use a normal cast.
srand((int)time(NULL));
That is not a "normal" cast in C++! That is a C-style cast.. As you of course know. But C-style casts in C++ are "not encouraged and commonly looked down upon".
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#7
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Because you're jealous of our beautiful syntax! :p

#8
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
while you guys are in the thread....... how would i turn say, this.....
char newgame = 'y';
into a Toupper that actually works
or is that not the right context where to put the toupper

maybe here?
 cout << "Do you want to play again? (y / n)" << endl;

            cin >> newgame;


#9
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Incorrect. There is more than one cast in c++. Being specific is very useful. It also makes you aware of the behaviour that is occurring or atleast a point of reference. If you don't know that the compiler is making assumptions on your behalf, it limits your ability to fully understand how the language behaves. Which is very useful for newer programmers like myself :D. C++ is a different language. Backwards compatibility is not a purity in a language, it just makes it easier for conversion and popularity. The more impure a language is, the more inconsistant it is!

#10
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Regarding casts, I'd estimate that 90% are misused by folks who apply them to silence a warning rather than write the code correctly.

#11
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts

Aereshaa said:

Because you're jealous of our beautiful syntax! :p
lol I just wanted to point it out :P

Hawk1 said:

while you guys are in the thread....... how would i turn say, this.....
char newgame = 'y';
into a Toupper that actually works
or is that not the right context where to put the toupper

maybe here?
 cout << "Do you want to play again? (y / n)" << endl;
            cin >> newgame;
You could put it after this...
 cout << "Do you want to play again? (y / n)" << endl;
            cin >> newgame;
For reference on the function: toupper - Cprogramming C and C++ Function Reference
toupper - C++ Reference

dcs said:

Regarding casts, I'd estimate that 90% are misused by folks who apply them to silence a warning rather than write the code correctly.
lol yeah
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#12
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
peace_and_quiet = static_cast<silence>(dcs);