space days apart

Hello I have a problem that I am trying to work on. I am supposed to get the input of offset and number of days in the given month from the user. I have the following code, but I feel like I don't know how to space the days evenly from one another. I tried using " " but that didn't work either. I know I don't have the offset computed quite yet, so don't worry about that, I'm just trying to figure out to space the days evenly from one another.

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
#include <iostream>
#include <iomanip>
using namespace std;
/**********************************************************************
 * this table will display a calendar.
 ***********************************************************************/

  int displayTable(int numDays, int offSet)
{
   int days;

   cout << "Su Mo Tu We Th Fr Sa\n";

   for (days = 1; days < numDays; days++)
   { cout << " " << setw(2) << days;
   if(days % 7 == 0)
      cout << "\n";
   }
}
/**********************************************************************
 * main will ask the user for offset and the number of days in the month.
 ***********************************************************************/
int main()
{
   int numDays;
   int offSet;

   cout << "Number of Days: ";
   cin >> numDays;

   cout << "Offset: ";
   cin >> offSet;

   cout << displayTable(numDays, offSet) << endl;

   return 0;

Here is my output.
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
2930
Hello alextexasfan12

I have not tested this yet, but you might try changing cout << " " << setw(2) << days; to cout << setw(2) << days << " "; and see what happens. Most of the time when I do something like this The change to is what I use.

Hope that helps,

Andy
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
#include <iostream>
#include <iomanip>

enum week_day { SUN, MON, TUE, WED, THU, FRI, SAT }; // 0,1,2,3,4,5,6

void display_month( int num_days, week_day first_day )
{
    #ifndef NDEBUG
        std::cout << "\nnum_days == " << num_days << "  first_day == " << first_day << '\n' ;
    #endif // NDEBUG

    const int width = 5 ;
    std::cout << "\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    
    // skip (print spaces) till we get to the first day in the month 
    for( int i = 0 ; i < first_day ; ++i ) std::cout << std::setw(width) << ' ' ; ;

    for( int day = 1; day <= num_days; ++day ) // from the first day onwards
    {
       std::cout << std::setw(2) << day << std::setw(width-2) << ' ' ;
       if( (day+first_day) % 7 == 0 ) std::cout << '\n' ; // if saturday, print a new line
    }

    std::cout << "\n\n" ;
}

int main()
{
    display_month( 31, THU ) ;
    display_month( 30, SUN ) ;
    display_month( 28, SAT ) ;
}

http://coliru.stacked-crooked.com/a/4c074ce7c377f47c
@alextexasfan12

Here's your program with only slight modifications to it.
I placed remarks where needed, to explain what I did.

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
#include <iostream>
#include <iomanip>

using namespace std;
/**********************************************************************
* this table will display a calendar.
***********************************************************************/

void displayTable(int numDays, int offSet) // Use void. You're not returning a value
{
	int days;

	cout << " Su Mo Tu We Th Fr Sa\n";
	for (int x=0;x<offSet;x++)// Space out the offset before writing day 1
		cout << "   ";
	for (days = 1; days <= numDays; days++)// Using <= since we want to print all the days
	{ 
		cout << " " << setw(2) << days;
		if((days+offSet) % 7 == 0)
			cout << endl;
	}
}
/**********************************************************************
* main will ask the user for offset and the number of days in the month.
***********************************************************************/
int main()
{
	int numDays;
	int offSet;

	cout << "Number of Days: ";
	cin >> numDays;

	cout << "Offset: ";
	cin >> offSet;

	displayTable(numDays, offSet);
	cout << endl << endl;

	return 0;
}
Hello alextexasfan12

"displayTable" returns an int, but the function returns nothing and it does not need to. Change "displayTable" to a void function.

Line 34 cout << displayTable(numDays, offSet) << endl; will not work that way. It should only be function call not part of a "cout".

You enter a number into variable "offset", but the "displayTable" function never makes use of this variable even though it is passed to the function.

The above changes will print a calendar with the first always starting on Sunday.

JLBorges's solution should give you some ideas.

Hope that helps,

Andy
So you gave me good advice, but there is a slight issue. Offset =0 starts on Monday how would I edit the loop for it to know that. Would it be for (int x =1; x <offset; x++) or x=-1 b/c I tried both of those and they aren't working. Thanks though for the input previously.
I think I got it, I just used a series of if statements to determine the setw to determine the spacing in between. Now I just have to make it formatted correctly.
Topic archived. No new replies allowed.