Confused about if statement and modulo.

I just started reading c++ tutorial here while also learning from the book.
So here is my problem.


1
2
3
4
5
  if ((yearBorn % 4) == 0) // 14 % 4 = 2
    {
    cout << "\nYou were born in a Leap Year--cool!\n";
    }

CURRENTYEAR 2014 - yearBorn 2000 is 14 then 14 % 4 is 2
so that means so yearBorn is not equal to 0 but it keeps printing
"You were born..." Why i'm getting this?
Thank you.

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
 
#include <iostream>
#define CURRENTYEAR 2014

using namespace std;

int main()
{
    int yearBorn, age;
    cout <<"What year were you born?\n";
    cin >> yearBorn;

    if (yearBorn > CURRENTYEAR)
    {
    cout << "Really? You haven't been born yet?\n";
    cout << "Want to try again with a different year?\n";
    cout << "What year were you born?\n";
    cin >> yearBorn;


    }
    age = CURRENTYEAR - yearBorn;
    cout <<"\nSo this year you will turn " << age << " on your birthday!\n";
    if ((yearBorn % 4) == 0)
    {
    cout << "\nYou were born in a Leap Year--cool!\n";
    }
return 0;
}

Last edited on

Line 18, if person entered a year over current year it has no checks.
Line 24, test for a leap year isnt complete.

How about this..

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

#include <iostream>
#define CURRENTYEAR 2014

using namespace std;

int main()
{
	int yearBorn, age;
	
	do
	{
		cout << "What year were you born?\n";
		cin >> yearBorn;	
	}while (yearBorn > CURRENTYEAR);

	age = CURRENTYEAR - yearBorn;
	cout << "\nSo this year you will turn " << age << " on your birthday!\n";

	if ((yearBorn % 4 == 0) && !(yearBorn % 100 == 0) || (yearBorn % 400 == 0))
		cout << "\nYou were born in a Leap Year--cool!\n";

	return 0;
}
Topic archived. No new replies allowed.