c++ - 杂志订阅程序的用户定义函数

标签 c++

我正在做这个编程任务,它让我创建一个程序,根据用户输入的月份和年份处理杂志订阅的续订和取消通知。该程序的一部分侧重于重用旧代码(这是扩展开关的来源)。我使用重用代码创建的函数不是显示实际月份,而是将月份等同于一个数字(1-12,与每个月相关)。我的问题是我正在尝试创建一个单独的函数来获取数字并将其转换为月份的实际名称。这是我目前所拥有的:

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>

using namespace std;

void getMonth(char first, char second, char third, int& monthNumber);
void getYear(int& yearNumber);
void convertMonthNumber(int monthNumber, string& month);

int main()
{
    char first, second, third;
    int monthNumber = 0;
    int yearNumber = 0;
    string month;    

    cout << "Subscription Evaluation Program";
    cout << endl << endl;
    getMonth(first, second, third, monthNumber);
    getYear(yearNumber);
    convertMonthNumber(monthNumber, month);

    cout << "The current date is " << month << " " << yearNumber; //Test to see if  convertMonthNumber works

    system("PAUSE");
}

void getMonth(char first, char second, char third, int& monthNumber)
{  
    cout << "Enter first letter of the current month: ";
    cin >> first;

switch(first)
{
    case 'F':
    case 'f':
        {
             int montNumber = 2;
        }
        break;
    case 'S':
    case 's':
         {
             int monthNumber = 9;
         }
        break;
    case 'O':
    case 'o':
        {
             int monthNumber = 10;
        }
        break;
    case 'N':
    case 'n':
        {
             int monthNumber = 11;
        }     
        break;
    case 'D':
    case 'd':
        {
             int monthNumber = 12;
        }
        break;
    case 'A':
    case 'a':
    {
        cout << "Enter second character of month: ";
        cin >> second;

        switch(second)
        {
            case 'P':
            case 'p':
                {
                     int monthNumber = 4;
                }
                break;
            case 'U':
            case 'u':
                {
                     int monthNumber = 8;
                }
                break;
            default:
                cout << "Unknown Month";
                cout << endl;
          }
    }
break;
case 'J':
case 'j':
{
    cout << "Enter second character of month : ";
    cin >> second;
    switch(second)
    {
        case 'A':
        case 'a':
            {
                 int monthNumber = 1;
            }
            break;
        case 'U':
        case 'u':
            cout<<"\nEnter third character: ";
            cin >> third;
            switch(third)
            {
                case 'L':
                case 'l':
                    {
                         int monthNumber = 7;
                    }    
                    break;
                case 'N':
                case 'n':
                    {
                         int monthNumber = 6;
                    }
                    break;
                default:
                    cout << "\nUnknown Month";
            }
            break;
        default:
            cout << "\nUnknown Month";
            cout << endl;
    }
    break;
    case 'M':
    case 'm':
        cout << "Enter second and third characters: ";
        cin >> second;
        cin >> third;
        switch(second)
        {
            case 'A':
            case 'a':
                {
                    switch(third)
                    {
                        case 'R':
                        case 'r':
                            {
                                 int monthNumber = 3;
                            }     
                            break;
                        case 'Y':
                        case 'y':
                            {
                                 int monthNumber = 5;
                            }    
                            break;
                        default:
                            cout << endl << "Unknown Month";
                            cout << endl;
                    }
                }
                break;
            default:
                cout << endl << "Unknown Month";
                cout << endl;
        }
    break;
    default:
        cout << endl << "Unknown Month";
        cout << endl;
        return;
    }
}

void getYear(int& yearNumber)
{
    const int LOW_YEAR_LIMIT = 2012;
    const int HIGH_YEAR_LIMIT = 2017;

    do{
    cout << "Enter current year (4 digits): ";
    cin >> yearNumber;
    if (yearNumber < LOW_YEAR_LIMIT || yearNumber >= HIGH_YEAR_LIMIT){
        cout << endl;
        cout << "Invalid year. Please enter again.";
        cout << endl << endl;
        }
    }while (yearNumber < LOW_YEAR_LIMIT || yearNumber >= HIGH_YEAR_LIMIT);
    return;
}

void convertMonthNumber(int monthNumber, string& month)
{    
    if (monthNumber = 1)
        string month = January;
    else if (monthNumber = 2)
         string month = February;
    else if (monthNumber = 3)
         string month = March;
    else if (monthNumber = 4)
         string month = April;
    else if (monthNumber = 5)
         string month = May;
    else if (monthNumber = 6)
         string month = June;
    else if (monthNumber = 7)
         string month = July;
    else if (monthNumber = 8)
         string month = August;
    else if (monthNumber = 9)
         string month = September;
    else if (monthNumber = 10)
         string month = October;
    else if (monthNumber = 11)
         string month = November;
    else if (monthNumber = 12)
         string month = December;  
    return;
}

所以我采纳了您的一些建议并这样做了;它不会返回月份。

void convertMonthNumber(int monthNumber, string& month)
{  
    const string JANUARY = "January";
    const string FEBRUARY = "February";
    const string MARCH = "March";
    const string APRIL = "April";
    const string MAY = "May";
    const string JUNE = "June";
    const string JULY = "July";
    const string AUGUST = "August";
    const string SEPTEMBER = "September";
    const string OCTOBER = "October";
    const string NOVEMBER = "November";
    const string DECEMBER = "December";

    if (monthNumber == 1)
         month = JANUARY;
    else if (monthNumber == 2)
         month = FEBRUARY;
    else if (monthNumber == 3)
         month = MARCH;
    else if (monthNumber == 4)
         month = APRIL;
    else if (monthNumber == 5)
       month = MAY;
    else if (monthNumber == 6)
         month = JUNE;
    else if (monthNumber == 7)
         month = JULY;
    else if (monthNumber == 8)
         month = AUGUST;
    else if (monthNumber == 9)
         month = SEPTEMBER;
    else if (monthNumber == 10)
         month = OCTOBER;
    else if (monthNumber == 11)
         month = NOVEMBER;
    else if (monthNumber == 12)
         string month = DECEMBER;  
    return;
}

最佳答案

让你的函数返回一个字符串。在函数中传递一个参数(month_num)并在函数内部声明一个局部字符串(month_name)变量并计算、赋值和返回month_name。我已经给了几个月的代码,休息你可以填写

string convertMonthNUmber(int month_num)
{
    string month_name;
    if(month_num==1)
        month_name="January";
    else if(month_num==2)
        month_name="February";
    :
    :
    :
    else 
        month_name="December";

    return month_name;
}

主要是将这个函数用作

month=convertMonthNUmber(month_num);

现在月份将具有给定月份编号的相应月份名称

关于c++ - 杂志订阅程序的用户定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12904261/

相关文章:

java - c++中是否有类似java中Desktop类的类? (特定于 Windows)

c# - Process.WaitForExit 不使用 __debugbreak 触发

c++ - 在 C++ 中将指向基类的指针传递给派生类的成员函数

c++ - 更改项目以运行 VS2012 C++

java - 如何使用 swig 类型映射将 std::vector<std::pair<std::string, int>> 从 Java 返回到 C++

C++类——数组中出现频率最高的对象

c++ - 读取音频流到输出设备

c++ - WebView2 中的新 Sec-* header

c++ - 为 AVR 编译 c++0x

c++ - 在派生类中使用指令将继承更改为 Public