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

C# Programming Question

Discussion in 'Programming & Webmastering' started by trojandnc, Nov 21, 2012.

  1. trojandnc New Member

    Joined:
    Nov 21, 2012
    Messages:
    3 (0.00/day)
    Thanks Received:
    0
    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.");
    }
    }
    }
     
  2. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.79/day)
    Thanks Received:
    304
    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: Nov 20, 2013
    trojandnc says thanks.
  3. trojandnc New Member

    Joined:
    Nov 21, 2012
    Messages:
    3 (0.00/day)
    Thanks Received:
    0
    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.
     
  4. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.79/day)
    Thanks Received:
    304
    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
     
  5. trojandnc New Member

    Joined:
    Nov 21, 2012
    Messages:
    3 (0.00/day)
    Thanks Received:
    0
    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?
     
  6. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.79/day)
    Thanks Received:
    304
    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
     
  7. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.87/day)
    Thanks Received:
    5,616
    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: Nov 21, 2012

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page