학습 벡터이며 네덜란드에서 복권 구매에 사용할 수있는 임의의 숫자를 선택하는 코드를 만들었습니다. 하지만 컴파일러는 'time_t'에서 'unsigned int, possible data loss'로 변환하는 것에 대해 경고합니다.시간에서 난수 생성기를 시드 할 때 데이터 손실 가능성에 대한 경고 메시지가 나타나는 이유는 무엇입니까?
누구든지이 문제의 원인을 발견 할 수 있습니까? 이 코드에서는 부호없는 int도 정의하지 않았습니다. int i는 기본적으로 부호있는 int입니다. 통찰력에 감사드립니다.
#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 경고 : '인수'변환 'time_t'에서 '부호 INT'로, 데이터 손실.
에서 우리가 정확한 컴파일러 메시지 및 코드 선을 모를 때 추측 정말 흥분입니다. 그러나, 네덜란드 복권에 대한 정보가 정말 :) –
경고 메시지가 당신에게 선을 준다 당신을 도와줍니다 게시물에서이 중요한 정보를 언급 했어야합니다 (전체 경고 메시지가 바람직 함). –
경고는 'srand (time (NULL));'행에 있습니다. 그게 시간에 대해서 말하는 유일한 사람이야. 앞으로 라인 번호가 실제로 도움이 될 것입니다. – RvdK