else/if statements help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: else/if statements help

  1. #1
    Join Date
    Oct 2012
    Posts
    8

    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 . . .

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,247

    Re: else/if statements help

    I suggest that you post your code without the line numbers but well indented in [code][/code] bbcode tags.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,289

    Re: else/if statements help

    Quote Originally Posted by s123456 View Post
    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.
    Victor Nijegorodov

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,054

    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.

  5. #5
    Join Date
    Oct 2012
    Posts
    8

    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

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,054

    Re: else/if statements help

    Quote Originally Posted by s123456 View Post
    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?

  7. #7
    Join Date
    Oct 2012
    Posts
    8

    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.

  8. #8
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,289

    Re: else/if statements help

    Quote Originally Posted by s123456 View Post
    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?

    As we already pointed out:
    Quote Originally Posted by laserlight View Post
    I suggest that you post your code without the line numbers but well indented in [code][/code] bbcode tags.
    Quote Originally Posted by VictorN View Post
    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...
    Victor Nijegorodov

  9. #9
    Join Date
    Oct 2012
    Posts
    8

    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.

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,054

    Re: else/if statements help

    Quote Originally Posted by s123456 View Post
    @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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center