-10
如果我输入5,我希望它显示“五月” 但他们显示的是始终警告,我怎么能使它工作?编译器会警告字符串转换 - 是否因为双引号?
#include <iostream>
using namespace std;
int main() {
int month;
char *m_name[] = {" ", "Jan", "Feb", "Mar", "Apr", "May",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
cout << "Enter month: ";
cin >> month;
if (month >= 1 && month <= 12)
cout << m_name[month];
else cout << "Illegal month";
return 0;
}
编译器警告:
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
^
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
请不要发布代码图片,而是复制代码并将其粘贴到问题中。使用[正确的格式](http://stackoverflow.com/help/formatting),您的代码将具有语法高亮显示,并且我们也可以复制代码,以便我们自己测试它。另外,请给我们您从程序中获得的* exact *输出,以及您期望它的输出。 –
只是为了回答标题(而不是别的):不,双引号对于多个字符是正确的。单引号用于单个字符。 –
你的代码看起来很好。你确定这是你编译的吗? – ZDF