这是我的代码:
int month()
{
//this array contains all of the months names
char months[24] = {'JANUARY', 'FEBURARY', 'MARCH ', 'APRIL', 'MAY', 'JUNE', 'JULY',
'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER', 'JAN',
'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEPT', 'OCT', 'NOV', 'DEC'};
char month_name[10]; // a null terminating char array for your month of birth
cin >> month_name;
for(int j = 0; j <= 24; j++ )
{
if(month_name == months[j])
{
return month_name;
}
else
{
cout << "\n" << endl;
show_error();
cout << "\n" << endl;
bFlag = false;
return bFlag;
}
}
}
每当我尝试运行它时,我都会收到消息警告:隐式常量转换溢出。这是什么意思?
答案 0 :(得分:2)
首先,您的月份名称应在"
引号之间,并使其成为char *
的数组
char * months [] = {“january”,“feburary”,...“12月”}
然后,不是使用==
使用_stricmp
来比较字符串,如果返回值为0
,则表示您匹配。
i
中的_stricmp
表示忽略大小写(因此通过对小写字母进行比较,将“1月”和“1月”视为等于),否则使用strcmp
_stricmp
已记录here(stricmp
在Visual Studio中已弃用)但请查看来自多路复用的评论,我不熟悉该功能,但他可能是对的!
正如Nik B.指出的,你应该总是返回一个int。当您发现月份返回其索引(j
)时。看看你现在如何添加这几个月的短名称,你可以通过使用
return j % 12;