Link to home
Start Free TrialLog in
Avatar of baronpaki
baronpaki

asked on

Making a Calendar

my assignment is to make a calendar program

-program asks for year
-asks for day of january 1st (0 = sunday, 1 = monday, etc)
-asks for a filename so that all output is thrown into a file

i wanna try and make this program so that the main function isn't a large one (my teacher said a function should never be more then a page long?) - so far i have not thought of a way to accomplish this.

so far, ive gottten it to output and all, but the formatting is terrible.. you'll see if you run the program
can someone help me:
1. make the program output the calendar properly
2. devise a way to make the code cleaner.. with use of multiple functions and what not.

here she is:
[code]
#include <iostream.h>
#include <fstream.h>

int main()
{
      
      int year, jan_day;
      char filename[10];

      cout << "Enter the Year: ";
      cin >> year;

      cout << "Enter the day of week that January 1st is on (0 = sunday, 1 = monday, etc): ";
      cin >> jan_day;

      cout << "Enter the name of the file you want created (example: cal2003.txt): ";
      cin >> filename;

      ofstream file (filename); // create output buffer called 'file' to be used in place of cout. directs all content into 'filename' inputed by the user


      int dim = 31; // days in month
      int a = 1;
      int montharray[12]={'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'}; //array to store months
      montharray[a] = 1;


      file << montharray[1] << endl;
      file << "Sun\tMon\tTue\tWed\tThr\tFri\tSat" << endl;
      for(int b=0; b < jan_day; b++) // b is number of empty tabs
      {
            file << " \t";
      }

      int c = 0; // variable for containing value of j when it is saturday
      int d = 0; // variable for tabs for each new month

      if (jan_day == 0)
      {
            for(int j = 1; j <= dim; j++) // j is each date (ie. 1, 2, 3, 4 ... 28, 29, 30, 31)
            {
                  if ((j%7 == 1) || (j == 8))
                  {
               file << endl;
                  }
            file << j << "\t";
            }
      }

      if (jan_day != 0)
      {
            for(int j = 1; j <= dim; j++)
            {
                  if ((b - 1 + j == 7) && (j != 8)) // endl when j is saturday
                  {
                        c = j; // assign c the saturday value (can't assign j becaues it is constantly incrementing)
                        file << endl;
                  }
             
                  if (c == j - 7 || c == j - 14 || c == j - 21 || c == j - 28) // endl on multiples of 7 from c
                  {
                        file << endl;
                  }

                  file << j << "\t";
            }
      }

      file << endl;
      
      for (a=1; a < 12; a++) // cycling through the montharray
      {
            // d is number of tabs for next month
            if (jan_day == 0)
            {
                  d = 3;
            }
            else if (jan_day == 1)
            {
                  d = 4;
            }
            else if (jan_day == 2)
            {
                  d = 5;
            }
            else if (jan_day == 3)
            {
                  d = 6;
            }
            else if (jan_day == 4)
            {
                  d = 0;
            }
            else if (jan_day == 5)
            {
                  d = 1;
            }
            else
            {
                  d = 2;
            }



            if (montharray[a] == 'jan' || montharray[a] == 'mar' || montharray[a] == 'may' || montharray[a] == 'jul' ||
                  montharray[a] == 'aug' || montharray[a] == 'oct' || montharray[a] == 'dec')
            {
                  dim = 31; // months with 31 days
            }
            else if (montharray[a] == 'apr' || montharray[a] == 'jun' || montharray[a] == 'sep' || montharray[a] == 'nov')
            {
                  dim = 30; // months with 30 days
            }      
            else
            {
                  dim = 28; // february
                  if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
                  {
                        dim = 29; // compensate for leap year..
                  }
            }

            d = jan_day + 2;
            file << endl;
            file << montharray[a] << endl;
            file << "Sun\tMon\tTue\tWed\tThr\tFri\tSat" << endl;

            for (int e = 0; e <= d; e++)
            {
                  file << "\t";
            }
            for(int j = 1; j <= dim; j++)
            {
                  if(j % 7 == 0)
                  {
               file << endl;
                  }
            file << j << "\t";
            }
            file << endl;
      }
      
    return 0;
}
[/code]

