Determine your zodiac sign

ddanbe 0 Tallied Votes 3K Views Share

Fill in your birthday in the DateTime structure and get your sign as a string.
Get an extra (an element associated whith the sign) via the out parameter thanks to C#!
This switch statement uses returns all over the place, so a break staement is not needed.

public string ZodiacSign(DateTime date, out string element)
        {
            switch (date.Month)
            {
                case 1: if(date.Day <= 20)
                    { element = "Earth"; return "Capricorn"; }
                    else
                    { element = "Air"; return "Aquarius"; }
                case 2: if (date.Day <= 19)
                    { element = "Air"; return "Aquarius"; }
                    else
                    { element = "Water"; return "Pisces"; }
                case 3: if (date.Day <= 20)
                    { element = "Water"; return "Pisces"; }
                    else
                    { element = "Fire"; return "Aries"; }
                case 4: if (date.Day <= 20)
                    { element = "Fire"; return "Aries"; }
                    else
                    { element = "Earth"; return "Taurus"; }
                case 5: if (date.Day <= 21)
                    { element = "Earth"; return "Taurus"; }
                    else
                    { element = "Air"; return "Gemini"; }
                case 6: if (date.Day <= 22)
                    { element = "Air"; return "Gemini"; }
                    else
                    { element = "Water"; return "Cancer"; }
                case 7: if (date.Day <= 22)
                    { element = "Water"; return "Cancer"; }
                    else
                    { element = "Fire"; return "Leo"; }
                case 8: if (date.Day <= 23)
                    { element = "Fire"; return "Leo"; }
                    else
                    { element = "Earth"; return "Virgo"; }
                case 9: if (date.Day <= 23)
                    { element = "Earth"; return "Virgo"; }
                    else
                    { element = "Air"; return "Libra"; }
                case 10: if (date.Day <= 23)
                    { element = "Air"; return "Libra"; }
                    else
                    { element = "Water"; return "Scorpio"; }
                case 11: if (date.Day <= 22)
                    { element = "Water"; return "Scorpio"; }
                    else
                    { element = "Fire"; return "Sagittarius"; }
                case 12: if (date.Day <= 21)
                    { element = "Fire"; return "Sagittarius"; }
                    else
                    { element = "Earth"; return "Capricorn"; }
                default:
                    element = ""; return "";
            }
        }
saurabh.dubey.372 0 Newbie Poster

If you can explain the working of the above program , then it will be great.

ddanbe 2,724 Professional Procrastinator Featured Poster

Hi, saurabh.dubey.372 welcome here at DaniWeb. :)
First of all it is not a program it is a method that you can incorporate in your programs.
What exactly do you need explaining about?

NetJunkie 29 Junior Poster

