Hi I am new to C++ and new to programming in general. I would greatly appreciate if you could help me. I need to create a program using loops, that will do the following 3 task:

1. For each of the three persons, determine and report whether that person was born in a leap year or not. Check whether the birthday information is valid. For example, no one could be born on February 29, 2005. It is an invalid date. If it is not valid, report an “Invlaid Date” message to the user and then have a return statement like: return 0; to stop the execution of the main function.
2. Then for each of the three persons, determine and report how many of the other two persons are (is) older than that person.
3. Finally, use the information in step 3 to determine and report who is (are) among the oldest in the family


I am having a problem comparing birthdays properly. Also any advice on cleaning up my code or making it more efficient would be great.

//*------------- Family Statics Program ---------- */


#include <string>
#include <iostream>

using namespace std;


/*------------ Beginning of main function ----------*/
int main()

{
	
/*---------- Declaring Variables ----------*/
	int m, d , y;
	int fatherM, fatherD, fatherY;
	int motherM, motherD, motherY;
	int age, fatherAge, motherAge;
	
	
	
	
	
/*---------- Getting User DOB----------*/	
	cout << "Please enter your date of birth (mm dd yyyy): " << endl;
	cout << "Month (1 - 12): ";
	cin >> m;
	cout << "Day   (1 - 31): ";
	cin >> d;
	cout << "Year (e.g. 1996): ";
	cin >> y;
	
/*---------- Validating Bday for user --------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------------------------------------------*/	
/*--------------------------------------------------------------------------------------------------------------------------------------*/	

	while ((m < 1|| m > 12) || (d < 1 || d > 31) || (d==31 && m==2 || m==4 || m==6 || m==9 || m==11))
	{
		cout << " Invalid date, start over"<<endl;
		return 0;
	}
	
	while ( (d==30) && (m==2) )
	{
		cout << " Invalid date, start over"<<endl;
		return 0;
	}
	
	while ( (m==2) && (d==29) && (y%4!=0) )
	{
		cout << " Invalid date, start over"<<endl;
		return 0;
	}	
    while  ( (m==2) && (d==29) && (y%100==0) )
	{
		cout << " Invalid date, start over"<<endl;
		return 0;
	}
/*---------- Determine leap year----------*/	

	if(y%400 ==0 || (y%100 != 0 && y%4 == 0))
    {
        cout<< " You were born on a leap year!"<<endl;
    }
    else
    {
        cout<< " You were not born on a leap year."<<endl;
    }	

	
	
/*---------- Getting Father DOB---------------------------------------------------------------------------------------------------------*/	
/*--------------------------------------------------------------------------------------------------------------------------------------*/	
/*--------------------------------------------------------------------------------------------------------------------------------------*/	

	cout << "Please enter your father's date of birth. (mm dd yyyy): " << endl;
	cout << "Month (1 - 12): ";
	cin >> fatherM;
	cout << "Day   (1 - 31): ";
	cin >> fatherD;
	cout << "Year (e.g. 1996): ";
	cin >> fatherY;
	
	/*---------- Validating Bday for father ----------*/
	while ((fatherM < 1|| fatherM > 12) || (fatherD < 1 || fatherD > 31) || (fatherD==31 && fatherM==2 || fatherM==4 || fatherM==6 || fatherM==9 || fatherM==11))
	{
		cout << " Invalid date, start over"<<endl;
		return 0;
	}
	
	while ( (fatherD==30) && (fatherM==2) )
	{
		cout << " Invalid date, start over"<<endl;
		return 0;
	}
	
	while ( (fatherM==2) && (fatherD==29) && (fatherY%4!=0) )
	{
		cout << " Invalid date, start over"<<endl;
		return 0;
	}	
    while  ( (fatherM==2) && (fatherD==29) && (fatherY%100==0) )
	{
		cout << " Invalid date, start over"<<endl;
		return 0;
	}
	/*---------- Determine leap year----------*/	
	
	if(fatherY%400 ==0 || (fatherY%100 != 0 && fatherY%4 == 0))
    {
        cout<< " Your father born on a leap year!"<<endl;
    }
    else
    {
        cout<< " Your  father was not born on a leap year."<<endl;
    }	
	
/*---------- Getting Mother DOB---------------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------------------------------------------*/	
/*--------------------------------------------------------------------------------------------------------------------------------------*/	

	cout << "Please enter your father's date of birth. (mm dd yyyy): " << endl;
	cout << "Month (1 - 12): ";
	cin >> motherM;
	cout << "Day   (1 - 31): ";
	cin >> motherD;
	cout << "Year (e.g. 1996): ";
	cin >> motherY;
	
	/*---------- Validating Bday for mother ----------*/
	while ((motherM < 1|| motherM > 12) || (motherD < 1 || fatherD > 31) || (motherD==31 && motherM==2 || motherM==4 || motherM==6 || motherM==9 || motherM==11))
	{
		cout << " Invalid date, start over"<<endl;
		return 0;
	}
	
	while ( (motherD==30) && (motherM==2) )
	{
		cout << " Invalid date, start over"<<endl;
		return 0;
	}
	
	while ( (motherM==2) && (motherD==29) && (motherY%4!=0) )
	{
		cout << " Invalid date, start over"<<endl;
		return 0;
	}	
    while  ( (motherM==2) && (motherD==29) && (motherY%100==0) )
	{
		cout << " Invalid date, start over"<<endl;
		return 0;
	}
	/*---------- Determine leap year----------*/	
	
	if(motherY%400 ==0 || (motherY%100 != 0 && motherY%4 == 0))
    {
        cout<< " Your mother was born on a leap year!"<<endl;
    }
    else
    {
        cout<< " Your mother was not born on a leap year."<<endl;
    }	
	
/*------Comparing Ages------------------------------------------------------------------------------------------------------------------*/	
/*--------------------------------------------------------------------------------------------------------------------------------------*/	
/*--------------------------------------------------------------------------------------------------------------------------------------*/	
	
	age = (m * 100) + (y * 10000) + (d);
	fatherAge = (fatherM * 100) + (fatherY * 10000) + fatherD;
	motherAge = (motherM * 100) + (motherY * 10000) + motherD;
	
	if ( (age < fatherAge) && (age < motherAge) )
	{
		cout << " Your father and mother are older than you."<<endl;
	}
	
	if ( (age > fatherAge) && (fatherAge < motherAge) )
	{
		cout << " Your father is the oldest amongst you and your mother.";
	}
	
	if ( (motherAge < age) && (motherAge < fatherAge) )
	{
		cout << " Your mother is the oldest amongst you and your father.";
	}
	
	if ( (age > fatherAge) && (age > motherAge) )
	{
		cout << " You are older than your father and mother."<<endl;
	}
	if ( (fatherAge > age) && (fatherAge > motherAge) )
	{
		cout << " Your father is younger than you and your mother.";
	}
	if ( (motherAge > age) && (motherAge > fatherAge) )
	{
		cout << " Your mother is the oldest amongst you and your father.";
	}
	if ((age > fatherAge) && (age < motherAge))
	{
		cout<< "You are older than your father but younger than your mother.";
	}
	if ((age > fatherAge) && (age > motherAge))
	{
		cout << "You are older than your mother but younger than your father.";
	}
	
	
	
	
	
	if ((fatherAge > age) && (fatherAge > motherAge))
	{
		cout<< " Your father is the oldest. " <<endl;
	}
	if ((age < fatherAge) && (age > motherAge))
	{
		cout<< " You are the oldest. " <<endl;
	}
	if ((motherAge > age) && (motherAge > fatherAge))
	{
		cout<< " Your mother is the oldest. " <<endl;
	}
	
	
	
	
	
	
	
	
		
	return 0;
}

Your problem is in this line right here

age = (m * 100) + (y * 10000) + (d);

I somewhat see what you are trying to do but that's a strange way of doing it. Try three nested if statements. For example to check if father is older than mother do this:

-check is fathers year < mothers year
-if yes is fathers month < mothers month
-if yes is fathers day < mothers day?

if any of these are no then the mother is older than the father.

Sorry for the double, post deleted look at post above

Edited 7 Years Ago by chunalt787: Accidental double posting

This article has been dead for over six months. Start a new discussion instead.