i could use some help =/
ASKER CERTIFIED SOLUTION
Avatar of snehanshu
snehanshu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_1001466
Member_2_1001466

In
int month_array[12] = {'jan', ...}
you assing character constants to an int. Did you wanted to have an enum here?
enum months {jan = 1, feb, mar, apr};

These enums can be compared to an int like
if (a == jan).

In C/C++ array subscripts start with 0. To get the first month you want
month_array[0]

Use variable names which are meaningful: with d I can associated a day but why do you not use day or iDay.
For a I don't get to month. I would more guess its for year (anno).

To get the code shorter try a loop which can print out any month. Call this 12 times from a for loop and just have jan_day initialised for the first call. The rest has to be set in the inner loop(s)

Avatar of baronpaki

ASKER

thank you snehanshu for all your help!  i do have some questionns though

you said set Tab_Count = jan_day;
and then start my loop at:
1-Tab_Count

but wouldnt that yield negative numbers if jan_day was greater then 2?
or maybe i didnt understand you correctly.

here is my code updated from what youve given me.. am i going on the right track or did i misinterpret you completely?

#include <iostream>
#include <fstream>

using namespace std;


int main()
{
      
      int year, jan_day;
      char filename[10];
      int Month_Ctr, Month_Days, Tab_Count, Week_Count, Day_Ctr;

      cout << "Enter the Year: ";
      cin >> year;

      cout << "Enter the day of week that January 1st is on (0 = sunday, 1 = monday, etc): ";
      cin >> jan_day;

      cout << "Enter the name of the file you want created (example: cal2003.txt): ";
      cin >> filename;

      ofstream file (filename); // create output buffer called 'file' to be used in place of cout. directs all content into 'filename' inputed by the user

      int dim = 31; // days in month
      char montharray[12][10]={{"January"}, {"February"}, {"March"}, {"April"}, {"May"}, {"June"}, {"July"}, {"August"}, {"September"}, {"October"}, {"November"}, {"December"}};

      Tab_Count = jan_day;

      for (Month_Ctr = 0; Month_Ctr < 12; Month_Ctr++) // cycling through the montharray
      {
            if (montharray[Month_Ctr] == "January" || montharray[Month_Ctr] == "March" || montharray[Month_Ctr] == "April" || montharray[Month_Ctr] == "May" ||
                  montharray[Month_Ctr] == "August" || montharray[Month_Ctr] == "October" || montharray[Month_Ctr] == "December")
            {
                  Month_Days = 31; // months with 31 days
            }
            else if (montharray[Month_Ctr] == "April" || montharray[Month_Ctr] == "June" || montharray[Month_Ctr] == "September" || montharray[Month_Ctr] == "November")
            {
                  Month_Days = 30; // months with 30 days
            }      
            else
            {
                  Month_Days = 28; // february
                  if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
                  {
                        Month_Days = 29; // compensate for leap year..
                  }
            }

        file << endl;
        file << montharray[Month_Ctr] << std::endl;
        file << "Sun\tMon\tTue\tWed\tThr\tFri\tSat" << endl;

            Day_Ctr = 1-Tab_Count;

            for(Day_Ctr; Day_Ctr <= Month_Days; Day_Ctr++)
            {
                  if (Day_Ctr < 1)
                  {
                        file << " \t";
                  }
                  else
                  {
                        file << Day_Ctr << "\t";
                        for (Week_Count = 1; Week_Count <=7; Week_Count++)
                        {
                              if (Week_Count == 7)
                              {
                                    file << endl;
                              }
                        }
                  }

                  Tab_Count = 7 - Week_Count;
            }

            file << endl;
      }
      
    return 0;
}

baronpaki,
  Nice to see that you got close. A good attempt.
