Adding and Subtracting two periods of time

I have a homework assignment that I just can't seem to get right. I've had this problem for a while and tried to get some help from my professor and a tutor. My professor pretty much wrote me an algorithm to use so I finally have the right idea of what to do but my calculations aren't coming out right. I rewrote the algorithm myself to see if that would help me before trying to fix my code and it did. I did all the calculations out and my math and logic were right but for some reason I just can't see, when I run the program I'm getting the wrong results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>               
using namespace std;

	const int YEAR_ONE = 10;      // First time period's years.
	const int WEEK_ONE = 30;      // First time period's weeks.
	const int DAY_ONE = 5;        // Holds first time period's days.
	const int YEAR_TWO = 5;       // Second time period's years.
	const int WEEK_TWO = 20;      // Second time period's weeks.
	const int DAY_TWO = 2;        // Second time period's in days.
	const int NUM_DAYS_YR = 365;  // Number of days in a year.
	const int NUM_DAYS_WK = 7;    // Number of days in a week.
int main()
{
	int daysOne;        // Holds first period's conversion to days.
	int daysTwo;        // holds second period's conversion to days.
	int daysSum;        // Holds result of adding two periods after converting to days.
	int daysDiff;       // Holds result of subtracting two periods after converting to days.
	int yearSum;        // Holds result of conversion back to years after adding.
	int weekSum;        // Holds result of conversion back to weeks after adding.
	int remDaysOne;     // Holds result of conversion back to days after adding.
	int yearDiff;       // Holds result of conversion back to years after subtracting.
	int weekDiff;       // Holds result of conversion back to weeks after subtracting.
	int remDaysTwo;     // Holds result of conversion back to days after subtracting.

	daysOne = YEAR_ONE * NUM_DAYS_YR;              // Converts first time period's years to days. 
	daysOne = (WEEK_ONE * NUM_DAYS_YR) + daysOne;  // Adds first period's years already in days to conversion of first period's weeks to days.
	daysOne = daysOne + DAY_ONE;                   // Adds first period's years and weeks already in days to remaining first period's days.
	daysTwo = YEAR_TWO * NUM_DAYS_YR;              // Converts second time period's years to days.
	daysTwo = (WEEK_TWO * NUM_DAYS_YR) + daysTwo;  // Adds second period's years already in days to conversion of second period's weeks to days.
	daysTwo = daysTwo + DAY_TWO;                   // Adds second period's years and weeks already in days to remaining second period's days.

	daysSum = daysOne + daysTwo;                   // Adds two period's of days.
	daysDiff = daysOne - daysTwo;                  // Subtracts two period's of days.

	yearSum = daysSum / NUM_DAYS_YR;               // Converts result of adding the days back to years.
	remDaysOne = daysSum % NUM_DAYS_YR;            // Calulates remaining days after converting to years.
	weekSum = remDaysOne / NUM_DAYS_WK;            // Converts result of adding the days back to weeks.
	remDaysOne = remDaysOne % NUM_DAYS_WK;         // Calculates remaining days after converting to weeks.
	yearDiff = daysDiff / NUM_DAYS_YR;             // Converts result of subtracting the days back to years.
	remDaysTwo = daysDiff % NUM_DAYS_YR;           // Calulates remaining days after converting to years.
	weekDiff = remDaysTwo / NUM_DAYS_WK;           // Converts result of subtracting the days back to weeks.
	remDaysTwo = remDaysTwo % NUM_DAYS_WK;         // Calculates remaining days after converting to weeks.

	cout << "First Time Period = " << YEAR_ONE << " Years, " << WEEK_ONE << " Weeks and " << DAY_ONE << " Days." << endl;
	cout << "Second Time Period = " << YEAR_TWO << " Years, " << WEEK_TWO << " Weeks and " << DAY_TWO << " Days." << endl << endl;
	cout << "Addition Result = " << yearSum << " Years, " << weekSum << " Weeks and " << remDaysOne << " Days." << endl;
	cout << "Subtraction Result = " << yearDiff << " Years, " << weekDiff << " Weeks and " << remDaysTwo << " Days." << endl << endl;

    return 0;
}


This is what I'm getting for output:

First Time Period = 10 Years, 30 Weeks and 5 Days.
Second Time Period = 5 Years, 20 Weeks and 2 Days.

Addition Result = 65 Years, 1 Weeks and 0 Days.
Subtraction Result = 15 Years, 0 Weeks and 3 Days.

Press any key to continue . . .
Last edited on
Line 26 and 29 you are using NUM_DAYS_YR instead of NUM_DAYS_WK
Oh wow... I had it right on paper. Thanks so much. I wish I had an extra set of eyes. lol
Topic archived. No new replies allowed.