好吧,所以我有这个项目要在17日上课,我想知道是否能得到一些帮助。基本上,我们假设创建一个日历,该日历根据公历创建的日期进行打印。我已经想出了如何做作业,但leap年却遇到了麻烦。

我遇到的问题是将offset的值返回给main函数,以便可以在另一个函数中使用它。也许这对于我的技能来说有点太先进了,但是我觉得这应该很容易做到。

让您了解leap年的工作方式。在Century Offset进行计算之后,如果年份是a年,则应减去1。或者在这种情况下为减量。由于某种原因,偏移量不会在像2016年这样的leap年减少。如果真的对为什么我的if语句不起作用感到困惑。有人可以帮忙吗?

//Write a program that will print a calendar for whatever year the user wants.

//Declaring libraries...some I might not need....
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

//Function prototypes
void ComputeCalendar(string [], string [], int, int);
void CenturyOffset(int year, int offset);
const int DAYS_OF_THE_WEEK = 7;
const int MONTHS_OF_THE_YEAR = 12;

//function calls

//Note: Use switch to calculate what day the calendar begins on

//Determines what day that calendar begins on
void CenturyOffset(int year, int offset){
    int c, y, z, s, d;

    c = year / 100;
    y = year % 100;
    z = y / 4;
    s = c + y + z;
    d = s % 7;
    offset = d;
    if (year % 4 == 0 && year % 400 == 0)
        offset--;
    cout << offset << endl;
    }

//function for creating the calendar
void ComputeCalendar(string months[], string days[], int year, int offset) {

    cout << "\n" << setw(50) << year << "\n" << endl;
    int cal;
    int week;
        for(cal=0; cal<MONTHS_OF_THE_YEAR; cal++)
            {
                cout << "\n" << endl;
                cout << setw(3) << months[cal] << " " << year << endl;
                cout << "\n" << endl;
                    for(week=0; week<DAYS_OF_THE_WEEK; week++)
                        {
                            cout << setw(5) << days[week];
                        }
                cout << endl;
            }
        }
//Main function that allows the user to input the year they wish to see
int main(int argc, char*argv[])
{
    int year;
    int offset;
    cout << "What year would you like to see the calendar for? \n" << endl;
    cin >> year;
    cout << "\n" << endl;

        while(year < 1754) //Check if the year was before or after 1754
            {
            cout << "You may not enter a year that is before 1754. \n" << endl;
            cout << "Please enter another year." << endl;
            cin >> year;
            cout << "\n" << endl;
            }
        string days[DAYS_OF_THE_WEEK] = {"SU","MO","TU","WE","TH","FR","SA"};
//Stores days of the week in an array
        string months[MONTHS_OF_THE_YEAR] = {"January", "February", "March",
                                  "April", "May", "June",
                                  "July", "August", "September",
                                  "October", "November", "December"};
//Stores Months of the year in an array
        ComputeCalendar(months, days, year, offset);
        CenturyOffset(year, offset);
}

最佳答案

如果您想在offset退出时保留CenturyOffset(...)的值,请从以下位置更改函数签名:

void CenturyOffset(int year, int offset);


void CenturyOffset(int year, int& offset);

这样,offsetreference

其他连结:
  • Reference (C++)
  • http://en.cppreference.com/w/cpp/language/reference


  • 您也可以返回值,而不是将其作为参数传递:
    int CenturyOffset(int year) {
       int offset;
       // ...
       return offset
    }
    

    在主要方面:
    int offset = CenturyOffset(year);
    // ...
    

    关于c++ - C++日历(无法获取变量的值以返回到主函数),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45044028/

    10-13 06:58