Problem with function

I keep getting the error that offset and days were not declared in this scope in the displayTable(offset, days) in the main function.

Could anyone help me see what I'm doing wrong.

BTW this is my calendar program. I'm trying to test the display table function to see if it's working.

Thanks

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
 int daysMonth(int month, int year)
{
   int days = 0;
   
   switch (month)
   {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
         days = 31;
         break;
      case 4:
      case 6:
      case 9:
      case 11:
         days = 30;
         break;
      case 2:
         if (isLeapYear(year))
            days = 29;
         else
            days = 28;
         break;
   }
   return days;
}

int computeOffset(int month, int year)
{
   int offset = sumOfDays(month, year) % 7;
   return offset;
}

void displayTable(int offset, int days)
{
   int numbDays = days; //days come current daysMonth function
   int slot = 1;  
   int day = -offset;
   if (day == -6)
   {
      day = 1;
   }
   cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";

   
   for (; day <= numbDays; day++, slot++) //numbDays is the # of days for the month //
   {
      cout << "  ";
      if (day <= 0)
         cout << "  ";
      else
         cout << setw(2) << day;
      if (slot % 7 == 0)
         cout << "\n";
   }
   if ((slot - 1) % 7 != 0)
      cout << endl;

}

int main()
{
   int month = getMonth();
   int year = getYear();
   currentMonth(month, year);
   displayTable(offset, days);

   return 0;
}
The online compiler sees your posted code differently:
 In function 'int daysMonth(int, int)':
23:29: error: 'isLeapYear' was not declared in this scope
 In function 'int computeOffset(int, int)':
34:38: error: 'sumOfDays' was not declared in this scope
 In function 'void displayTable(int, int)':
47:4: error: 'cout' was not declared in this scope
56:24: error: 'setw' was not declared in this scope
61:15: error: 'endl' was not declared in this scope
 In function 'int main()':
67:25: error: 'getMonth' was not declared in this scope
68:23: error: 'getYear' was not declared in this scope
69:28: error: 'currentMonth' was not declared in this scope
70:17: error: 'offset' was not declared in this scope
70:25: error: 'days' was not declared in this scope


Anyway, I do presume that you don't have global variables and thus we should be looking at this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// (presumed) function declarations
int getMonth();
int getYear();
void currentMonth(int, int);
void displayTable(int, int);

int main()
{
   int month = getMonth();
   int year = getYear();
   currentMonth( month, year );
   displayTable( offset, days ); //syntax error

   return 0;
}

Your main() declares two variables: month and year. They are initialized and then used in currentMonth(). However, identifiers 'offset' and 'days' have not been declared anywhere (within scope).
Last edited on
Topic archived. No new replies allowed.