Nov 3, 2012 at 12:27am UTC
assuming everything else works...i'm having trouble with the output...the if else statements are driving me crazy. it is outputting the date twice when i enter 7/3/12
code displays todays date....displays "payday" entered......adds 7 days to "payday" to show NEXT payday.
void Date::addDays(int month, int day, int year)
{
_day = _day + 7;
if(_day > 30)
{
_day = _day - 30;
_month = _month + 1;
}
else
{
cout << _month;
cout << "/";
cout << _day;
cout << "/";
cout << _year;
}
if(_month>12 )
{
_month = _month - 12;
cout << _month;
cout << "/";
cout << _day;
_year = _year + 1;
cout << "/";
cout << _year;
}
else if(_month < 12)
{
cout << _month;
cout << "/";
cout << _day;
cout << "/";
cout << _year;
}
}
OUTPUT:
When is pay day?
MONTH: 7
DAY: 3
YEAR: 12
Today's date is: 11/1/12
The date of payday is: 7/3/12
Next's week's payday is: 7/10/127/10/12
Press any key to continue . . .
Nov 3, 2012 at 12:37am UTC
Make sure that you only output things once. If day < 30 and month < 12 in your case, then it will do both else statments and print the date twice.
Change your function to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
void Date::addDays(int month, int day, int year)
{
_day = _day + 7;
if (_day > 30)
{
_day = _day - 30;
_month = _month + 1;
}
if (_month>12 )
{
_month = _month - 12;
_year = _year + 1;
}
cout << _month;
cout << "/" ;
cout << _day;
cout << "/" ;
cout << _year;
}
Oh also, your function uses _month, _day, _year, which I assume are member variables. The formal parameters (month, day, year) are unused.
Last edited on Nov 3, 2012 at 12:38am UTC
Nov 3, 2012 at 12:39am UTC
Step through your logic.
Is day > than 30? No, display the date.
Is month < 12? Yes, display the date.
You want to do all your calculations first. Then have one set of statements to display the date (outside any if statements).
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
BTW, your variable names don't agree with your arguments.
Last edited on Nov 3, 2012 at 12:41am UTC
Nov 3, 2012 at 12:47am UTC
Thank you Stewbond!
i know the question was trivial
but the if else staments were driving me crazy last night
you suggested less code and it worked!
i had less code but i decided to add more and it was like a web
thank you!