Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie needing some help

Status
Not open for further replies.

Cnewb

MIS
Sep 10, 2002
2
0
0
US
First of all, let me start off by stating I'm a C newbie. So, please don't flame.

I'm in the process of writing my first program and I've run into a road block and could use some assistance.

Basically, here's what the program is supposed to do:

1. Ask the user for their Date of Birth in the format: mm/dd/yyyy
2. Ask the user for the current date in the format: mm/dd/yyyy
3. Once this information has been obtained, the program will tell you if you are 18 years of age or older.

Before anyone asks.....yes, this is for a begining C class I'm taking at school. I've finished writing it and it seems to be working fine....with the exception of a few dates.

Here's the source:
/*Age checking problem solution in C*/

#include <stdio.h>

main()
{

int m1, d1, y1, m2, d2, y2, yd; /*Declaration of integer variables:
m1= month of DOB
d1= day of DOB
y1= year of DOB
m2= month of date
d2= day of date
y2= year of date
yd= year of date - year of DOB*/

printf(&quot;Please enter the month of the DOB in the format mm:\n&quot;);
scanf(&quot;%d&quot;, &m1);
printf(&quot;Please enter the day of the DOB in the format dd:\n&quot;);
scanf(&quot;%d&quot;, &d1);
printf(&quot;Please enter the year of the DOB in the format yyyy:\n&quot;);
scanf(&quot;%d&quot;, &y1);
printf(&quot;\n&quot;);
printf(&quot;Please enter the month of today's date in the format mm:\n&quot;);
scanf(&quot;%d&quot;, &m2);
printf(&quot;Please enter the day of today's date in the format dd:\n&quot;);
scanf(&quot;%d&quot;, &d2);
printf(&quot;Please enter the year of today's date in the format yyyy:\n&quot;);
scanf(&quot;%d&quot;, &y2);
printf(&quot;\n&quot;);

yd= y2 - y1;
if(yd > 18)
printf(&quot;You are 18 years old or older!\n&quot;);
else
if(m2 < m1)
printf(&quot;You are not 18 years old or older!\n&quot;); else
if(d1 > d2)
printf(&quot;You are not 18 years old or older!\n&quot;);
else
printf(&quot;You are 18 years old or older!\n&quot;);

return 0;

}


Like I said, I'm new to C so you'll have to forgive my language structure. Any help is much appreciated!

p.s. We can not use nested functions for this program since we havn't learned that yet. We can only use If/else statements.
 
Program looks OK except

if(yd > 18)

should be

if(yd >= 18)

Which date combinations are giving you problems?
 
Hi,

There are lots of dates that won't work. It's all in how you are checking the numbers. Let's say I was born in 1992 on June 12th and that it is currently Feb. 11th, 2002.

Is 2002-1992 > 18....No
Is 02 > 06....No
Is 11 > 12....No

So, I will get the default message...which is wrong.

The yyyy format is easy to check first. That isn't the problem The problem is that you assume that if it isn't greater then you just need to have the day or month be greater. You need to consider equal to 18.

yd = y2 - y1;
/* check the most obvious first */
if ( yd < 18 ) {
/* then you aren't 18*/
}
else {
/* Now check if it is equal*/
if ( yd == 18 ) {
/* check your month and day here */
}
else {
/* Not less then, not ==, so it must be greater*/
}
}

This isn't the only or really the best solution, it is just one that should help you out. You'll have to insert the rest of the code but you'll have no problem with that.

Good Luck.

-Tyler
 
Hi -

I just wanted to make sure you realize that you can't use xwb's solution either. That still won't catch the fact that even if it is 18 years (by subtracting the years) that it may not have reached the month and day of your b-day...so you aren't really 18 yet.

Just wanted to point that out.

-Tyler
 
for class you gotta roll your own but i'm not against helping

you have three things to check on the year
>, =, & <. the < & > conditions you know what to do. it's only on the = clause you have to deal with the month.

the month check are analogous & only go to day compare on months =

then go to day compare
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top