用c++编写一个子函数实现月份输出
时间: 2023-08-12 12:05:05 浏览: 99
以下是一个简单的C++程序,用于根据用户输入的数字输出对应的月份:
```c++
#include <iostream>
using namespace std;
void printMonth(int month) {
switch (month) {
case 1:
cout << "January" << endl;
break;
case 2:
cout << "February" << endl;
break;
case 3:
cout << "March" << endl;
break;
case 4:
cout << "April" << endl;
break;
case 5:
cout << "May" << endl;
break;
case 6:
cout << "June" << endl;
break;
case 7:
cout << "July" << endl;
break;
case 8:
cout << "August" << endl;
break;
case 9:
cout << "September" << endl;
break;
case 10:
cout << "October" << endl;
break;
case 11:
cout << "November" << endl;
break;
case 12:
cout << "December" << endl;
break;
default:
cout << "Invalid input" << endl;
break;
}
}
int main() {
int month;
cout << "Enter a number between 1 and 12: ";
cin >> month;
printMonth(month);
return 0;
}
```
在这个程序中,`printMonth()` 函数接受一个整数参数 `month`,并使用 `switch` 语句将其转换为对应的字符串。主函数从用户获取输入,并调用 `printMonth()` 函数以输出月份。
阅读全文