• We've upgraded our forums. Please post any issues/requests in this thread.

C# Programming Question

trojandnc

New Member
Joined
Nov 21, 2012
Messages
3 (0.00/day)
Likes
0
#1
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.");
}
}
}
 
Joined
Dec 2, 2009
Messages
3,257 (1.11/day)
Likes
315
System Name Mercury KM-81
Processor Phenom II x4 B50
Motherboard ASUS M4A89GTD PRO
Cooling Cooler Master Hyper TX3
Memory 6GB Ram (4GBx1 Corsair 2GBx1 Nanya)
Video Card(s) PowerColor AX6770 V2.0
Storage 2TB WD Black, 1TB Hitachi, 500 GB WD Blue
Display(s) Philips 247E-LPH 24" 1920x1080
Case Mercury
Audio Device(s) Integrated Realtek
Power Supply Corsair 750TX
Software Windows 7 64-bit
Benchmark Scores Maybe it is time to benchmark :D
#2
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

 
Last edited by a moderator:

trojandnc

New Member
Joined
Nov 21, 2012
Messages
3 (0.00/day)
Likes
0
#3
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.
 
Joined
Dec 2, 2009
Messages
3,257 (1.11/day)
Likes
315
System Name Mercury KM-81
Processor Phenom II x4 B50
Motherboard ASUS M4A89GTD PRO
Cooling Cooler Master Hyper TX3
Memory 6GB Ram (4GBx1 Corsair 2GBx1 Nanya)
Video Card(s) PowerColor AX6770 V2.0
Storage 2TB WD Black, 1TB Hitachi, 500 GB WD Blue
Display(s) Philips 247E-LPH 24" 1920x1080
Case Mercury
Audio Device(s) Integrated Realtek
Power Supply Corsair 750TX
Software Windows 7 64-bit
Benchmark Scores Maybe it is time to benchmark :D
#4
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

New Member
Joined
Nov 21, 2012
Messages
3 (0.00/day)
Likes
0
#5
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?
 
Joined
Dec 2, 2009
Messages
3,257 (1.11/day)
Likes
315
System Name Mercury KM-81
Processor Phenom II x4 B50
Motherboard ASUS M4A89GTD PRO
Cooling Cooler Master Hyper TX3
Memory 6GB Ram (4GBx1 Corsair 2GBx1 Nanya)
Video Card(s) PowerColor AX6770 V2.0
Storage 2TB WD Black, 1TB Hitachi, 500 GB WD Blue
Display(s) Philips 247E-LPH 24" 1920x1080
Case Mercury
Audio Device(s) Integrated Realtek
Power Supply Corsair 750TX
Software Windows 7 64-bit
Benchmark Scores Maybe it is time to benchmark :D
#6
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

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#7
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
Code:
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..
Code:
(currentAge.TotalDays/365.25).ToString()
 
Last edited: