### Calendar problems

I can get my calendar to get most days correct, except February leap years. It's something with my computeOffset. Anyone see the problem?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173`` ``````#include #include using namespace std; int getMonth() { int month; cout << "Enter a month number: "; cin >> month; while ( month < 1 || month > 12) { cout << "Month must be between 1 and 12.\n" << "Enter a month number: "; cin >> month; } return month; } int getYear() { int year; cout << "Enter year: "; cin >> year; while ( year < 1753) { cout << "Year must be 1753 or later.\n" << "Enter year: "; cin >> year; } return year; } bool isLeapYear(int year, int month) { if ((year % 4 == 0 && year % 100 != 0 && month != 1) || (year % 400 == 0)) return true; else return false; } int computeNumberOfDays(int month, int year) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) return 31; else if (month == 2 && isLeapYear(year, month)) return 29; else if (month == 2) return 28; else if (month == 4 || month == 6 || month == 9 || month == 11) return 30; } /********************************************************************** * Will prompt for and save the offset ***********************************************************************/ int computeOffset(int month, int year) { int offset = 0; int count = year - 1753; int addYear = year; for (int addYear = 0; addYear < count; addYear++) { offset = (offset + 365 + isLeapYear(year, month)) % 7; year--; } for (int addMonth = 1; addMonth < month; addMonth++) offset = (offset + computeNumberOfDays(addMonth, addYear)) % 7; return offset; } void displayOffset(int offset) { if (offset == 0) cout << setw(8) << "1"; else if (offset == 1) cout << setw(12) << "1"; else if (offset == 2) cout << setw(16) << "1"; else if (offset == 3) cout << setw(20) << "1"; else if (offset == 4) cout << setw(24) << "1"; else if (offset == 5) cout << setw(28) << "1"; } void displayCalendar(int numberOfDays, int offset, int month, int year) { int spaces = 0; int date = 1; cout << endl; if (month == 1) cout << "January"; else if (month == 2) cout << "February"; else if (month == 3) cout << "March"; else if (month == 4) cout << "April"; else if (month == 5 ) cout << "May"; else if (month == 6 ) cout << "June"; else if (month == 7 ) cout << "July"; else if (month == 8 ) cout << "August"; else if (month == 9 ) cout << "September"; else if (month == 10 ) cout << "October"; else if (month == 11 ) cout << "November"; else if (month == 12 ) cout << "December"; cout << ", " << year << endl; cout << " Su Mo Tu We Th Fr Sa" << endl; while (numberOfDays >= date) { if (offset < 6) { displayOffset(offset); date = 2; spaces = (offset * 4) + 8; offset = 6; } while (spaces < 28 && (numberOfDays >= date)) { if (date < 10) cout << " " << date; else cout << " " << date; date += 1; spaces += 4; } cout << endl; spaces = 0; } } int main() { int month = getMonth(); int year = getYear(); int numberOfDays = computeNumberOfDays(month, year); int offset = computeOffset(month, year); displayCalendar(numberOfDays, offset, month, year); return 0; } ``````
Last edited on
The first thing is this:

`bool isLeapYear(int year, int month)`

A leap year does not depend on the month. It only affects the month > 1.
I had the month in there because otherwise it would affect January
It shouldn't affect the month. What you do is the following:

Q: Is it a leap year?
A: Basically yes, but it is january so no.
I tried it without month and it didn't help. Still has the same issue of it not reading February correctly, and now it also doesn't register January correctly.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960`` ``````#include #include #include bool isLeapYear(int year) { return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); } int daysInMonth(int year, int month) { if (month == 4 || month == 6 || month == 9 || month == 11) return 30; if (month == 2) return isLeapYear(year) ? 29 : 28; return 31; } // Jan 1, 1753 was a monday (offset 1) int computeOffset(int year, int month) { int a = (year - 1753) / 4; int b = (year - 1701) / 100; int c = (year - 1601) / 400; int leap_years = a - b + c; int offset = (1 + year - 1753 + leap_years) % 7; for (int m = 1; m < month; m++) offset = (offset + daysInMonth(year, m)) % 7; return offset; } void displayCalendar(int year, int month) { const char *month_names[] = { "January","February","March","April","May","June", "July","August","September","October","November","December"}; int numberOfDays = daysInMonth(year, month); int pos = computeOffset(year, month) + 1; std::cout << month_names[month-1] << ", " << year << '\n'; std::cout << " Su Mo Tu We Th Fr Sa\n"; std::cout << std::setw(pos * 4) << 1; for (int date = 2; date <= numberOfDays; ) { if (pos++ % 7 == 0) std::cout << '\n'; std::cout << std::setw(4) << date++; } std::cout << '\n'; } int main(int argc, char **argv) { if (argc != 3) { std::cerr << "Usage: mycal YEAR MONTH\n"; return 1; } int year = atoi(argv[1]), month = atoi(argv[2]); if (year < 1753) { std::cerr << "Error: YEAR must be greater than 1752\n"; return 1; } if (month < 1 || month > 12) { std::cerr << "Error: MONTH must be from 1 to 12\n"; return 1; } displayCalendar(year, month); }``````

