Calendar-- Loop Design

This has been irritating me I couldn't really find a solution. The top row seems to shift than the other row. I really don't know how to fix it! \


#include <iostream>
#include <iomanip>
using namespace std;

int displaytable(int numDays, int offset)
{
// getting the header
cout << " Su Mo Tu We Th Fr Sa\n ";

if (offset == 6)
offset = 0;
else
offset++;
for (int i = 0; i < offset; i++)
cout << setw(4) << endl;
for ( int day = 1; day <= numDays; day++)
{ cout << setw(4) << day;

if ((day + offset) % 7 == 0)
cout << endl;
}

}



int main()
{
// get the number of days
int numDays;
cout << "Number of days: ";
cin >> numDays;

// get the offset number
int offset;
cout << "Offset: ";
cin >> offset;

int display(displaytable(numDays, offset));
return 0;
}

A.OUT
Number of days: 31
Offset: 3
Su Mo Tu We Th Fr Sa
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 31
[evargasl@LinuxLab01 ~]$
Last edited on
The a.out is a bit off here but it if you put the code in. It will show you!
sorry its actually this one.
#include <iostream>
#include <iomanip>
using namespace std;

int displaytable(int numDays, int offset)
{
// getting the header
cout << " Su Mo Tu We Th Fr Sa\n ";

if (offset == 6)
offset = 0;
else
offset++;
for (int i = 0; i < offset; i++)
cout << setw(4) << " ";
for ( int day = 1; day <= numDays; day++)
{ cout << setw(4) << day;

if ((day + offset) % 7 == 0)
cout << endl;
}

}



int main()
{
// get the number of days
int numDays;
cout << "Number of days: ";
cin >> numDays;

// get the offset number
int offset;
cout << "Offset: ";
cin >> offset;

int display(displaytable(numDays, offset));
return 0;
}

Last edited on
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
31
32
33
34
35
#include <iostream>
#include <iomanip>

void displaytable( int numDays, int offset )
{
    // getting the header
    std::cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";

    offset %= 7 ; // sanity check (defensive)
    if (offset == 6) offset = 0;

    for( int i = 0 ; i < offset; ++i ) std::cout << std::setw(4) << ' ' ;

    for( int day = 1; day <= numDays; ++day )
    {
        std::cout << std::setw(4) << day;

        if ( ++offset%7 == 0 ) std::cout << '\n' ;
    }
}

int main()
{
    // get the number of days
    int numDays = 0 ;
    std::cout << "Number of days: ";
    std::cin >> numDays;

    // get the offset number
    int offset = 0 ;
    std::cout << "Offset: ";
    std::cin >> offset;

    if( numDays > 0 && offset >= 0 ) displaytable( numDays, offset ) ;
}
Letting the standard library <ctime> do all the heavy lifting:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <ctime>

static const std::string month_name[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" } ;

int get_month() ; // Jan = 0, ... Dec = 11
int get_year() ; // 1971 - 2999
int offset( int year, int month ) ; // Sun == 0
int days_in_month( int year, int month ) ;
void display_month( int ndays, int offset ) ;

int main()
{
    std::cout << "program to display calendar for a month\n\n" ;
    const int month = get_month() ;
    const int year = get_year() ;

    std::cout << "\n\n\n             " << month_name[month] << ' ' << year << "\n\n" ;
    display_month( days_in_month(year,month), offset(year,month) ) ;
}

int get_month() // Jan = 0, ... Dec = 11
{
    std::cout << "enter month [ " ;
    for( const auto& m : month_name ) std::cout << m << ' ' ;
    std::cout << "]: " ;

    std::string month ;
    std::cin >> month ;
    if( month.size() > 2 ) 
    {
        month = month.substr(0,3) ; // we are interested in the first three characters
        month[0] = std::toupper( month[0] ) ; // make the first character upper case 
        for( int i : { 1, 2 } ) month[i] = std::tolower( month[i] ) ; // and the rest lower case

        for( int i = 0 ; i < 12 ; ++i ) if( month_name[i] == month ) return i ; // found the month 
    }

    std::cout << "invalid month. try again\n" ;
    return get_month() ; // try again
}

int get_year() // 1971 - 2999
{
    int year ;
    std::cout << "enter year [1971-2999]: " ; // unix time, 64-bit
    if( std::cin >> year && year > 1971 && year < 3'000 ) return year ;

    std::cout << "invalid year. try again\n" ;
    // clear a possible failed state, then extract and discard the offending input 
    std::cin.clear() ; // http://en.cppreference.com/w/cpp/io/basic_ios/clear
    std::cin.ignore( 1000, '\n' ) ; // http://en.cppreference.com/w/cpp/io/basic_istream/ignore

    return get_year() ; // try again
}

int offset( int year, int month ) // Sun == 0
{
    // http://en.cppreference.com/w/cpp/chrono/c/tm
    std::tm calendar_time {} ; // initialise to all zeroes
    calendar_time.tm_mday = 1 ;
    calendar_time.tm_mon = month ;
    calendar_time.tm_year = year - 1900 ;

    // http://en.cppreference.com/w/cpp/chrono/c/mktime
    const auto t = std::mktime( std::addressof(calendar_time) ) ;
    // http://en.cppreference.com/w/cpp/chrono/c/localtime
    return std::localtime( std::addressof(t) )->tm_wday ;
}

int days_in_month( int year, int month )
{
    std::tm calendar_time {} ; // initialise to all zeroes
    calendar_time.tm_mday = 28 ; // smallest number of days in any month
    calendar_time.tm_mon = month ;
    calendar_time.tm_year = year - 1900 ;

    std::time_t t ;
    int last_day ;
    do // bump tm_mday till month rolls over
    {
        last_day = calendar_time.tm_mday ;
        ++calendar_time.tm_mday ;
        t = std::mktime( std::addressof(calendar_time) ) ;
    }
    while( std::localtime( std::addressof(t) )->tm_mon == month ) ;

    return last_day ; // return the tm_mday just before first of next month
}

void display_month( int ndays, int offset )
{
    std::cout << "  Sun  Mon  Tue  Wed  Thu  Fri  Sat\n"
                 "------------------------------------\n" ;

    offset %= 7 ; // sanity check (defensive)
    if (offset == 6) offset = 0;

    for( int i = 0 ; i < offset; ++i ) std::cout << std::setw(5) << ' ' ;

    for( int day = 1; day <= ndays; ++day )
    {
        std::cout << std::setw(5) << day;
        if ( ++offset%7 == 0 ) std::cout << '\n' ;
    }

    if( offset%7 ) std::cout << '\n' ;
    std::cout << "------------------------------------\n" ;
} 

http://coliru.stacked-crooked.com/a/11e6091dd64cd9cd
Last edited on
Topic archived. No new replies allowed.