Scope help

Hey, im trying to count the total days from year 2011 untill year 0 (considering leap years). Can someone help me out? It seems my variable "counter" remains inside the "while" scope, but i cant figure out how to pass it to the main scope so i can cout it.


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


bool leapYear (int year);
int daysToZero(int year);

int main()
{
   int d1 = 31, m1 = 12, y1 = 2011, count1 = 0;
   int currentYear = y1;
   int counter = 0;
   int days;
   while (currentYear < 0)
   {
      if (leapYear(currentYear) == 1)
      {
         days = 366;
      }
      else
      {
         days = 365;
      }
      counter = counter + days;
      currentYear--;
   }
   cout << counter;
   return 0;
}

bool leapYear (int year)
{
   bool status = false;
   if (year % 4 == 0 && year % 100 != 0)
   {
      status = true;
   }
   else if (year % 4 == 0 && year % 100 == 0 && year % 400 == 0)
   {
      status = true;
   }
   return status;
}
I strongly suspect it is line 15

 
while (currentYear < 0)


change the lower than to > 0 and it should work.
1
2
3
4
5
bool leapYear (const int year)
{
  
   return   (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}


There were several major changes to the calendar, are you going to take them into account?

This:
counter = counter + days;

Can be written :

counter += days;
ty for the replies. Once i change the < symbol, should the value of the counter inside the while scope pass to the main scope? Suppose the value of the counter variable inside the while loop ends up being 100, will the valué of the counter variable inside the main scope be also 100?
Yes.

The variable counter is declared on line 13, so it's scope is until the end of main.
Topic archived. No new replies allowed.