警告C4244:'argument':从'time_t'转换为'unsigned int',可能丢失数据 - C ++ [英] warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data -- C++
问题描述
我做了一个简单的程序,允许用户选择一些骰子,然后猜测结果...我发布此代码之前,但与错误的问题,所以它被删除...现在我不能有任何错误,甚至警告C4244:'参数':从'time_t'到'unsigned int'的转换,但是由于某些原因,这个警告持续弹出,我不知道如何解决它...
可能会丢失数据
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;
}
这是我想要弄清楚的,为什么我收到此警告:
警告C4244:'argument':从'time_t'转换为'unsigned int',可能丢失数据
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
推荐答案
p>这是因为在你的系统上, time_t
是比 unsigned int
更大的整数类型。
That's because on your system, time_t
is a larger integer type than unsigned int
.
-
time()
返回time_t
可能是64位整数。 -
srand()
想要一个unsigned int
这可能是一个32位整数。
time()
returns atime_t
which is probably a 64-bit integer.srand()
wants anunsigned 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) );
在这种情况下,下转(和潜在的数据丢失)使用它种子RNG。
In this case, the downcast (and potential data loss) doesn't matter since you're only using it to seed the RNG.
这篇关于警告C4244:'argument':从'time_t'转换为'unsigned int',可能丢失数据 - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!