Ok, here are the comments:
1) Yes, Tab_Count = jan_day; will result in ative  numbers.  You CAN use negative numbers in the for loop. Your if (Day_Ctr < 1) will ensure that nothing is printed for these and give you the desired result.
2) When calculating Month_Days, you cannot do this:
 montharray[Month_Ctr] == "January"
 (Strings cannot be compared by ==, you need strcmp function for that)
  You could just replace the comparisons by
 (Month_Ctr == 0) || (Month_Ctr == 2) and so on.
3) You've got the Week_Count concept wrong.
  You don't need
for (Week_Count = 1; Week_Count <=7; Week_Count++)
at all. Initialize Week_Count outside the Day_Ctr loop, keep incrementing it for each day, and when Week_Count becomes 7, set its value back to 1. That way, at the end of the  Day_Ctr loop, Week_Count would give you the proper value for your rightly placed statement Tab_Count = 7 - Week_Count.

You are fairly close and I hope this would get your job gone :)
Good luck,
...Snehanshu
ok i fixed it to read

Tab_Count = jan_day;
      Week_Count = 0;
      for (Month_Ctr = 0; Month_Ctr < 12; Month_Ctr++) // cycling through the montharray
      {

            file << endl;
        file << montharray[Month_Ctr] << std::endl;
        file << "Sun\tMon\tTue\tWed\tThr\tFri\tSat" << endl;

            if (montharray[Month_Ctr] == "January" || montharray[Month_Ctr] == "March" || montharray[Month_Ctr] == "April" || montharray[Month_Ctr] == "May" ||
                  montharray[Month_Ctr] == "August" || montharray[Month_Ctr] == "October" || montharray[Month_Ctr] == "December")
            {
                  Month_Days = 31; // months with 31 days
            }
            else if (montharray[Month_Ctr] == "April" || montharray[Month_Ctr] == "June" || montharray[Month_Ctr] == "September" || montharray[Month_Ctr] == "November")
            {
                  Month_Days = 30; // months with 30 days
            }      
            else
            {
                  Month_Days = 28; // february
                  if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
                  {
                        Month_Days = 29; // compensate for leap year..
                  }
            }

            Day_Ctr = 1-Tab_Count;

            for(Day_Ctr; Day_Ctr <= Month_Days; Day_Ctr++)
            {
                  if (Day_Ctr < 1)
                  {
                        file << " \t";
                  }
                  else
                  {
                        file << Day_Ctr << "\t";
                  }

                  if (Day_Ctr % 7 == 0)
                  {
                        file << endl;
                        Week_Count++;
                  }
            }

            Tab_Count = 7 - Week_Count;
            file << endl;
      }


like that and it works a lot better.
now, it doesnt display the correct number of days in each month and starts skipping days altogether.
there's something wrong with the way im checking how many days are in each month...
also the jan_day value isn't taken into effect, it always starts on a sunday.

can you see anything off the bat that i am doing wrongly?
oh wow i didnt see that you posted before i posted my last message! thx so much, im going to give it a shot with what you just said
Just a few more guidelines:
          Day_Ctr = 1-Tab_Count;
          //initialize Week_Count here
          for(Day_Ctr; Day_Ctr <= Month_Days; Day_Ctr++)
          {
               if (Day_Ctr < 1)
               {
                    file << " \t";
               }
               else
               {
                    file << Day_Ctr << "\t";
               }
               //increment Week_Count here: its not the number of weeks, tis the number of days in the current week: Sorry for the wrong naming

               if (Day_Ctr % 7 == 0)//Check Week_Count here not Day_Ctr
               {
                    file << endl;
                    Week_Count++;//Reset it to 0
               }
          }
Cheers!
...Shu
oh MAN! shu im so close to finishing this thing. i know ive got it all down right -  the only problem is with my tabbing for the next month. its off for some reason

