Good Afternoon,

This is my first class in C++ and I'm having problems with a program that needs to print a calendar of June, July and August 2008 into the output file "outCalendar.txt" . I should format the result for month well so that it looks like a real calendar table. So far I have this code.

#include <iostream>
#include <fstream>
#include <string>
#include <cmath> 
#include <cstdlib>
#include <iomanip>

using namespace std; 
int MonthDays[4]= {0,30,31,31,};
string MonthNames [4]={"Null","June","July","August"};
void printmonth( const int month, const int startday, const bool leapyear);

void showMenu(); 

int main( )
{
    printmonth(6,6,false);
    printmonth(7,0,false);
    printmonth(8,1,false);

    //file declaration

    outCalendarFile;            //output file to store the calendar

    //variable declarations

    int choice;                 //store choice value (1, 2, 0r 3)
    char option;                //store option value (y/n)



    //open the output files 
    outCalendarFile.open("Lab_4_outputCalendar.txt"); 
    while ( true )
    {
        showMenu();             //show menu
        cin
        >>choice;           //get the user's choice


        if (choice == 3 ) 
        {void printmonth( const int month, const int startday, const bool leapyear)
{
    int dayinmonth = MonthDays[month];
    int daynumber=1;
    int colCount=startday;

    cout << "         " << MonthNames[month] << endl;
    cout << endl;
    cout <<"  Sun"<<" "<<"Mon"<<" "<<"Tue"<<" "<<"Wed" <<" "<<"Thu"<<" "<<"Fri"<<" "<<"Sat"<<endl;
    cout << endl;
    cout << setw((colCount*3)+startday) << "";

    while (daynumber <= daysinmonth)
    {
        if (colCount ==8)
        {
            cout << endl;
            colCount = 1;
        }

        cout << setw(4);
        cout << daynumber;
        colCount++;
        daynumber++;
    }
    colCount = 1;
    cout << endl << endl;

        }


        cout<<"Do you want to play again (y/n)? "; 
        cin>>option; 

        if (option !='y')
        {
            cout<<"Bye bye!"<<endl<<endl;         
            break; 
        }
    }

    //close files
    outCalendarFile.close( );               //close outputCalendat.txt 




    return 0;

}
void showMenu()
{
    cout<<"************************************************\n"
        <<"Enter 1 to build a square root, log10 table     \n"
        <<"Enter 2 to play rolling two dices               \n"
        <<"Enter 3 to print a calendar for 2008            \n"
        <<"What is your choice  ==>   "; 

    return; 
}

Edited 4 Years Ago by deceptikon: Made the question stand out more by removing it from the code block

My question is how to just print a month like June 2012 only to the screen

yes I know, but I need to output the month of june the screen. So far I have this code for that section of the program


    if (choice == 3 ) 
            {
            cout<<"How many days in the month?"<<endl;
            cin>>days;
            cout<<endl;
            cout<<setw(10)<<right<<"\t\t Year: 2012"<<endl;
            cout<<setw(20)<<right<<"\t    June" <<endl;
            cout<<endl;
            cout<<setw(10)<<right<<"Sun"<<setw(5)<<right<<"Mon"<<setw(5)<<right<<"Tue"
                <<setw(5)<<right<<"Wed"<<setw(7)<<right<<"Thurs"
                <<setw(5)<<right<<"Fri"<<setw(5)<<right<<"Sat"<<endl;
            cout<<"   ------------------------------------------"<<endl;

            for (int i=1; i<=days; i++)
            {
            cout<<setw(36)<<right<<i<<endl;
            }
            }

and here is the screen that I get
Enter 3 to print a calendar for 2012
What is your choice  ==>        3
How many days in the month?
30

                 Year: 2012
                    June

       Sun  Mon  Tue  Wed  Thurs  Fri  Sat
   ------------------------------------------
                                   1
                                   2
                                   3
                                   4
                                   5
                                   6
                                   7
                                   8
                                   9
                                  10
                                  11
                                  12
                                  13
                                  14
                                  15
                                  16
                                  17
                                  18
                                  19
                                  20
                                  21
                                  22
                                  23
                                  24
                                  25
                                  26
                                  27
                                  28
                                  29
                                  30
Do you want to play again (y/n)?
I want to format the number so they can be arrange as a month of a calendar
thank you

what should I do in order for the numbers won't be to apart. I already remove the endl.

thank you

`here is the revised code
if (choice == 3 ) 
        {
        cout<<"How many days in the month?"<<endl;
        cin>>days;
        cout<<endl;
        cout<<setw(10)<<right<<"\t\t Year: 2012"<<endl;
        cout<<setw(20)<<right<<"\t    June" <<endl;
        cout<<endl;
        cout<<setw(10)<<right<<"Sun"<<setw(5)<<right<<"Mon"<<setw(5)<<right<<"Tue"
            <<setw(5)<<right<<"Wed"<<setw(7)<<right<<"Thurs"
            <<setw(5)<<right<<"Fri"<<setw(5)<<right<<"Sat"<<endl;
        cout<<"   ------------------------------------------"<<endl;
         cout<<"\t\t\t\t";
        for (int i=1; i<=days; i++)
        {

        cout<<setw(5)<<right<<i;

        }
        }
        and here is the output that I get
        `Enter 1 to build a square root, log10 table
Enter 2 to play rolling two dices

    Enter 3 to print a calendar for 2012
    What is your choice  ==>        3
    How many days in the month?
    30

                     Year: 2012
                        June

           Sun  Mon  Tue  Wed  Thurs  Fri  Sat
       ------------------------------------------
                                                                       1    2    3    4    5    6    7    8    9
    10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25
    26   27   28   29   30
    Do you want to play again (y/n)?



what is that I need to do so the number 3 start on Sun on the next row and so on
thank you

First off, you want to change how you are setting the first day of the month. Right now, you have a fixed offset for the first day, and are then using that same offset for each successive day of the month, causing them to be printed at intervals of 36 characters each. Needless to say, this isn't what you want.

Let start by coming up with a way of computing the offset for the first day, then work out the loop such that it adds an endl after the Saturday of each week.

I came with a way to offset the firstday

cout<<"\t\t\t\t";
for (int i=1; i<=days; i++)
    {
        cout<<setw(5)<<right<<i;
    }

But, I don't know how to insert an endl in a loop

Edited 4 Years Ago by deceptikon: Cleaned up code tags

You can do this without a loop by computing the offset directly:

cout<<setw(5 * first_day)<<right<<i;

Note that days is not the right value for this, as it is the total number of days in the month; what you want is the number of the offset for the first day in the week (e.g., Sunday = 0, Monday = 1, Tuesday =2, etc.). You may find it helpful to have an enumeration type for this purpose:

enum DAYS_OF_WEEK {SUN, MON, TUE, WED, THU, FRI, SAT};

As for the endl, what you want to do is add a test inside the printing loop that checks to see if the current day plus the day of the week of the first day is divisible by seven. For this purpose you'll want to use the modulo operator (%), and when it is zero, then you want to start a new line.

 // first, print the first day of the week
 cout << setw(5 * first_day) << right << '1';

for (int i = 2; i < days; i++)
{
    cout << setw(5) << right << i;

    if (((first_day + i) % 7) == 0)
    {
        cout << endl;
    }
}

HTH.

Edited 4 Years Ago by Schol-R-LEA

This question has already been answered. Start a new discussion instead.