# else/if statements help

• November 2nd, 2012, 01:17 AM
s123456
else/if statements help
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.

1.void Date::addDays(int month, int day, int year)
2.{
3. _day = _day + 7;
4.
5. if(_day > 30)
6. {
7. _day = _day - 30;
8. _month = _month + 1;
9. }
10. else
11. {
12. cout << _month;
13. cout << "/";
14. cout << _day;
15. cout << "/";
16. cout << _year;
17.
18. }
19.
20. if(_month>12)
21. {
22. _month = _month - 12;
23. cout << _month;
24. cout << "/";
25. cout << _day;
26. _year = _year + 1;
27. cout << "/";
28. cout << _year;
29. }
30. else
31. {
32. cout << _month;
33. cout << "/";
34. cout << _day;
35. cout << "/";
36. cout << _year;
37. }
38.
39.}

OUTPUT:

1.When is pay day?
2.MONTH: 7
3.DAY: 3
4.YEAR: 12
5.
6.Today's date is: 11/1/127.
8.The date of payday is: 7/3/12
9.
10.Next's week's payday is: 7/10/127/10/12
11.
12.Press any key to continue . . .

OUTPUT:

1.When is pay day?
2.MONTH: 12
3.DAY: 29
4.YEAR: 12
5.
6.Today's date is: 11/1/12
7.
8.The date of payday is: 12/29/12
9.
10.Next's week's payday is: 1/6/13
11.
12.Press any key to continue . . .

OUTPUT:

**

1.When is pay day?
2.MONTH: 11
3.DAY: 29
4.YEAR: 12
5.
6.Today's date is: 11/1/12
7.
8.The date of payday is: 11/29/12
9.
10.Next's week's payday is: 12/6/12
11.
12.Press any key to continue . . .
• November 2nd, 2012, 02:00 AM
laserlight
Re: else/if statements help
I suggest that you post your code without the line numbers but well indented in [code][/code] bbcode tags.
• November 2nd, 2012, 04:55 AM
VictorN
Re: else/if statements help
Quote:

Originally Posted by s123456
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.

I agree with laserlight: your code is very hard to read/understand because is is not formatted properly. Please, read Announcement: Before you post.... carefully.

Besides, you could get much more help for your problem from the debugger than waiting the answers from us. Just set a break point at the beginning of your code, press F5, and then debug it step-by-step (using F10) observing in the debugger watch windows what happens with all your variables.
• November 2nd, 2012, 07:19 AM
GCDEF
Re: else/if statements help
I agree with the others, but here's a hint. Do all the calculations to figure out the next date, but don't output until you're done figuring out all three fields. You have four redundant output sections. It should usually be assumed that any time you're repeating code like that you're doing something wrong.
• November 2nd, 2012, 07:52 PM
s123456
Re: else/if statements help
i'm glad you are all on the same page
why didnt anyone suggest less code?
i posted the question late last night .. i was tired...it was a trivial question but i decided to ask for advice
i was too busy trying to add code ...i had way too many print statements.....
thank you
• November 2nd, 2012, 08:49 PM
GCDEF
Re: else/if statements help
Quote:

Originally Posted by s123456
i'm glad you are all on the same page
why didnt anyone suggest less code?
i posted the question late last night .. i was tired...it was a trivial question but i decided to ask for advice
i was too busy trying to add code ...i had way too many print statements.....
thank you

I did suggest less code. I said you have redundant output code and that was an indication that something was wrong. Did you not read the responses?
• November 2nd, 2012, 10:28 PM
s123456
Re: else/if statements help
@GCDEF
sorry i didnt understand your comment but i see it now
"Do all the calculations to figure out the next date, but don't output until you're done figuring out all three fields"

in short....less code.
• November 3rd, 2012, 06:06 AM
VictorN
Re: else/if statements help
Quote:

Originally Posted by s123456
i'm glad you are all on the same page
why didnt anyone suggest less code?
i posted the question late last night .. i was tired...it was a trivial question but i decided to ask for advice
i was too busy trying to add code ...

Well, you were busy and tired... But why do you think we here were not busy nor tired? :confused:

Quote:

Originally Posted by laserlight
I suggest that you post your code without the line numbers but well indented in [code][/code] bbcode tags.

Quote:

Originally Posted by VictorN
I agree with laserlight: your code is very hard to read/understand because is is not formatted properly.

And believe me: we also are busy enough to make a lot of work (like your code formatting/debugging) for you!
So, to make your and our work easier you have to post your question and code in the form to be read/understood without any additional efforts.
But instead of this (editing the OP adding code tags and doing the debugging) you again continue to complain... :eek: :thumbd:
• November 3rd, 2012, 05:06 PM
s123456
Re: else/if statements help
@VictorN
What I ment was that i was tired and frustrated so I decided to come to this forum for help....I meant I was too busy trying to add code when instead I should have made it shorter...thank you for your suggestion for the next time i post a question on how to format my code....but someone had alreadysuggested that before you.....if I wanted to use the debugger then I wouldn't come to this forum for help.....oh and if you are too busy or too tired to help me then don't.
• November 3rd, 2012, 05:14 PM
GCDEF
Re: else/if statements help
Quote:

Originally Posted by s123456
@VictorN
.if I wanted to use the debugger then I wouldn't come to this forum for help.....oh and if you are too busy or too tired to help me then don't.

Wow. This forum is not a substitute for using the debugger, and if you're going to expect free help from some seriously experienced programmers, the least you can do is follow the forum guidelines and format your code appropriately. Most of us won't even bother to try to find problems in unformatted code. Getting snippy with the people that can and will help you probably isn't a really good plan. We've all been through the learning phase and we all know it's frustrating, but don't take it out on the people trying to help.