here's what ive got:
      for (Month_Ctr = 0; Month_Ctr < 12; Month_Ctr++) // cycling through the montharray
      {

            file << endl;
        file << montharray[Month_Ctr] << std::endl;
        file << "Sun\tMon\tTue\tWed\tThr\tFri\tSat" << endl;

            if (Month_Ctr == 0 ||Month_Ctr == 2 || Month_Ctr == 4 || Month_Ctr == 6 || Month_Ctr == 7 || Month_Ctr == 9 || Month_Ctr == 11)
            {
                  Month_Days = 31; // months with 31 days
            }
            else if (Month_Ctr == 3 || Month_Ctr == 5 || Month_Ctr == 8 || Month_Ctr == 10)
            {
                  Month_Days = 30; // months with 30 days
            }      
            else
            {
                  Month_Days = 28; // february
                  if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
                  {
                        Month_Days = 29; // compensate for leap year..
                  }
            }

            Day_Ctr = 1-Tab_Count;
            Week_Count = 0;

            for(Day_Ctr; Day_Ctr <= Month_Days; Day_Ctr++)
            {
                  if (Day_Ctr < 1)
                  {
                        file << " \t";
                  }
                  else
                  {
                        file << Day_Ctr << "\t";
                  }

                  Week_Count++;

                  if (Week_Count % 7 == 0)
                  {
                        file << endl;
                        Week_Count = 0;
                  }
            }

            Tab_Count = 7 - Week_Count;
            file << endl;
      }


o, just out of curiousity, are you "shu" from www.tutorialforums.com, too?


Its good to see you getting close. I just hope you did learn and understand the concepts involved.
You start Week_Count to 0 and then check
(Week_Count % 7 == 0)
The trick lies in a correcting (Week_Count % 7 == 0)
If you know what you are doing, you'll be able to correct this :-)

Well, https://www.experts-exchange.com is my only cyber home: I am not the shu from anywhere else ;-)

Cheers!
...Shu
OK, I am an Idiot. I didn't read the question well. Ignore my previous comment.
The problem actually lies here:
Tab_Count = 7 - Week_Count;
Just think over it: at the end of the Day_Ctr loop, what would be the value of Week_Count if the last day of that month is Monday? And what should it actually be? How would you correct that?

...Shu
OK, Hold on: I am writing all these comments without actually compiling your code.
Let me get my hands on a C compiler and I'll get back to you.
Sorry for the confusion.
In the mean time, try analyzing the code and output to see what could have gone wrong and where. It would be good for you if you figured it out yourself.

...Shu
okok it helps me to see things written down..

if jan_day = 6 then
the last day of the month is a monday..
then week_count is 2 (1 for sunday, 1 for monday)
tab_count is going to be 5 which places me at friday... i need to be at tuesday which would require only 2 tabs
so im overtabbing by 3?

however simply subtracting 3 isn't fixing the solution
.. ill keep at it, thx for your help shu

----
btw how long did it take you to get to the level you are at? i think its pretty amazing that you can look at my (original code) and quickly reduce that sucker to a  simple for loop.
it seems i need to develop my logic skills a bit
SHU!
i got itHE HEHEHEHEHEHEHE
man im so happy

i need to do
Tab_Count = Week_Count
in order for it to work.

thx so much, man.

i wish i had like 5 billion points to hand over to you, sadly i only have 45.
thanks once again
dang iwish i could edit my comments so i dont keep posting like this
but i did want to say

that yeah, i did understand thoroughly everything you showed me..
EXCEPT for:

std::endl;


that made no sense to me.
Baron,
  Its great to see you solve it yourself.
  I re-read my explanation and I was wrong on this part. Good you figured it out yourself. Trust me: I just finished compiling the code and was about to post it.

  std::endl;
  Is what you require in Visual Studio 2003 .net. Don't be bothered by that.
>> btw how long did it take you to get to the level you are at?
  My level isn't special: I am a learner.
  Its just that when I first wrote a 100 line program thinking its "the best", my teacher showed me how it could have been done in 10 lines. From then on, I tried applying the same concept to similar situations and also thought what my teacher could have said if she were there. That's the key and I am sure you'll get beyond my level soon: I now program in another language.
  45 points is great. We are not supposed to do homework, just guide. So, if you've learn't a few things from this, I'd be really excited.

Good luck,
...Snehanshu
just out of curiousity, what is the other language you program in now?