-2

I'm being asked to write a program to validate a date entered by a user. The date must be entered as three integer variables representing day, month and year. Output must state whether or not the date is valid.

Sounds simple enough... All I need to do is ask the user to input a day, Month and year. Then recall this and display it as a date. However I may want the user to enter the month first as this way I'll be able to work out how many days are in that month.

What I think I need to do is start with an array or 3 I need to get the program to remember how many days there are in each month so if the month is entered as February it wont accept anything greater than 29 is an input.

Before I start to evening look at Visual Studio, am I think anywhere along the right lines for this? Or is there another angle I can take to this?

Okay so I've been working on this and this is what I've got.

int monthentered = 0;
            int dayentered = 0;
            int year = 0000;
            int [] month = new int [12];
            int [] day = new int [31];
            bool leap = false;

            for (int x = 0; x <= 11; x++)
            {
                month[x] = x+1;
            }

            for (int x = 0; x <= 30; x++)
            {
                day[x] = x+1;
            }
            Console.WriteLine("Please enter a year...");
            year = (Convert.ToInt16(Console.ReadLine()));
            Console.WriteLine("{0}", year);

            Console.WriteLine("Please enter a month...");
            monthentered = (Convert.ToInt16(Console.ReadLine()));

            Console.WriteLine("Please enter a day...");
            dayentered = (Convert.ToInt16(Console.ReadLine()));

            while (monthentered == 01 || 03 || 05 || 07 || 08 || 10 || 12)
            {
                while (dayentered == 31)
                {
                    Console.WriteLine("There are only 30 days in this month please re-enter your day...");
                    dayentered = (Convert.ToInt16(Console.ReadLine()));
                }
            }

            while (monthentered == 02)
            {
                while (dayentered > 28)
                {
                    Console.WriteLine("There are only 28 days in this month please re-enter your day...");
                    dayentered = (Convert.ToInt16(Console.ReadLine()));
                }
            }



            Console.WriteLine("{0}/{1}/{2}", dayentered, monthentered, year);
            Console.ReadKey();

Issue with the while monthenter = bit. can someone provide me with help as to what I've done wrong?

Brucie67
  • 21
  • 1
  • 1
    Just try. You will learn more from trying than you would from getting us to validate your ideas (or not validate them, as the case may be). – Oded Nov 13 '12 at 13:56
  • have done googling on this issue? – Freelancer Nov 13 '12 at 13:57
  • I've got possibly the worst programming teacher in the history of teaching. I'm repeating the whole first year because he hasn't got any teaching skills. and 80% of my class is going the same thing or left. I'm not asking anyone for code or for anyone to validate my thougts, well okay maybe I am, What I'm looking for here is a logical mind to help me understand what I'm doing because I don't feel my teacher can do it, For the second year running. – Brucie67 Nov 13 '12 at 14:02
  • googling is the best option you have got. I can understand your situation. But always try to put some code whenever you posts your question. It shows that you have tried something and also helps others to direct you in the correct direction. Best Of Luck – Freelancer Nov 13 '12 at 14:07
  • Thanks freelancer. I've something to go on here now so I'll go try make up attempt at this and hopefully I won't be back because I'll have got this but if I do return I will bring with me any and all code I have done. – Brucie67 Nov 13 '12 at 14:12

2 Answers2

1
bool inputOk = false;
try
{
    try
    {
        int y = int.Parse(yearTextBox.Text);
        int m = int.Parse(monthTextBox.Text);
        int d = int.Parse(dayTextBox.Text);
        inputOk = true;
    }
    catch
    {
        Debug.Writeline("Invalid input");
    }

    if(inputOk)
    {
        DateTime value = new DateTime(y, m, d);
        Debug.Writeline("It's a valid date");
    }
}
catch
{
    Debug.Writeline("It isn't a valid date");
}
Yván Ecarri
  • 1,661
  • 18
  • 39
  • This code is bound to fail. It would be much better to use `DateTime.ParaseExact` in a case like this as Candide suggested. – Security Hound Nov 13 '12 at 14:09
  • 1
    Why is it boud to fail? Brucie67 said the input is in 3 integer variables so my solution take this in account. ParseExact is more difficult to use and you have to build a string from the three integers and pass a formatter (for which you have to decide what format to use and build the string accordingly) I see no case in building such a string whereas the input is already in 3 variables. – Yván Ecarri Nov 13 '12 at 14:26
  • Because you would thrown an exception when the input of the textbox was not an integer which isn't clean code. – Security Hound Nov 13 '12 at 14:52
  • No I won't throw an exception (well, I will but I'll quickly capture it). I'll show an output stating that the input is not a valid date, which is a functional requirement. We can discuss about the cleannes of using exceptions to comply functional requirements; after all, Int32.Parse uses such a mechanism by design and it's part of the CLR core. I can argue on the convenience or not to make things this way but the language provides this mechanism and I am using it the best I can to give a solution that is easily undesrtood. – Yván Ecarri Nov 13 '12 at 15:08
  • Which is the reason using ParseExact is better in a case like this. In a case like this ParseExact would not throw an exception, when the input could not be converted into a DateTime, but return a value that one can expect. Because invalid input and invalid date are two entirely different errors. – Security Hound Nov 13 '12 at 15:18
  • Ok, Agreeed on the subtle difference between invalid input and invalid date :-P It can be also controlled by other means and the requirement states that the input "entered as three integer variables representing day, month and year." – Yván Ecarri Nov 13 '12 at 15:25
0

DateTime.ParseExact can do the parsing for you provided you give it a format.

The question has been answered here before. Your approach using arrays will work but the .NET API is far better.

Community
  • 1
  • 1
Candide
  • 30,469
  • 8
  • 53
  • 60