65,108
社区成员
#include <iostream>
#include <string>
using namespace std;
int main() {
// 创建一个包含月份名称的字符串数组(注意:索引0未使用,因为月份是从1开始的)
string mouth_name[13] = {"false ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
// 创建一个指向字符串数组中月份名称的指针数组
string* mouth_number[12] = {
&mouth_name[1], &mouth_name[2], &mouth_name[3], &mouth_name[4],
&mouth_name[5], &mouth_name[6], &mouth_name[7], &mouth_name[8],
&mouth_name[9], &mouth_name[10], &mouth_name[11], &mouth_name[12]
};
cout << "请输入月份数字:";
int num;
cin >> num;
// 检查输入是否在有效范围内
if (num <= 0 || num > 12) {
cout << "您输入的格式有问题,请输入1到12之间的数字。" << endl;
} else {
cout << mouth_number[num - 1]<< endl;// 使用num - 1来访问正确的月份,因为数组索引是从0开始的
}
return 0;
}
地址
cout << *mouth_number[num - 1]<< endl;