Simple calender program.

I am a calender program which should look like this.
1
2
3
4
5
S  M   Tu  W   Th  F  S
   2  3   4   5   6   7
8   9  10  11 12 13 14
15 16 17 18 19 20 21
23 24 25 26 27 28

but the alignment of numbers with days is all messed up what should I do.
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
#include <iostream>
#include <iomanip>
using namespace std;

void displayTable(int numDays, int offset);
int getNumDays();
int getOffset();


/* MAIN
* Pretty much a delegator. Calls other functions.*/
int main()
{
   //Get the number of days
   int numDays = getNumDays();

   //Get the day offset
   int offset = getOffset();

   //Displays the result
   displayTable(numDays, offset);

   return 0;
}


/* DISPLAYTABLE
 * Displays the physical table to the screen.*/
void displayTable(int numDays, int offset)
{
   //Declare Variables
   int days;
   int test;

   //List the day headings
   cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";

   //Displays number of days
   for (days = 1; days <= numDays; days++)
      cout << "  " << setw(2) << days; //for loop only loops this line
   {//random scope entry
   if ((days+offset) % 7==0)
      cout << "\n" << days;
   }
   return;
}


/* OFFSET
 * Determines the number of days to offset the Calendar and then returns
 * that value back to displayTable.*/
int getOffset()
{
   int offset;
   cout << "Offset:";
   cin >> offset;

   return offset;
}


/* NUMDAYS
 * Accepts the number of days from the user.*/
int getNumDays()
{
   int numDays;
   cout << "Number of days: ";
   cin >> numDays;
   return numDays;
}
Hi there,

You will want to use manipulators in order to set a specific length of every column.
More information and examples here:

http://cplusplus.com/reference/iomanip/
http://cplusplus.com/reference/iomanip/setw/

Don't forget to do #include <iomanip>

Hope that helps.

All the best,
NwN
thnks.
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
#include <iostream>
#include <iomanip>

using namespace std;

//PROTOTYPES
void printMonthCalendar(char, int);

int main()
{
   //LOCAL DECLARATIONS
   char startDay;
   int daysInMonth;

   cout << "The first day is: ";
   cin >> startDay;
   cout << "The days in the month: ";
   cin >> daysInMonth;

   cout << endl;
   printMonthCalendar(startDay, daysInMonth);
   cout << endl;

   return 0;
}

//---------------------------------------------------------
// FUNCTION DEFINITIONS
//---------------------------------------------------------
void printMonthCalendar(char strDayOfWeek, int daysInMonth)
{
   int indent = 0;
   if (toupper(strDayOfWeek) == 'M')
      indent = 1;
   else if (toupper(strDayOfWeek) == 'T')
      indent = 2;
   else if (toupper(strDayOfWeek) == 'W')
      indent = 3;
   else if (toupper(strDayOfWeek) == 'R')
      indent = 4;
   else if (toupper(strDayOfWeek) == 'F')
      indent = 5;
   //else if (toupper(strDayOfWeek) == 'S')  //Saturday
   //   indent = 6;
   else  //other characters will be considerd as Sunday
      indent = 0;

   cout << "Sun Mon Tue Wed Thu Fri Sat" << endl;
   if (indent)
      cout << setw(indent * 4) << " ";
   for (int i = 1; i <= daysInMonth; i++)
   {
      cout << setw(3) << i << " ";
      indent++;
      if (indent % 7 == 0)
         cout << endl;
   }
   if (indent % 7)
      cout << endl;
} 

I used this.
Topic archived. No new replies allowed.