Why does i keep a constant value?

Whatever values I put i=1968461930
Why?

the finished program is supposed to calculate the leap years between two given year. I want i to be the closest leap year after the user input

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

using namespace std;

int main ()

{


int   sy, ey, i ;



cout << "Enter starting year:" << endl ;
cin >> sy ;
cout << "Enter ending year;" << endl ;
cin >> ey ;


if ( sy <= 0 || ey <= 0)
{
    cout << "Invalid input!!";
}


else
{
 if ((sy % 4) == 0)
 {
    sy ==  i ;
 }

 else if (sy % 4 == 1)
 {
     sy + 3 ==  i ;

 }
else if (sy % 4 == 2)

{
    sy + 2 == i;
}

else  (sy % 4 == 3);


{
    sy + 1 == i;
    
}



    cout << i << endl;


}


    return 0;
}
Last edited on
What value is i supposed to have? I don't see that it gets initialized to a value anywhere, so it will have some garbage value in it.
Your problem is with your assignment operators...You are not using the correct syntax.

The "=" works differently than it does in your math class. Whatever is on the right of the operator passes its information to the left. What ever variable that is on the left of the operator will take the value of whats on the right..

NEW_DATA = OLD_DATA;


change this to

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
else
{
 if ((sy % 4) == 0)
 {
     i = sy;
 }

 else if (sy % 4 == 1)
 {
     i = sy +3;

 }
else if (sy % 4 == 2)

{
    i = sy+2;
}

else  (sy % 4 == 3);


{
   i = sy+1;  
}



    cout << i << endl;


}


    return 0;
}


**Try to make your variables more explicit...Be a little bit more descriptive in your naming conventions when declaring a variable. What does sy and ey stand for?

***Also the structure of your if else statements are not correct....well just very sloppy looking :/
Last edited on
Topic archived. No new replies allowed.