Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;获取当前日期输出结果_C++ - Fatal编程技术网

C++ C++;获取当前日期输出结果

C++ C++;获取当前日期输出结果,c++,C++,我的输出无法读取,我只想将日期、月份、年份指定为字符串 如果我现在就可以->tm_year+1900,它可以工作,但是如果我把它分配给一个字符串,以后可以输出,就像这样。如何更改代码,以便在不丢失以后的引用的情况下将值分配给字符串 终端: Date is 我的代码: int main() { //get today date string year,month,day; /* Output 6 month frame for appointment booking*/ time_t t

我的输出无法读取,我只想将日期、月份、年份指定为字符串

如果我现在就可以->tm_year+1900,它可以工作,但是如果我把它分配给一个字符串,以后可以输出,就像这样。如何更改代码,以便在不丢失以后的引用的情况下将值分配给字符串

终端:

Date is 
我的代码:

int main()
{
//get today date
string year,month,day;

/* Output 6 month frame for appointment booking*/
 time_t t = time(0);   // get time now
 struct tm * now = localtime( & t );

year = now->tm_year + 1900;
month = now->tm_mon + 1;
day = now->tm_mday;

cout << "Date is " << year << month << day << endl;

return 0;
}
intmain()
{
//今日约会
串年、月、日;
/*输出预约的6个月框架*/
time_t=time(0);//立即获取时间
struct tm*now=localtime(&t);
年份=现在->tm_年份+1900;
月份=现在->周一+一;
白天=现在->白天;

cout您不能将整数指定给字符串(当然,您可以,但它并不能完全满足您的期望);您必须首先转换它们:

std::string year = std::to_string(now->tm_year + 1900);
其他选项包括将数字存储为整数;您也可以打印整数:

int year = now->tm_year + 1900;

您可以在代码文件中写入
int
而不是
string

好的,我现在明白了。我使用的数据类型错误,使用int修复了问题。谢谢。如果此答案对您有帮助,请标记您的问题已回答。