C++ 我可以比较字符*(月份)和字符串(二月)吗?如果没有,我该怎么做呢?
我希望这个程序能够正常工作,这样我就可以把任何一个月的字数转换成它的等价数字C++ 我可以比较字符*(月份)和字符串(二月)吗?如果没有,我该怎么做呢?,c++,character,C++,Character,我希望这个程序能够正常工作,这样我就可以把任何一个月的字数转换成它的等价数字 #include <iostream> using namespace std; int main() { char month[20]; int INT_month; cout << "Enter a month: (ex. January, February)"; cin >> month; // Let's say the input is Fe
#include <iostream>
using namespace std;
int main()
{
char month[20];
int INT_month;
cout << "Enter a month: (ex. January, February)";
cin >> month; // Let's say the input is February
if (month == "February")
INT_month = 2;
cout << "The month in its' integral form is " << INT_month << endl;
// INT_month = 2130567168 // WHY DOES IT DO THIS????
return 0;
}
#包括
使用名称空间std;
int main()
{
半个月[20];
国际月;
cout>month;//假设输入是二月
如果(月份=“二月”)
月内=2;
cout一种方法是创建月名的向量,并使用查找索引加1作为月号:
vector<string> months = {
"january", "february", "march", ...
};
string search = "march";
auto pos = find(months.begin(), months.end(), search);
if (pos != months.end()) {
cout << "Month number: " << distance(months.begin(), pos) + 1 << endl;
} else {
cerr << "Not found" << endl;
}
向量月={
“一月”、“二月”、“三月”。。。
};
字符串搜索=“三月”;
auto pos=find(months.begin(),months.end(),search);
如果(pos!=months.end()){
你的问题不是很清楚。你介意详细说明你的问题是什么吗?对不起。我在问题中犯了一个错误。我的意思是说“array”而不是“string”。为什么“INT_month”变量不是“2”?我的意思是你的描述应该详细说明标题。