[General C# Coding] Help

03/01/2009 02:56 Rofa300#1
So as Many of u Know or Don't, am Learning C# and Till School Is Over My Learning Progress Will be SLOW.However, i tried to Make a Simple Program That Do Every thing this Pic Shows
[Only registered and activated users can see links. Click Here To Register...]
Now i need to Know how to make The User CHOOSE From Multi Options , like to Add [Mr , Mrs ... etc] , Emme Showed me b4 but i lost it b4 i look at it ...


Here is The Code
Code:
static void Main(string[] args)
        {
            StartLabel:
            try
            {
                Console.Write("What is Your Name: ");
                string Name1 = Console.ReadLine();
                Console.Write("What is Your Mid Name: ");
                string Name2 = Console.ReadLine();
                Console.Write("What is Your Last Name: ");
                string Name3 = Console.ReadLine();
                Console.WriteLine("What Is Your Birth Year?");
                int year = int.Parse(Console.ReadLine());
                Console.WriteLine("What Is Your Birth Month? ");
                int month = int.Parse(Console.ReadLine());
                Console.WriteLine("What Is Your Birth Day? ");
                int day = int.Parse(Console.ReadLine());

                DateTime myDOB = new DateTime(year, month, day);
                int age = CalcAge(myDOB);
                Console.WriteLine(" ");
                Console.WriteLine(" ");
                Console.WriteLine("Hello" + " " + Name1 + " " + Name2 + " " + Name3 + " " +"You Are {0} Years Old.", age);
                Console.WriteLine(" ");
                Console.WriteLine(" ");
                Console.WriteLine(" ");
                Console.WriteLine("Press Any Key To Close The Program ...");
                Console.ReadLine();
            }
            catch(Exception)
            {
                Console.WriteLine("Invaild Input , Please Try Again . . . ");
                Console.ReadLine();
                goto StartLabel;
            }
        }

        public static int CalcAge(DateTime DateOfBirth)
        {
            DateTime now = DateTime.Today;
            int years = now.Year - DateOfBirth.Year;
            if (now.DayOfYear < DateOfBirth.DayOfYear)
                --years;
            return years;
        }
    }
}
03/03/2009 03:00 leavemealone#2
Wrong section.

#Moved
03/03/2009 06:19 andyd123#3
One way you could do this, without giving away that you are trying to determine Mrs, Mr, or Ms would be like this...
Code:
spublic static void Main(string[] args)
        {
            StartLabel:
            try
            {
                Console.Write("What is Your Name: ");
                string Name1 = Console.ReadLine();
                Console.Write("What is Your Mid Name: ");
                string Name2 = Console.ReadLine();
                Console.Write("What is Your Last Name: ");
                string Name3 = Console.ReadLine();
                Console.Write("Are you a man or woman? ");
                bool man = true;
                string sex = Console.ReadLine();
                if(sex.Contains("wo"))
                    man = false;
                Console.Write("Are you married? ");
                string married = Console.ReadLine();
                bool married2 = false;
                if(married.Contains("y"))
                    married2 = true;
                Console.WriteLine("What Is Your Birth Year?");
                int year = int.Parse(Console.ReadLine());
                Console.WriteLine("What Is Your Birth Month? ");
                int month = int.Parse(Console.ReadLine());
                Console.WriteLine("What Is Your Birth Day? ");
                int day = int.Parse(Console.ReadLine());

                DateTime myDOB = new DateTime(year, month, day);
                int age = CalcAge(myDOB);
                Console.WriteLine(" ");
                Console.WriteLine(" ");
                if(man)
                {
                    Console.WriteLine("Hello" + " Mr. " + Name1 + " " + Name2 + " " + Name3 + " " +"You Are {0} Years Old.", age);
                }
                else if(!man && !married2)
                {
                    Console.WriteLine("Hello" + " Ms. " + Name1 + " " + Name2 + " " + Name3 + " " +"You Are {0} Years Old.", age);
                }
                else
                {
                    Console.WriteLine("Hello" + " Mrs. " + Name1 + " " + Name2 + " " + Name3 + " " +"You Are {0} Years Old.", age);
                }
                Console.WriteLine(" ");
                Console.WriteLine(" ");
                Console.WriteLine(" ");
                Console.WriteLine("Press Any Key To Close The Program ...");
                Console.ReadLine();
            }
            catch(Exception)
            {
                Console.WriteLine("Invaild Input , Please Try Again . . . ");
                Console.ReadLine();
                goto StartLabel;
            }
        }

        public static int CalcAge(DateTime DateOfBirth)
        {
            DateTime now = DateTime.Today;
            int years = now.Year - DateOfBirth.Year;
            if (now.DayOfYear < DateOfBirth.DayOfYear)
                --years;
            return years;
        }
It is entirely possible to do this other ways, even ways that use less lines of code, but you can always figure those out ;)
03/05/2009 22:20 Rofa300#4
thx Andy
03/05/2009 23:41 andyd123#5
Not a problem :)
03/06/2009 20:59 unknownone#6
Quote:
Originally Posted by andyd123 View Post
One way you could do this, without giving away that you are trying to determine Mrs, Mr, or Ms would be like this...

It is entirely possible to do this other ways, even ways that use less lines of code, but you can always figure those out ;)
A good idea to do it in code, but not an ideal solution in a real environment. How someone likes to be addressed is preference, particlarly for women (Ie, Ms. or Miss.). You're also dropping chance for people to put other honorifics such as Dr.
IMO, it would be better to leave it open for text, or have a more complete list, maybe contained in an enum - where you'd be able to select which name to use with a number.
03/11/2009 20:54 Rofa300#7
@unknownone : what i wanted to Learn is HOW to show TEXT if the User Inputted Another Text Or Number , just for Learning