Calendar still displaying when parameters are invalid

Here's what I need to do:
If the start day is not in the range 0 to 6 inclusive or if the number of days is not in the range 1 to 31 inclusive, the program should display a helpful error message instead of displaying a calendar. If the parameters are OK, the program should display the numbers for the calendar as they would appear on a typical calendar.

However, when I test this function, the calendar is still displaying even when the parameters are invalid.


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

void displayCalendarDays(int startDay, int numDaysInMonth);


void main()
{
  cout << endl;
  cout << endl;
  cout << "Startday = 3, numDays = 31" << endl;
  displayCalendarDays (3, 31);
  cout << endl;
  cout << endl;
  cout << "Startday = 0, numDays = 31" << endl;
  displayCalendarDays (0, 31);
  cout << endl;
  cout << endl;
  cout << "Startday = 7, numDays = 31" << endl;
  displayCalendarDays (7, 31);
  cout << endl;
  cout << endl;
  cout << "Startday = 0, numDays = 32" << endl;
  displayCalendarDays (0, 32);
  cout << endl;
  cout << endl;
} // end main

  void displayCalendarDays(int startDay, int numDaysInMonth)  
{  
    int day;
  
    for (day = 0; day < startDay; day++)
        cout << "   ";
  
    for (day = 1; day <= numDaysInMonth; day++)
    {
        cout << setw(3) << day;

    if ((day + startDay) % 7 == 0)
        cout << endl;
    }
  
    if (startDay < 0 || startDay > 6 || numDaysInMonth < 28 || numDaysInMonth > 31)
    { 
        cout << endl;
  	cout << "You have entered either an invalid start day \n";  
        cout << "or an invalid number of days in the month.\n";  
        cout << "Please try again." << endl;  
    }
    else if (startDay >= 0 && startDay <= 6 && numDaysInMonth > 28 && numDaysInMonth < 31)

    return; 
}
@bpro

You are checking if the parameters AFTER you print the calendar. Move the last if statement just past int day, and add a return after the cout << "Please try again." << endl;, otherwise the months will still print since you haven't told the program not to.
Thank you @whitenite1, got it working!
Topic archived. No new replies allowed.