我正在学习向量,并编写了一些代码,可以选择随机数字用于在荷兰购买彩票。但是虽然它可以运行,编译器却警告我“从'time_t'到'unsigned int'的转换,可能会丢失数据”。
有人能发现是什么原因引起的吗?在这段代码中,我甚至没有定义任何无符号整数;据我所知,默认情况下int i是有符号整数。感谢您的见解。
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
using namespace std;
void print_numbers();
string print_color();
int main() {
srand(time(NULL));
print_numbers();
string color = print_color();
cout << color << endl;
system("PAUSE");
return 0;
}
//Fill vector with 6 random integers.
//
void print_numbers() {
vector<int> lucky_num;
for (int i = 0; i < 6; i++) {
lucky_num.push_back(1 + rand() % 45);
cout << lucky_num.at(i) << endl;
}
}
//Select random color from array.
//
string print_color() {
string colors[6] = {"red", "orange", "yellow", "blue", "green", "purple"};
int i = rand()%6;
return colors[i];
}
确切的编译器信息:警告 C4244: 'argument': 从 'time_t' 转换为 'unsigned int',可能会丢失数据。第11行。