Solved

Making a Calendar

Posted on 2003-10-27
16
277 Views
Last Modified: 2010-04-02
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 =/
0
Comment
Question by:baronpaki
  • 8
  • 7
16 Comments
 
LVL 5

Accepted Solution

by:
snehanshu earned 45 total points
ID: 9632681
baronpaki,
  Since this is homework, I shall try to strike a balance between explanation and actual code.
  So, here goes:
1) Remember to use meaningful variable names: it helps in understanding code.
2) Your montharray could have been
char montharray[12][4]={{"jan"}, {"feb"}, {"mar"}, {"apr"}, {"may"}, {"jun"}, {"jul"}, {"aug"}, {"sep"}, {"oct"}, {"nov"}, {"dec"}};
3) Here are the variables that are needed for a simpler solution:
int year, jan_day;//input values
char filename[10];//input values
int Month_Ctr, Month_Days, Tab_Count, Week_Count, Day_Ctr;

the algorithm would be as follows:
a) For each month, Tab_Count would represent the number of initial tabs. Hence, for january, you would get Tab_Count from jan_day. For further months, calculate tab count (Explained later).
i.e. Tab_Count = jan_day;
b) Run a loop for the months (Month_Ctr)
i.e. for (Month_Ctr = 0; Month_Ctr < 12; Month_Ctr++)

c) write the formatting values like day headers, month name etc at the first step in your for loop
i.e.
          file << endl;
          file << montharray[Month_Ctr] << std::endl;
          file << "Sun\tMon\tTue\tWed\tThr\tFri\tSat" << endl;

d) Find the number of days in the current month "Month_Days"
CODE IS SIMILAR TO WHAT YOU HAVE ALREADY DONE FOR THIS

e) At this point, you are ready to use a "single" for loop (Day_Ctr) that would write to the file. But, Rather than starting the loop from 1 to Month_Days, you can start the loop from 1-Tab_Count to Month_Days.
In this loop, if Day_Ctr is less than 1 then write space and tab, otherwise write Day_Ctr and a tab. Also, here, you would want to insert an endl after every 7th iteration. So, maintain another counter, Week_Count for this.

f) Now, to update Tab_Count for the next month: The number of tabs you have to display nextmonth is 7 - (number of days printed from the last week of current month).
i.e.
Tab_Count = 7 - Week_Count;

I would love to add my sample code, but I think it would be better for you if you tried to figure it out yourself. Let me know if there is any difficulty in understanding the above.
Good luck,
...Snehanshu
0
 
LVL 13

Expert Comment

by:SteH
ID: 9632759
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)

0
 

Author Comment

by:baronpaki
ID: 9636207
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;
}

0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9638250
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
0
 

Author Comment

by:baronpaki
ID: 9638321
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?
0
 

Author Comment

by:baronpaki
ID: 9638327
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
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9638399
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
0
 

Author Comment

by:baronpaki
ID: 9638782
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?


0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Expert Comment

by:snehanshu
ID: 9638838
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, www.Experts-Exchange.com is my only cyber home: I am not the shu from anywhere else ;-)

Cheers!
...Shu
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9638953
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
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9638986
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
0
 

Author Comment

by:baronpaki
ID: 9639124
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
0
 

Author Comment

by:baronpaki
ID: 9639635
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
0
 

Author Comment

by:baronpaki
ID: 9639640
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.
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9639726
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
0
 

Author Comment

by:baronpaki
ID: 9639859
just out of curiousity, what is the other language you program in now?
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now