• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

C# Programming Question

trojandnc

New Member
Joined
Nov 21, 2012
Messages
3 (0.00/day)
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,351 (0.64/day)
System Name Dark Stealth
Processor Ryzen 5 5600x
Motherboard Gigabyte B450M Gaming rev 1.0
Cooling Snowman, arctic p12 x2 fans
Memory 16x2 DDR4 Corsair Dominator Pro
Video Card(s) 3080 10gb
Storage 2TB NVME PCIE 4.0 Crucial P3 Plus, 1TB Crucial MX500 SSD, 4TB WD RED HDD
Display(s) HP Omen 34c (34" monitor 3440x1440 165Hz VA panel)
Case Zalman S2
Power Supply Corsair 750TX
Mouse Logitech pro superlight, mx mouse s3, Razer Basiliskx with battery
Keyboard Custom mechanical keyboard tm680
Software Windows 11
Benchmark Scores 70-80 fps 3440x1440 on cyberpunk 2077 max settings
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)
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,351 (0.64/day)
System Name Dark Stealth
Processor Ryzen 5 5600x
Motherboard Gigabyte B450M Gaming rev 1.0
Cooling Snowman, arctic p12 x2 fans
Memory 16x2 DDR4 Corsair Dominator Pro
Video Card(s) 3080 10gb
Storage 2TB NVME PCIE 4.0 Crucial P3 Plus, 1TB Crucial MX500 SSD, 4TB WD RED HDD
Display(s) HP Omen 34c (34" monitor 3440x1440 165Hz VA panel)
Case Zalman S2
Power Supply Corsair 750TX
Mouse Logitech pro superlight, mx mouse s3, Razer Basiliskx with battery
Keyboard Custom mechanical keyboard tm680
Software Windows 11
Benchmark Scores 70-80 fps 3440x1440 on cyberpunk 2077 max settings
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)
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,351 (0.64/day)
System Name Dark Stealth
Processor Ryzen 5 5600x
Motherboard Gigabyte B450M Gaming rev 1.0
Cooling Snowman, arctic p12 x2 fans
Memory 16x2 DDR4 Corsair Dominator Pro
Video Card(s) 3080 10gb
Storage 2TB NVME PCIE 4.0 Crucial P3 Plus, 1TB Crucial MX500 SSD, 4TB WD RED HDD
Display(s) HP Omen 34c (34" monitor 3440x1440 165Hz VA panel)
Case Zalman S2
Power Supply Corsair 750TX
Mouse Logitech pro superlight, mx mouse s3, Razer Basiliskx with battery
Keyboard Custom mechanical keyboard tm680
Software Windows 11
Benchmark Scores 70-80 fps 3440x1440 on cyberpunk 2077 max settings
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
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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:
Top