Last edited on
I appreciate the code, but as a first semester coder I have no clue what most of that stuff means. I don't want a new code. If someone could help me figure out how to change the offset for leap year February by - 1 that's all I need. I have tried to offset -=1 while month == 2 and isLeapYear(year,month) but it doesn't seem to change anything at all.
It's just your code rewritten. isLeapYear and daysInMonth have been corrected (I may have renamed a thing or two, and I reordered the parameters putting year first). The offset calculation has been simplified and corrected and displayCalendar was simplified, too. The only novel thing is taking input from the command line so that it's easier to test. Just forget about that if you want. I think if you look at the code more closely you'll see that it's pretty simple.

What day does your calendar start on for Jan 1, 2000 ? (should be Sat)
What about Jan 1, 2020? (should be Wed)
https://www.timeanddate.com/calendar/?year=2020&country=1
I found the problem. I needed to subract one to the offest if it is a February and a leapyear, but the problem i was running into was the year was being subtracted, so when it would test the year, it was no longer a leap year.
 ``1234567891011121314151617181920`` ``````int computeOffset(int month, int year) { int offset = 0; int count = year - 1753; int addYear = year; int origYear = year; for (int addYear = 0; addYear < count; addYear++) { offset = (offset + 365 + isLeapYear(year, month)) % 7; year--; } if ( month == 2 && isLeapYear(origYear,month)) offset -= 1; for (int addMonth = 1; addMonth < month; addMonth++) offset = (offset + computeNumberOfDays(addMonth, year)) % 7; return offset; }``````
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162`` ``````* Brother Foushee, CS124 * Author: * Braxton Meyer * Summary: * This program * * * Estimated: 6.0 hrs * Actual: 0.0 hrs * The most difficult part was * * ************************************************************************/ #include #include #include #include using namespace std; char readFile(char fileName[], char board[]) { ifstream fin(fileName); if(fin.fail()) { cout << "yo this be bad" << fileName << endl; return -1; } for (int i = 0; !fin.eof(); i++) { fin >> board[i]; // if (board[i] == '0') // board[i] = ' '; } fin.close(); return 0; } void saveQuit(char board[]) { char save; cout << "What file would you like to write your board to: "; cin >> save; //for (int i = 0; i < 2; i++) //{ int i = 0; char * pboard; pboard = &board[i]; ofstream fout("save.txt", ios::trunc); fout << pboard; fout.close(); //} return; } void showValues() { return; } void editSquare(char board[]) { cout << "What are the coordinates of the square: "; char coordinates[1]; cin >> coordinates; cout << coordinates[0]; return; } void display(char board[]) { cout << "\n A B C D E F G H I\n"; int j = 0; for (int i = 0; i < 9; i++) { if ((i % 3) == 0 && i != 0) cout << " -----+-----+-----\n"; cout << i + 1 << " "; bool newRow = true; while ( j < (9 + (i * 9))) { if (newRow == false) cout << "|"; //spot 1 if (board[j] == '0') cout << " "; else cout << board[j] << " "; //spot 2 if (board[j + 1] == '0') cout << " "; else cout << board[j + 1] << " "; //spot 3 if (board[j + 2] == '0') cout << " "; else cout << board[j + 2]; newRow = false; j += 3; } cout << endl; } return; } void displayOptions() { cout << "Options:\n" << " ? Show these instructions\n" << " D Display the board\n" << " E Edit one square\n" << " S Show the possible values for a square\n" << " Q Save and Quit\n"; } int interact(char board[]) { char input; cin >> input; switch(input) { case '?': displayOptions(); break; case 'D': display(board); break; case 'E': editSquare(board); break; case 's': case 'S': break; case 'q': case 'Q': saveQuit(board); return -1; break; } return 0; } /********************************************************************** * main reads the list, determines the speed of the searches, and outputs * the results. ***********************************************************************/ int main() { char fileName[256]; cout << "Where is your board located? "; cin >> fileName; char board[81]; readFile(fileName, board); displayOptions(); display(board); int choice = 0; while (choice != -1) choice = interact(board); return 0; }``````
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174`` ``````#include #include #include using namespace std; /********************************************************************** * This will take the users input ***********************************************************************/ void askQuestion(char prompt[], char response[]) { prompt[0] = '\t'; prompt[1] = toupper(prompt[1]); for (int i = 2; prompt[i]; i++) { if ( prompt[i] == '_') prompt[i] = ' '; else prompt[i] = tolower(prompt[i]); } cout << prompt << ": "; for (int i = 0; i < 80; i++) { response[i] = '\0'; } cin.getline(response,80); } /********************************************************************** * This function will read a file ***********************************************************************/ float readFile(char fileName[], char story[][80]) { ifstream fin(fileName); if (fin.fail()) { cout << "Error reading file " << "\"" << fileName << "\"" << endl; return -1; } int i = 0; for (i = 0; !fin.eof(); i++) { fin >> story[i]; if ( story[i][0] == ':' && isalpha(story[i][1])) { char response[80]; askQuestion(story[i], response); for (int j = 0; j < 80; j++) story[i][j] = response[j]; } } story[i][0] = ' '; fin.close(); return i; } /********************************************************************** * This will get the name of the file ***********************************************************************/ void getFile(char fileName[]) { cout << "Please enter the filename of the Mad Lib: "; cin >> fileName; cin.ignore(); } /********************************************************************** * Will replace special characters with correct punctuation ***********************************************************************/ void replacePunctuation(char story[][80]) { for (int i = 0; story[i][0] != ' '; i++) { if ( story[i][0] == ':') { if (story[i][1] == '!') { story[i][0] = '\n'; story[i][1] = '\0'; } else if (story[i][1] == '<') { story[i][0] = '<'; story[i][1] = '\0'; } else if (story[i][1] == '>') { story[i][0] = '>'; story[i][1] = '\0'; } else if (story[i][1] == '.') { story[i][0] = '.'; story[i][1] = '\0'; } else if (story[i][1] == ',') { story[i][0] = ','; story[i][1] = '\0'; } } } } /********************************************************************** * This will display the story onto the screen ***********************************************************************/ void display(char story[][80], int numWords) { for (int i = 0; i < numWords; i++) { char current = *story[i]; char next = *story[i + 1]; bool addSpace = true; cout << story[i]; { if ( next == '\n' || next == '.' || next == ',' || next == '>' || next == ' ') addSpace = false; else if (current == '\n' || current == ' ' || current == '<' || (current == '.' && !isalpha(next))) addSpace = false; else addSpace = true; } { if ( addSpace == true) cout << " "; } } cout << endl; } /********************************************************************** * Will ask for input and then will put input into a story ***********************************************************************/ int main() { char fileName[256]; char story[32][80]; char answer; bool playAgain = false; do { getFile(fileName); int numWords = readFile(fileName, story); replacePunctuation(story); cout << endl; display(story, numWords); cout << "Do you want to play again (y/n)? "; cin >> answer; if ( answer == 'y') playAgain = true; else playAgain = false; } while (playAgain); cout << "Thank you for playing." << endl; return 0; } ``````
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330`` ``````#include #include #include using namespace std; /********************************************************************** * Gets the file name ***********************************************************************/ void getFile(char fileName[]) { cout << "Where is your board located? "; cin >> fileName; } /********************************************************************** * Takes information from a file and turns it into an array ***********************************************************************/ int readFile(char fileName[], char board[][10]) { ifstream fin(fileName); if (fin.fail()) { cout << "Unable to read file: " << fileName << endl; return -1; } for (int k = 0; k < 9; k++) { for (int j = 0; j < 9; j++) fin >> board[k][j]; } fin.close(); return 0; } /********************************************************************** * Places the sudoku board onto the screen ***********************************************************************/ void display(char board[][10]) { cout << " A B C D E F G H I" << endl; for ( int i = 0; i < 9; i++) { if (( i % 3) == 0 && i != 0) cout << " -----+-----+-----\n"; cout << i + 1 << " "; for (int j = 0; j < 9; j++) { if (board[i][j] == '0') cout << " "; else cout << board[i][j]; if (j == 2 || j == 5) cout << "|"; if (j != 8 && j != 2 && j != 5) cout << " "; } cout << endl; } } /********************************************************************** * This will check the valid numbers for a certain grid location ***********************************************************************/ char validNumbers(char board[][10], char square[], char guess) { int letter = square[0] - 65; int number = square[1] - 49; char validRow[9]; char validCol[9]; char validSquare[9]; bool valid = true; for (int i = 0; i < 9; i++) { if (guess == board[number][i]) { valid = false; } if (guess == board[i][letter]) { valid = false; } } for (int j = 0; j < 3; j++) { for (int i = 0; i < 3; i++) { if (guess == board[(number / 3) * 3 + i ][letter / 3 * 3 + j]) { valid = false; } } } return valid; } /********************************************************************** * Asks for a square and a value to put in it ***********************************************************************/ int editSquare(char board[][10]) { char guess; bool empty = false; bool valid = true; char square[2]; char validNumber[9]; char boardLetter; char boardNumber; cout << "What are the coordinates of the square: "; cin >> square; if (isalpha(square[0])) square[0] = toupper(square[0]); else { boardLetter = square[1]; boardNumber = square[0]; square[0] = boardLetter; square[1] = boardNumber; square[0] = toupper(square[0]); } int letter = square[0] - 65; int number = square[1] - 49; if (board[number][(letter)] == '0') empty = true; if (empty == true) { cout << "What is the value at '" << square[0] << square[1] << "\': "; cin >> guess; } else { cout << "ERROR: Square " << "\'" << square[0]; cout << square[1] << "\' is filled\n"; } valid = validNumbers(board, square, guess); if (empty == true && valid == false ) { cout << "ERROR: Value \'" << guess << "\' in square \'" << square[0] << square[1] << "\' is invalid\n"; } else if (empty == true && valid == true) board[number][letter] = guess; } /********************************************************************** * Saves the board into a file ***********************************************************************/ void saveQuit(char board[][10]) { char newFile[256]; cout << "What file would you like to write your board to: "; cin >> newFile; ofstream fout(newFile, ios::trunc); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { fout << board[i][j] << " "; } fout << endl; } fout.close(); cout << "Board written successfully\n"; } /********************************************************************** * Display options ***********************************************************************/ void options() { cout << "Options:" << endl; cout << " ? Show these instructions" << endl; cout << " D Display the board" << endl; cout << " E Edit one square" << endl; cout << " S Show the possible values for a square" << endl; cout << " Q Save and Quit" << endl; cout << endl; } /********************************************************************** * Displays the valid numbers for a square ***********************************************************************/ void showValidNumbers(char board[][10]) { char square[2]; int count = 0; char validNumber[9]; bool valid = false; char boardNumber; char boardLetter; cout << "What are the coordinates of the square: "; cin >> square; if (isalpha(square[0])) square[0] = toupper(square[0]); else { boardLetter = square[1]; boardNumber = square[0]; square[0] = boardLetter; square[1] = boardNumber; square[0] = toupper(square[0]); } int letter = square[0] - 65; int number = square[1] - 49; cout << "The possible values for \'" << square[0] << square[1] << "\' are: "; for (int i = 1; i < 10; i++) { char guess = (char)(i + '0'); valid = validNumbers(board, square, guess); if (valid == true) { count++; } } for (int j = 1; j < 10; j++) { char guess = (char)(j + '0'); valid = validNumbers(board, square, guess); if (valid == true) { switch (count) { case 1: cout << j; break; default: cout << j << ", "; count--; } } } } /********************************************************************** * Allows for user input to interact with the game ***********************************************************************/ int interact(char board[][10]) { char interact; char input; cout << "> "; cin >> interact; switch (interact) { case '?': options(); break; case 'D': case 'd': display(board); break; case 'E': case 'e': editSquare(board); break; case 'S': case 's': showValidNumbers(board); cout << endl; break; case 'Q': case 'q': saveQuit(board); return -1; break; } return 0; } /********************************************************************** * Allows a player to play sudoku ***********************************************************************/ int main() { char fileName[256]; char board[10][10]; int input; getFile(fileName); readFile(fileName, board); options(); display(board); while (input != -1) { cout << endl; input = interact(board); } return 0; }``````
You just seem to be dumping copies of your code into this thread, without asking any questions or mentioning any problems.

Why?
Topic archived. No new replies allowed.