gpt4 book ai didi

c++ - 试图 Cout 函数的返回

转载 作者:太空宇宙 更新时间:2023-11-04 16:21:13 33 4
gpt4 key购买 nike

我是 C++ 的新手,我在使用这段代码时遇到了问题:

string output_date(int day, int month, int year){
string date;
if ((day > 0 && day <= 30) && (month > 0 && month <= 12) && (year >= 2013)){
switch (month){
case 1: date = day + " JAN " + year; break;
case 2: date = day + " FEB " + year; break;
case 3: date = day + " MAR " + year; break;
case 4: date = day + " APR " + year; break;
case 5: date = day + " MAY " + year; break;
case 6: date = day + " JUN " + year; break;
case 7: date = day + " JUL " + year; break;
case 8: date = day + " AUG " + year; break;
case 9: date = day + " SEP " + year; break;
case 10: date = day + " OCT " + year; break;
case 11: date = day + " NOV " + year; break;
case 12: date = day + " DEC " + year; break;
}
}
return date;
}

当我尝试做的时候:

cout << output_date(22,12,2013);

什么也没有。我做错了什么?

最佳答案

我建议使用 stringstream 并从流中返回一个字符串:

stringstream date;
if ((day > 0 && day <= 30) && (month > 0 && month <= 12) && (year >= 2013)){
switch (month){
case 1: date << day << " JAN " << year; break;
case 2: date << day << " FEB " << year; break;
//yadda yadda.....
}
}
return date.str();

为此,您需要包含 header <sstream>

关于c++ - 试图 Cout 函数的返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16655961/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com