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"
我做了一个简单的程序,让用户可以选择一些骰子,然后猜测结果…我之前发布了这个代码,但是错误的问题被删除了……现在我不能在这段代码上有任何错误甚至警告,但由于某种原因,这个警告一直在弹出,我不知道如何修复它……“警告C4244:‘参数’:从‘time_t’转换为‘unsigned int’,可能丢失数据”
#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
这就是我想要弄明白的,为什么我要得到这个警告:警告C4244:“参数”:从“time_t”转换为“unsigned int”,可能丢失数据。
4 个解决方案
#1
52
That's because on your system, time_t
is a larger integer type than unsigned int
.
这是因为在您的系统中,time_t是一个较大的整数类型,而不是unsigned int。
-
time()
returns atime_t
which is probably a 64-bit integer. - time()返回一个time_t,它可能是一个64位的整数。
-
srand()
wants anunsigned int
which is probably a 32-bit integer. - srand()需要一个无符号整数,它可能是一个32位的整数。
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.
在这种情况下,downcast(和潜在的数据丢失)并不重要,因为您只使用它来播种RNG。
#2
7
This line involves an implicit cast from time_t
which time
returns to unsigned int
which srand
takes:
这条线包含了来自time_t的隐式转换,时间返回到unsigned int, srand获取:
srand ( time(NULL) );
You can make it an explicit cast instead:
你可以把它变成一个明确的cast:
srand ( static_cast<unsigned int>(time(NULL)) );
#3
1
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.
time()返回一个time_t,它可以是32或64位。srand()取一个未签名的int,它是32位。公平地说,你可能不会在意,因为它只是被用作随机化的种子。
#4
1
This line involves an implicit cast from time_t which time returns to unsigned int which srand takes:
这条线包含了来自time_t的隐式转换,时间返回到unsigned int, srand获取:
srand ( time(NULL) );
You can make it an explicit cast instead:
你可以把它变成一个明确的cast:
srand ( static_cast<unsigned int>(time(NULL)) );
#1
52
That's because on your system, time_t
is a larger integer type than unsigned int
.
这是因为在您的系统中,time_t是一个较大的整数类型,而不是unsigned int。
-
time()
returns atime_t
which is probably a 64-bit integer. - time()返回一个time_t,它可能是一个64位的整数。
-
srand()
wants anunsigned int
which is probably a 32-bit integer. - srand()需要一个无符号整数,它可能是一个32位的整数。
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.
在这种情况下,downcast(和潜在的数据丢失)并不重要,因为您只使用它来播种RNG。
#2
7
This line involves an implicit cast from time_t
which time
returns to unsigned int
which srand
takes:
这条线包含了来自time_t的隐式转换,时间返回到unsigned int, srand获取:
srand ( time(NULL) );
You can make it an explicit cast instead:
你可以把它变成一个明确的cast:
srand ( static_cast<unsigned int>(time(NULL)) );
#3
1
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.
time()返回一个time_t,它可以是32或64位。srand()取一个未签名的int,它是32位。公平地说,你可能不会在意,因为它只是被用作随机化的种子。
#4
1
This line involves an implicit cast from time_t which time returns to unsigned int which srand takes:
这条线包含了来自time_t的隐式转换,时间返回到unsigned int, srand获取:
srand ( time(NULL) );
You can make it an explicit cast instead:
你可以把它变成一个明确的cast:
srand ( static_cast<unsigned int>(time(NULL)) );