Reputation: 25
Okay, so I have this project due for class by the 17th and I was wondering if I could get a bit of help. Basically, we are suppose to create a calendar that prints based on the day the Gregorian Calendar was created. I already figured out how to do the assignment but I am having trouble with leap year.
What I am having a problem with is returning the value of offset back to the main function so it can be used in another function. Perhaps this is a bit too advanced for my skills but I feel like this should be something fairly easy to do.
To give you an idea how leap year works. After the Century Offset does it's calculation, if the year is a leap year, it's suppose to subtract 1. Or in this case decrement. For some reason, the offset will not decrement for a leap year like 2016. If really confused as to why my if statement is not working. Can someone help?
//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);
}
Upvotes: 1
Views: 342
Reputation: 25
Okay, so I solved this question by using the notes you guys gave me. I decided to use references in-order to get the integer back to the main function. I've acquired another problem however, I will post another question to solve this.
//Declaring Function prototypes int dayone(int year, int *offset, int *leapyear);
//Function calls
//This function determines what day the Calendar starts on int dayone(int year, int *offset, int *leapyear){ int c, y, z, s, d;
c = year / 100;
y = year % 100;
z = y / 4;
s = c + y + z;
d = s % 7;
*offset = d;
*leapyear = 0;
if(year % 4 == 0 || year % 400 == 0 && year % 100 != 0)
{
*offset--;
*leapyear++;
}
return *offset, *leapyear;
}
//This is in int main(), didn't want to post the rest of the code since I've done a lot since then.
dayone(year, &offset, &leapyear);
That's what I came up with. I'm sorry if my question was a little confusing. Sometimes I have a hard time explaining what I want to do with a code.
Upvotes: 0
Reputation: 5222
Mainnote: Your Leap year logic is incorrect, it should be
year % 4 == 0 && year % 100 != 0 || year % 400 == 0
You have 2 options to return a value:
1) Use a return value
In this case You have to change the signature of your method:
int CenturyOffset( int year)
And in the body of this function You have to write:
return offset;
Then in the body of your main function you use it as following:
int offset = CenturyOffset(year);
2) Return by reference (some people know it is an out value)
In this case You have to change the signature of your method:
void CenturyOffset( int year, int& offset)
And in the body of the function there is no change.
Then in the body of your main function you use it as following:
int offset = 0; // always should initialize a value before use
CenturyOffset(year, offset);
...
Upvotes: 0
Reputation: 1396
If you'd like to retain the value of offset
when CenturyOffset(...)
exits, change the function signature from:
void CenturyOffset(int year, int offset);
to
void CenturyOffset(int year, int& offset);
This way, offset
is a reference.
Other links:
You may also return the value instead of having it passed as a parameter:
int CenturyOffset(int year) {
int offset;
// ...
return offset
}
in main:
int offset = CenturyOffset(year);
// ...
Upvotes: 3
Reputation: 3214
Without modifying anything else, you can change your function signature to:
void CenturyOffset(int year, int& offset)
This passes a reference which will then modify the variable passed in from main
, rather than a copy.
However, I would recommend changing the function to use a return:
int CenturyOffset(int year){
int c, y, z, s, d, offset;
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--;
return offset;
}
Now, you can get the value of offset
in main
:
int offset = CenturyOffset(year);
cout << offset << endl;
OtherFunction(offset);
Upvotes: 1