I know where the problem is but I can't fix it.

Hello, basically I've been given a task where a user inputs a number (seconds) and you need to calculate which date is it starting from 9 of August 1945 (Nagasagi bombing). So if you input 51 i would be 08.09.1945 00:00:51. To make it seem more challenging , my teacher has prohibited me from using <ctime> and <chrono>, so I'm doing everything with while loops (if seconds are more than 60, minutes++ and then second -=60 and so forth).The problem is with the days. I know that the while loop stops when startingDay = 32 , but if its febuary it minuses 28 and startingDay starts at 4 instead of 1. Any ideas how to fix this ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  while (startingDay > 31) {
		
		if (startingMonth == 4 || startingMonth == 6 || startingMonth == 9  || startingMonth == 11) {
			startingDay -= 30;
		}
		else if (startingMonth == 2) {

			if ((startingYear % 4 == 0 && startingYear % 100 != 0) || (startingYear % 400 == 0)) {
				startingDay -= 29;
			}
			else {
				startingDay -= 28;
			}
			
		}
		else {
			startingDay -= 31;
		}
		startingMonth++;
You should probably rethink you're approach to this. Having the hardcoded startingDay > 31 doesn't seem to be intuitive given months have different amount of days.

You can take the seconds, turn those into years and months and days, then loop through the number of years for(int i = 1945; years > 0; i++, years--) { //check if leap year }. And then you can remove/add days as dictated by the leap years you encounter.

You should try some different methods.
Topic archived. No new replies allowed.