I wrote a C# console application that takes the switch statement and implements that into the main method of the .cs file. I added comments in my code so you can see what I am doing step-by-step. I also explain the switch statement in the comments to help you better understand what is happening when it takes and checks the DateTime object against the cases. This can be done much easier but I assume you are new to development and I want to make sure I use the "long" way so that you understand everything that is happening.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyZodiac
{
    class Program
    {
        static void Main(string[] args)
        {
            //Sets the title and the color of the text in the console window
            Console.Title = "My Zodiac by Chris Harris";
            Console.ForegroundColor = ConsoleColor.Green;  
            Console.Clear();

            //Creates the string to be captured from input and the DateTime object needed for comparison
            string bDay;
            DateTime date = new DateTime();

            //Asks for the birthday of the user
            Console.WriteLine("Please enter your birthday: (i.e. MM/DD/YYYY)");
            bDay = Console.ReadLine();
            Console.WriteLine("\n");

            //Takes the string and converts it to our DateTime object
            date = Convert.ToDateTime(bDay);

            //This switch statement is very simple.
            //The case determines the number that represents the month.
            //So for example 1 is January and 19 is less than or equal to 20.
            //Since these decisions are met, the application will return Capricorn as the zodiac sign.

            //Another example would be if the month was February (month #2) and the day was the 1st, the application would return Aquarius
            //The application also waits for the user to click any key to exit the application

            switch (date.Month)
            {
                case 1: if (date.Day <= 20)
                    { Console.WriteLine("Capricorn"); Console.ReadKey(); }
                    else
                    { Console.WriteLine("Aquarius"); Console.ReadKey(); }
                    break;
                case 2: if (date.Day <= 19)
                    { Console.WriteLine("Aquarius"); Console.ReadKey(); }
                    else
                    { Console.WriteLine("Pisces"); Console.ReadKey(); }
                    break;
                case 3: if (date.Day <= 20)
                    { Console.WriteLine("Pisces"); Console.ReadKey(); }
                    else
                    { Console.WriteLine("Aries"); Console.ReadKey(); }
                    break;
                case 4: if (date.Day <= 20)
                    { Console.WriteLine("Aries"); Console.ReadKey(); }
                    else
                    { Console.WriteLine("Taurus"); Console.ReadKey(); }
                    break;
                case 5: if (date.Day <= 21)
                    { Console.WriteLine("Taurus"); Console.ReadKey(); }
                    else
                    { Console.WriteLine("Gemini"); Console.ReadKey(); }
                    break;
                case 6: if (date.Day <= 22)
                    { Console.WriteLine("Gemini"); Console.ReadKey(); }
                    else
                    { Console.WriteLine("Cancer"); Console.ReadKey(); }
                    break;
                case 7: if (date.Day <= 22)
                    { Console.WriteLine("Cancer"); Console.ReadKey(); }
                    else
                    { Console.WriteLine("Leo"); Console.ReadKey(); }
                    break;
                case 8: if (date.Day <= 23)
                    { Console.WriteLine("Leo"); Console.ReadKey(); }
                    else
                    { Console.WriteLine("Virgo"); Console.ReadKey(); }
                    break;
                case 9: if (date.Day <= 23)
                    { Console.WriteLine("Virgo"); Console.ReadKey(); }
                    else
                    { Console.WriteLine("Libra"); Console.ReadKey(); }
                    break;
                case 10: if (date.Day <= 23)
                    { Console.WriteLine("Libra"); Console.ReadKey(); }
                    else
                    { Console.WriteLine("Scorpio"); Console.ReadKey(); }
                    break;
                case 11: if (date.Day <= 22)
                    { Console.WriteLine("Scorpio"); Console.ReadKey(); }
                    else
                    { Console.WriteLine("Sagittarius"); Console.ReadKey(); }
                    break;
                case 12: if (date.Day <= 21)
                    { Console.WriteLine("Sagittarius"); Console.ReadKey(); }
                    else
                    { Console.WriteLine("Capricorn"); Console.ReadKey(); }
                    break;
                default:
                    Console.WriteLine("Your zodiac sign was not found! Please try again!");
                    Console.ReadKey();
                    break;
            }
        }
    }
}
ddanbe 2,724 Professional Procrastinator Featured Poster

Nice work NetJunkie.
But why all those Console.ReadKeys?
You only need one Console.ReadKey(); after line 104, out of the switch.
Once the switch case has decided that it is say "Capricorn", it falls out and continues after line 104.
If you would like my method approach you would define it in a Console app like this:
public static string ZodiacSign(DateTime date, out string element)
etc.
You would call it like this:

string MyElement = string.Empty;
string MySign = ZodiacSign(date, out MyElement);
Console.WriteLine(MyElement,MySign);

If you prefer to stick with your method, please do, but arrange the ReadKey.

NetJunkie 29 Junior Poster

ddanbe, yeah I have no idea what I was thinking. I put the Console.ReadKey(); at the end of the default case which didn't work properly and I didn't notice that I had put it in the case rather than outside of the switch. That was a poor mistake on my behalf. I guess that goes to show that you shouldn't code on limited sleep and in the middle of a college class! :D

ddanbe 2,724 Professional Procrastinator Featured Poster

To produce some good code that are BAD habits indeed. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.