New Programmer Trying to Output the Day of the Week
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: New Programmer Trying to Output the Day of the Week

Hybrid View

  1. #1
    Join Date
    Apr 2013
    Posts
    1

    New Programmer Trying to Output the Day of the Week

    Hi guys, I'm just starting coding and having a heck of a time trying to figure out how to make this application. It's pretty simple and I just want it to input a date, then output a day of the week. If any of you could just give me a few pointers as to where I'm going wrong, I'd greatly appreciate it!

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
        int startday = 0, sm = 1, sd = 1, sy = 2000, cm = 0, cd = 0, cy = 0;
        cout << "Please enter the current month, day, and year: ";
        cin >> cm >> cd >> cy; int curyear = cy, curmonth = cm, curday = cd;
    
    
    
    
    
        //Months added
        while (curmonth >= 1)
        {
            if (curmonth == 1 || 3 || 5 || 7 || 8 || 10 || 12) //March, May, July, August, September, December
            {
                startday = startday + 31;
            }
            else if (curmonth == 2)
            {
                //LEAP YEAR HERE, YOU!
                if ((curyear % 4 == 0) && ((curyear % 400 == 0) || (curyear / 4 != 100)))
                {
                    startday = startday + 29;
                }
                else
                {
                    startday = startday + 28;
                }
            }
            else if (curmonth == 4 || 6 || 9 || 11)
            {
                startday = startday + 30;
            }
            curmonth--;
        }
    
        //Years Added
        for (; cy < sy; cy++)
        {
            startday = startday + (365 * cy) + cd;
        }
    
        int dayname = (startday - 1) % 7;
    
        if (dayname == 6)
               cout << cm << " " << cd << " " << curyear << " was a Thursday";
        else if (dayname == 0)
               cout << cm << " " << cd << " " << curyear << " was a Friday";
        else if (dayname == 1)
               cout << cm << " " << cd << " " << curyear << " was a Saturday";
        else if (dayname == 2)
               cout << cm << " " << cd << " " << curyear << " was a Sunday";
        else if (dayname == 3)
               cout << cm << " " << cd << " " << curyear << " was a Monday";
        else if (dayname == 4)
               cout << cm << " " << cd << " " << curyear << " was a Tuesday";
        else   cout << cm << " " << cd << " " << curyear << " was a Wednesday";
    
        return 0;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,418

    Re: New Programmer Trying to Output the Day of the Week

    Quote Originally Posted by ec23 View Post
    Hi guys, I'm just starting coding and having a heck of a time trying to figure out how to make this application.
    Code:
    if (curmonth == 1 || 3 || 5 || 7 || 8 || 10 || 12)
    Explain what that line is supposed to do. Now after explaining, have you seen any code in a C++ book, tutorial, or even on this site by experienced programmers that resembles the line above? There is a reason why you won't see code like that anywhere, since it isn't correct.

    So to cut to the chase, whatever you think that line was supposed to do, doesn't do what you think it does. If you want to compare a value to multiple values, it is done by taking that value and literally comparing it to each value:
    Code:
    if ( curmonth == 1 || curmonth == 3 ||  curmonth == 5 || curmonth == 7 || curmonth == 8 || curmonth == 10 || curmonth == 12)
    For each boolean subexpression, you have to state the entire comparison. Think of it this way:
    Code:
    if ( (curmonth == 1) || (curmonth == 3) ||  (curmonth == 5) || (curmonth == 7) || (curmonth == 8) || (curmonth == 10) || (curmonth == 12) )
    Note that each subexpression is parenthesized.

    Your original attempt just took (curmonth == 1) and if not true, then went ahead and applied || to 3, which will always result in "true" or 1. The "|| 5 || 7 || 8 || 10 || 12" is thrown away by the compiler, since the boolean expression already resulted in "true", and due to || being applied, was "short-circuited".

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 3rd, 2013 at 11:35 PM.

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