Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Calendar visual c++ help

  1. Mar 21, 2008 #1
    okay so they asked us to make an entire calendar where the user inputs the first day of the month and month itself and a calendar is generated for an entire year..
    so far i've gotten to being able to generate a good thingi for a month.. and well i've used a while loop to repeat till the months get to 12... as in no matter wat month number u enter it'll get to december.. but like if u entered 4 it wudnt do months 4,5,6,7,8,9,10,11,12,1,2,3,4.. it'd do 4 to 12 only
    and i also dont get how the next month to start from where the previous month ended..
    i dont need the program thingi itself. but hints'd be nice..
    heres my code so far:

    #include<iostream.h>
    int space;
    int day;
    int date;
    int month;


    int main ()
    {
    space=0;
    cout<<"\t\t\t::CALENDAR::\n";
    cout<<"\n\nenter number of month:\t";
    cin>>month;

    cout<<"\n\nenter first day of the month:\t";
    cout<<"\n\nMonday = 0\nTuesady = 1\nWednesday = 2";
    cout<<"\nThursday = 3\nFriday = 4\nSaturday = 5";
    cout<<"\nSunday = 6\n";
    cin>>day;
    cout<<"\n\n\t\t";
    while (month<=12)
    { if (month==1)
    cout<<"JANUARY";
    else if (month==2)
    cout<<"FEBRUARY";
    else if (month==3)
    cout<<"MARCH";
    else if (month==4)
    cout<<"APRIL";
    else if (month==5)
    cout<<"MAY";
    else if (month==6)
    cout<<"JUNE";
    else if (month==7)
    cout<<"JULY";
    else if (month==8)
    cout<<"AUGUST";
    else if (month==9)
    cout<<"SEPTEMBER";
    else if (month==10)
    cout<<"OCTOBER";
    else if (month==11)
    cout<<"NOVEMBER";
    else if (month==12)
    cout<<"DECEMBER";
    else
    {cout<<"invalid month, calendar will close";
    return 0;
    }
    cout<<"\n\nMon\tTue\tWed\tThu\tFri\tSat\tSun\n";

    while (space<day)
    {cout<<"\t";
    space++;
    }

    if (month==4||month==6||month==9||month==11)
    {
    for (date=1;date<31;date++)
    {
    ++space;
    cout<<date;
    cout<<"\t";


    if (space%7==0)
    {cout<<"\n";
    }

    }
    }
    else if (month==2)
    {
    for (date=1;date<29;date++)
    {
    ++space;
    cout<<date;
    cout<<"\t";


    if (space%7==0)
    {cout<<"\n";
    }

    }
    }
    else
    {for (date=1;date<=31;date++)
    {
    ++space;
    cout<<date;
    cout<<"\t";


    if (space%7==0)
    {cout<<"\n";
    }

    }
    }

    month++;
    cout<<"\n\n";

    }
    cout<<"\n\n";
    return 0;

    }
     
  2. jcsd
  3. Mar 26, 2008 #2
    Your month-counting approach is wrong. If starting at 4 has to produce 4 through 12 and 1 through 3, then you cannot simply increment to 12 months.

    We know that regardless of what the user enters, the program has to return 12 different months, right?

    So we have to count to 12 no matter what, and still take the user input (lets say 4) into consideration. So how about this:

    for(i = 0; i < 12; i++) cout << "\nCurrent month number: " + (i + 4) % 12;

    (I haven't used c++ in years, but I think % is modulo.)

    As for the days and the names of the months, you should look into using an API for that if you are allowed to. MFC classes or the win32 API or *something*. Manipulating dates is a major pain without it.

    If you are not allowed to, I would suggest that you make some sort of structure to contain the different names of the months, the names of the days, the amount of days in each month and so on. Your code gets ugly and hard to work with if you spell it out like you are currently doing.

    You can put the data in arrays like this:

    String *NameOfMonth [] = { "January", "February" ... };
    String *DaysInMonth [] = { 31, 29, 31, 30 .. };
    String *NameOfDay [] = { "Sunday", "Monday" ..};

    Then do something like this:

    for(i = 0; i < 12; i++) cout << "\n" + NameOfMonth[(i + 4) % 12];

    Typical pitfalls if you cannot use an API: Different numbers of days in February in leapyears. And if June ends on a Friday, Juli starts on a Saturday (ie, you cannot use a simple loop to iterate through all the days of a month, you need an independent variable to keep track of the day of the week).

    Hope this helps point you in the right direction.

    k
     
  4. Mar 30, 2008 #3
    we havent done strings and stuff yet... its just very basic stuff at the momment...
    but i did the date thingi...
    i set space to 0 if space%7==0,
    and then in the end i set date to space
    and then set space to 0 again...
    so that worked out well...
    i dont get how to do the month thing... sorry... it totally escapes me...
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Calendar visual c++ help
  1. C++ and Visual C++ (Replies: 4)

  2. VB.net VS visual C (Replies: 1)

  3. New to Visual C++ (Replies: 11)

Loading...