C++ Calendar Error

I'm working on making a basic calendar program using a FOR loop cycle, I have everything correct until I work on the row alignment with dates. The dates are suppose to continue onto Sunday after the date reaches Saturday, it does it all on all of the rows but the first row. In which it keeps going until 7 digits show in the first row. I think it deal with my FOR loop at the end of void displayTable, any tips or recommendations, I've been at it for nearly 9 hours now.
note: Offset == 0 is a monday

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
#include <iostream>
#include <iomanip>
using namespace std;

int getNumDays()
 {
    int numDays;
    cout << "Number of days: ";
    cin >> numDays;

    return numDays;
 }

 int getOffset()
 {
    int offset;
    cout << "Offset: ";
    cin >> offset;

    return offset;
 }
 void displayTable(int getNumDays, int getOffset)
 {
    int days = 0;
    int calcOffset = getOffset + (7 - getOffset);

    cout << "  Su" << setw(4) << "Mo" << setw(4) << "Tu"
        << setw(4) << "We" << setw(4) << "Th" << setw(4) << "Fr"
        << setw(4) << "Sa" << endl;
    if (getOffset == 0)
       cout << setw(4) << " ";
    if (getOffset == 1)
       cout << setw(8) << " ";
    if (getOffset == 2)
       cout << setw(12) << " ";
    if (getOffset == 3)
    cout << setw(16) << " ";
    if (getOffset == 4)
       cout << setw(20) << " ";
    if (getOffset == 5)
       cout << setw(24) << " ";
    else (getOffset == 6)
       ;

    for (days = 1; days <= (getNumDays); days++)
    {
       cout << "  " << setw(2) << days;
       if (days % 7 == 0)
          cout << endl;
    }

    cout << endl;

    return;

 }

 int main()
 {
    int numDays = getNumDays();
    int offset = getOffset();

    displayTable(numDays, offset);
    return 0;
 }
Last edited on
Try:
https://rubberduckdebugging.com/


OK, from the top of your function:

void displayTable(int getNumDays, int getOffset)
From what I can see above this, getNumDays and getOffset are functions; why are you naming them as the function parameters?
Remember: explain this to your rubber duck.


int calcOffset = getOffset + (7 - getOffset);
What are you intending to do with calcOffset?
Is calcOffset ever anything other than 7?
Is calcOffset ever used?
Remember: your rubber duck wants to hear the explanation here.


1
2
3
4
5
6
7
8
9
10
11
12
if (getOffset == 0)
       cout << setw(4) << " ";
    if (getOffset == 1)
       cout << setw(8) << " ";
    if (getOffset == 2)
       cout << setw(12) << " ";
    if (getOffset == 3)
    cout << setw(16) << " ";
    if (getOffset == 4)
       cout << setw(20) << " ";
    if (getOffset == 5)
       cout << setw(24) << " ";

How about
cout << setw(4*(getOffset+1)) << " ";
(Your rubber duck is now pointing out that getOffset wasn't a very good name for a variable.)


1
2
       if (days % 7 == 0)
          cout << endl;

Well, you've told it to line feed every 7th day ... so that's precisely what it does. Tell your rubber duck what your really want to do. Go on, try for your initial-line blanks:
cout << setw(4*((getOffset+1) % 7)) << ""; // yes, really empty string
and when you come to force a newline,
if ((days+getOffset+1) % 7 == 0) cout << endl;
(Sorry, your rubber duck is squawking about that getOffset again).


https://rubberduckdebugging.com/cyberduck/
Last edited on
Thank you! Everything was solved! Probably the only error now is if the month ends on a Saturday it makes a new line that isn't needed, but I will try and look for the condition now. Thank you again.

> Number of days: 30
> Offset: 4
> 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
> \n
Exp: No output


Conditions... the only hard point for me to really understand now.

And solved.
Last edited on
Topic archived. No new replies allowed.