-1

I have 3 problems with this code and I wanted to ask for a little help.

  1. I seem to get an error message every time in person2 that a local variable named myAge and DateOfBirth is already defined in this scope? How can this be?

  2. My second problem is that i cant seem to get the timestamp removed from the players date of birth.. I only need the date and not a timestamp?

  3. My third and last problem is that all of the code won't display in the cmd.exe at the same time? I have to press enter to show the next player, but i want all players to be displayed at the same time?

Can anyone be of help? Thank you in advance!

    public enum Gendertype { Male, Female };
    public class Person
    {
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public string Nationality { get; set; }
    public Gendertype Gender { get; set; }

    public Person(string fn, string mn, string ln, DateTime dob, string n, Gendertype g)
    {
        FirstName = fn;
        MiddleName = mn;
        LastName = ln;
        DateOfBirth = dob;
        Nationality = n;
        Gender = g;
    }

}
class Program
{ 
static void Main()
{
    Person person1 = new Person("Rafael" + "\n", "" + "\n", "Nadal" + "\n", new DateTime(1986,06, 03), "Spanish" + "\n", Gendertype.Male);
    Console.WriteLine("Player 1: \n First name = {0} Middle name = {1 } Last name = {2} Date of birth = {3} \n Nationality = {4} Gender = {5}", person1.FirstName, person1.MiddleName, person1.LastName, person1.DateOfBirth, person1.Nationality, person1.Gender);
        DateTime DateOfBirth = DateTime.Parse("1986/06/03");
        TimeSpan myAge = DateTime.Now.Subtract(DateOfBirth);
        Console.WriteLine(" Age:");
        Console.WriteLine(myAge.TotalDays/365);
        Console.ReadLine();

            Person person2 = new Person("Rafael" + "\n", "" + "\n", "Nadal" + "\n", new DateTime(1988, 07, 04), "Spanish" + "\n", Gendertype.Male);
            Console.WriteLine("Player 2: \n First name = {0} Middle name = {1 } Last name = {2} Date of birth = {3} \n Nationality = {4} Gender = {5}", person2.FirstName, person2.MiddleName, person2.LastName, person2.DateOfBirth, person2.Nationality, person2.Gender);
            DateTime dob = DateTime.Parse("1988/07/04");
            TimeSpan Age = DateTime.Now.Subtract(DateOfBirth);
            Console.WriteLine(" Age:");
            Console.WriteLine(myAge.TotalDays / 365);
            Console.ReadLine();


            person2.FirstName = "Molly";
            Console.WriteLine("Player 2: \n First name = {0} Middle name = {1 } Last name = {2} Date of birth = {3} \n Nationality = {4} Gender = {5}", person1.FirstName, person1.MiddleName, person1.LastName, person1.DateOfBirth, person1.Nationality, person1.Gender);
            Console.WriteLine("person1 Name = {0} Age = {1}", person2.FirstName, person2.DateOfBirth);


            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
  }

}

Aafflalo
  • 25
  • 1
  • **1)** If the error message is a secret you cannot share we cannot help you. **2)** The type DateTime always consists of Date and Time - if you need a specific output format (like "yyyy/MM/dd") just specifiy it when rendering the output. **3)** You did put those `Console.ReadLine();` there, so that's the console does. – Filburt Apr 20 '17 at 18:02
  • 1) The code you are showing us is not exhibiting this problem. 2) `Console.WriteLine` uses [Composite Formatting](https://msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx), you can specify the format of the output like so: `Date of birth = {3:yyyy/MM/dd}`, 3) Because you put `Console.ReadLine();` after each one so it pauses until you hit enter; read the [MSDN](https://msdn.microsoft.com/en-us/library/system.console.readline(v=vs.110).aspx): "If the standard input device is the keyboard, the ReadLine method blocks until the user presses the Enter key." – Quantic Apr 20 '17 at 18:04
  • 1
    Note that you're calculating age incorrectly too. See [this StackOverflow question](http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c) and also [my blog post on this subject](http://codeofmatt.com/2014/04/09/handling-birthdays-and-other-anniversaries/). – Matt Johnson-Pint Apr 20 '17 at 18:11

2 Answers2

1
  1. You have declared your myAge/DateOfBirth variables in the Main() function right after declaring person1:

    DateTime DateOfBirth = DateTime.Parse("1986/06/03");

    TimeSpan myAge = DateTime.Now.Subtract(DateOfBirth);

I assume you are then trying to declare another pair of variables with the same name right after declaring person2, but you are still within the same Main() function. The compiler is complaining that you're declaring variables when you've already declared the variables. To fix this, just get rid of the DateTime and TimeSpan parts of your second lines. This way, you're not trying to declare new variables with the same name, you're just re-using the old ones.

  1. You are calculating age using DateTime.Now which includes hours, seconds, and milliseconds. If you want to get the date only, try DateTime.Today (https://msdn.microsoft.com/en-us/library/system.datetime.today(v=vs.110).aspx). As for text-friendly output, you should look into DateTime formatting to get the correct output (https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx)

  2. You have a Console.ReadLine() call after each set of output. This will halt the regular execution of the code to wait for user input. Just get rid of those lines and it will execute all at once.

Matt Singer
  • 101
  • 2
  • 10
0

try this

public enum Gendertype { Male, Female };
    public class Person
    {
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public string Nationality { get; set; }
    public Gendertype Gender { get; set; }
    public int Age 
    {
        return DateTime.Now.Subtract(DateOfBirth).TotalDays / 365;
    }
    public Person(string fn, string mn, string ln, DateTime dob, string n, Gendertype g)
    {
        FirstName = fn;
        MiddleName = mn;
        LastName = ln;
        DateOfBirth = dob;
        Nationality = n;
        Gender = g;
    }

}
class Program
{ 
    static void Main()
    {
        Person person1 = new Person("Rafael" + "\n", "" + "\n", "Nadal" + "\n", new DateTime(1986,06, 03), "Spanish" + "\n", Gendertype.Male);
        Console.WriteLine("Player 1: \n First name = {0} Middle name = {1 } Last name = {2} Date of birth = {3} \n Nationality = {4} Gender = {5}", person1.FirstName, person1.MiddleName, person1.LastName, person1.DateOfBirth, person1.Nationality, person1.Gender);

        Console.WriteLine(" Age:");
        Console.WriteLine(person1.Age);
        //Console.ReadLine(); this waits for your enter key

        Person person2 = new Person("Rafael" + "\n", "" + "\n", "Nadal" + "\n", new DateTime(1988, 07, 04), "Spanish" + "\n", Gendertype.Male);
        Console.WriteLine("Player 2: \n First name = {0} Middle name = {1 } Last name = {2} Date of birth = {3} \n Nationality = {4} Gender = {5}", person2.FirstName, person2.MiddleName, person2.LastName, person2.DateOfBirth, person2.Nationality, person2.Gender);

        Console.WriteLine(" Age:");
        Console.WriteLine(person2.Age);
        //Console.ReadLine(); this waits for your enter key


        person2.FirstName = "Molly";
        Console.WriteLine("Player 2: \n First name = {0} Middle name = {1 } Last name = {2} Date of birth = {3} \n Nationality = {4} Gender = {5}", person1.FirstName, person1.MiddleName, person1.LastName, person1.DateOfBirth, person1.Nationality, person1.Gender);
        Console.WriteLine("person1 Name = {0} Age = {1}", person2.FirstName, person2.DateOfBirth.ToShortDateString());


        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
     }
}
Krishna
  • 1,945
  • 1
  • 13
  • 24