How do you loop or wrap numbers around for calendar?

Write your question here. How do i wrape numbers around in my calandar



I have been working on this program for hours and cannot find out how to make the numbers loop around after they hit saturday. They either go way passed it to the right or if i add and endl; they go up and down.

// This is how my output looks like (except they curve around they just go forever to the right:

Number of days: 31
Offset: 0
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


// This is my program, what do i need to add? please help

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


/*************************************************
* Prompt for days
**********************************************/
int GetDays()
{
int days;
cout << "Number of days: ";
cin >> days;
return days;
}

/***************************************************
* compute offset
***************************************************/
int ComputeOffSet()
{
int num = 0;
cout << "Offset: ";
cin >> num;
return num;
}

/***************************************************
* add display
*****************************************************/
void display(int days, int num)
{
cout << " Su Mo Tu We Th Fr Sa" << endl;


num = num * 3 + 3;
for (int s = 0; s < num; s++)
{
cout << " ";
}

for (int i = 1; i <= days; i++)
{
num += i;

cout << setw(4) << i;
}

cout << endl;

}
/**********************************************************************
*This will output everything
***********************************************************************/
int main()
{
int days = GetDays();
int num = ComputeOffSet();
display(days, num);
return 0;
}

Last edited on
closed account (48T7M4Gy)
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
#include <iostream>
#include <iomanip>
using namespace std;


/*************************************************
* Prompt for days
**********************************************/
int GetDays()
{
int days;
cout << "Number of days: ";
cin >> days;
return days;
}

/***************************************************
* compute offset
***************************************************/
int ComputeOffSet()
{
int num = 0;
cout << "Offset: ";
cin >> num;
return num;
}

/***************************************************
* add display
*****************************************************/
void display(int days, int num)
{
cout << " Su Mo Tu We Th Fr Sa" << endl;


num = num * 3 + 3;
for (int s = 0; s < num; s++)
{
cout << " ";
}

for (int i = 1; i <= days; i++)
{
num += i;

cout << setw(4) << i;
}

cout << endl;

}
/**********************************************************************
*This will output everything
***********************************************************************/
int main()
{
int days = GetDays();
int num = ComputeOffSet();
display(days, num);
return 0;
}

closed account (48T7M4Gy)
1
2
3
4
5
6
   string dayName[7] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" };
    
    int days = GetDays();
    
    for( int i = 0; i < days; i++)
        cout << dayName[i % 7] << " ";


This is a start. You will still need to adjust i for the offset. :-)
@keymort

thanks i really appreciate it! :)

also is there a way to do it without strings do you know? i havent really been taught that yet but and do not want to be graded down because of that, even though they make life so much easier lol.
@keymort

hey i kind of figured it out using a module

if (i%7==0)
cout << endl;

but my question is how come when i type in 7 days and 0 offset my 7 gets cut off? and other numbers dont when i type in 9 with offset 1 for example.
closed account (48T7M4Gy)
Strings are easiest, character arrays ( more particularly char*'s) another possibility but you can't get away from it and another is to 'slice' pieces out of a single "SuMoTuWeThFrSa"" string of characters. ( The magic is there's really no magic! )
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void display(int days, int num)
{
cout << " Su Mo Tu We Th Fr Sa" << endl;

for (int s = 0; s < num; s++)
{
    cout << "   ";
}

for (int i = 1; i <= days; i++)
{
    cout << setw(3) << i;
}

cout << endl;

}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void display(int days, int num)
{
for ( int j = 1; j <= (days + num) /7 + 1; j++)
cout << " Su Mo Tu We Th Fr Sa";
cout << endl;

for (int s = 0; s < num; s++)
{
    cout << "   ";
}

for (int i = 1; i <= days; i++)
{
    cout << setw(3) << i;
}

cout << endl;

}


Slightly magical.
Last edited on
@kemort

can i ask you for a huge favor please, i have been working on this all day today and i am so close, i cant figure out what i am doing wrong, my offset at zero should start on monday,

//This is my output:

a.out
Number of days: 31
Offset: 5
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

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


// This is my code:

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


/*************************************************
 * Prompt for days
 **********************************************/
int GetDays()
{
   int days;
   cout << "Number of days: ";
   cin >> days;
   return days;
}

/***************************************************
 * compute offset
 ***************************************************/
int ComputeOffSet()
{
   int num = 0;
   cout << "Offset: ";
   cin >> num;
   return num;
}

/***************************************************8
 * add display
 *****************************************************/
void display(int days, int num)
{
   cout << "  Su  Mo  Tu  We  Th  Fr  Sa" << endl;

   int we = num;

   num = num * 3 + 3;

   for (int s = 0; s < num; s++)
      {
         cout << " ";
      }

   int i = 1;

   for (i; i <= (7-we); i++)
   {
      num += i;
      if (i%(7-we)==0)
         cout << endl;
      cout << setw(4) << i;
    }


   for (int j = 0; i <= days; j++, i++)
   {
      num += j;

      if (j%7==0)
         cout << endl;
      cout << setw(4) << i;
   }


      cout << endl;

}

/*****************************************************************
 * This will output everything!
 *****************************************************************/
int main()
{
   int days = GetDays();
   int num = ComputeOffSet();
   display(days, num);
   return 0;
}

[code]
Last edited on
closed account (48T7M4Gy)
Please format your code using the <> tool on the right hand side. That way you can run the code online

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/***************************************************8
* add display
*****************************************************/
void display(int days, int num)
{
      cout << " Su Mo Tu We Th Fr Sa" << endl;

for (int s = 0; s < num; s++)
{
     cout << "   ";
}

for (int j = 1; i <= days; j++, i++)
{
cout << setw(4) << i;

if ((num + j)%7==0)
cout << endl;
}
}
Last edited on
Topic archived. No new replies allowed.