编写一个C++程序:输入月份数字输出对应的月份的英语单词
时间: 2024-10-28 10:10:26 浏览: 28
C++常用英语单词.docx
你可以编写一个C++程序,通过创建一个map结构存储月份数字与其英文名称的对应关系,然后从用户那里获取输入的月份数字,查找并输出相应的英文单词。下面是一个简单的示例:
```cpp
#include <iostream>
#include <map>
using namespace std;
// 创建映射关系
map<int, string> monthNames = {
{1, "January"},
{2, "February"},
{3, "March"},
{4, "April"},
{5, "May"},
{6, "June"},
{7, "July"},
{8, "August"},
{9, "September"},
{10, "October"},
{11, "November"},
{12, "December"}
};
int main() {
int inputMonth;
cout << "Enter the number of a month (1-12): ";
cin >> inputMonth;
// 检查输入是否在范围内
if (inputMonth >= 1 && inputMonth <= 12) {
map<int, string>::iterator it = monthNames.find(inputMonth);
if (it != monthNames.end()) {
cout << "The English name for that month is: " << it->second << endl;
} else {
cout << "Invalid input! Please enter a month between 1 and 12." << endl;
}
} else {
cout << "Invalid input! Please enter a month between 1 and 12." << endl;
}
return 0;
}
阅读全文