# C# Programming Question



## trojandnc (Nov 21, 2012)

I am trying to write a program that asks the user to input their date of birth to verify if they are over 18. If they are I need to print "transaction approved" to the console and if not I need it to print "transaction denied."

The problem I am having is that I cannot compare  a the values that I want in the if statement.  Help!

Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication7
{
class Program
{
public static void Main()
{
DateTime dateOfBirth, legalDate, currentDate;
TimeSpan currentAge;
Console.WriteLine("Please Enter Your Date of Birth");
dateOfBirth = Convert.ToDateTime(Console.ReadLine());
legalDate = DateTime.Now.AddYears(-18);
currentDate = DateTime.Now;
currentAge = currentDate.Subtract(dateOfBirth); 
Console.WriteLine(
"As of " + currentDate + " you are: " + currentAge + " years old");
if ((currentAge < 18))
{
Console.WriteLine(
"Your transaction has been approved. Thank you.");
}
else
Console.WriteLine(
"You were not born on or before " + legalDate + ". Your transaction has been denied.");
}
}
}


----------



## GSquadron (Nov 21, 2012)

try it like this: *else{*

also, you want it to output on windows or on cmd?

sometimes it depends on which ide you are working on


----------



## trojandnc (Nov 21, 2012)

Aleksander Dishnica said:


> try it like this: *else{*
> 
> also, you want it to output on windows or on cmd?
> 
> sometimes it depends on which ide you are working on



Thanks for the quick reply

I'm using Visual C# 2010 Express. (Windows)

I had gotten it to run, but it would not differentiate age between any birth date that was in the year 1994.


----------



## GSquadron (Nov 21, 2012)

you should add a (2012 - bornyear) to find the age, not age as 1994 or other than that.
For example if someone is born on 1994, you should make 2012 - 1994 = 18
than you will be able to find if this 18 is > or < than what you want


----------



## trojandnc (Nov 21, 2012)

Thanks. What is the difference between hard coding 2012 as the year and using DateTime.Today so that when the year changes I would not have to remember to go back and edit code?


----------



## GSquadron (Nov 21, 2012)

you can use 2012, or you can use date() or whatever function C# has to control which is the year in your computer, so the year of your computer - 1994 it will find it.
Try it with 2012 without the date() function first.

The function should be like this:
DateTime value = new DateTime(2010, 1, 18);

Example this link:
http://www.dotnetperls.com/datetime


----------



## Kreij (Nov 21, 2012)

You can't use the less than operand when comparing a TimeSpan and Int.
So checking to see if currentAge (a TimeSpan) is less than 18 (an Int) throws a compiler error.

You could use

```
if (dateOfBirth <= legalDate) {
  ...
}
```

... and no, you should not hardcode in any dates as the legal date will change every day.

You also cannot use the currentAge in the string output as it's not in years, it's a TimeSpan.
You could use something like..

```
(currentAge.TotalDays/365.25).ToString()
```


----------

