CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2011
    Posts
    1

    Unhappy Help - Program without use of Date/Time Function

    - You are supplied with a date which should be in the form day in the month (1 to 31), month (1 to 12), year (1760 onwards)

    - check the validity of the date (above limits are observed, day number is valid for the month – i.e 31, 6 is not valid as there are only 30 days in June)

    - check for leap years; a leap year occurs when the year is divisible either by 400, or by 4 but not 100

    - produce a string with the day within the month followed by “st”, “nd”, “rd” or “th” as appropriate, followed by the full month name, followed by a comma and then the year. So 22, 1, 1944 should produce the string:
    22nd January, 1944
    Firstly to be absolutely clear, I can NOT rely on methods provided by the C# environment, so DateTime is out of the question.

    I am relatively new to programming and have recently been set a series of challenges by my tutor. I have been able to tackle most successful all be it with a bit of help here and there, but I am becoming increasingly frustrated by this question. I have figured out the formulae to calculate leap years, but I am struggling to attain the requirements in regards to the user input and output. It has been suggested to me by a friend that I use either a substring or split string in regards to attaining date input in the format of dd/mm/yyyy. However, I have no experience of using either function and am looking for some advice as to which would be more appropriate and perhaps easier to implement in this circumstance. I am also at a loss as to output the date as the specification states with the month name and st, nd etc. after the day.

    I imagine that this more than likely seems like a noobish problem that most adequate programmers would tackle and solve without much trouble, but I am becoming desperate. Any pointers in the right direction would be greatly appreciated.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Help - Program without use of Date/Time Function

    Quote Originally Posted by IFHP View Post
    I imagine that this more than likely seems like a noobish problem that most adequate programmers would tackle and solve without much trouble, but I am becoming desperate. Any pointers in the right direction would be greatly appreciated.
    This is the type of program that you will only get in a class. An experienced programmer would use an existing framework class like DateTime to solve the problem. In the real world, no one would hand write their own DateTime class (and if they did, it would never pass review).


    Quote Originally Posted by IFHP View Post
    It has been suggested to me by a friend that I use either a substring or split string in regards to attaining date input in the format of dd/mm/yyyy. However, I have no experience of using either function and am looking for some advice as to which would be more appropriate and perhaps easier to implement in this circumstance.
    When someone suggests an approach, pay attention. The good news is that you did, because you wrote down what they told you (i.e. substring and split). The next thing to do is to know where to find information about these two 'items'.

    The best resource for looking up C# classes and methods is Msdn (The Microsoft Developer Network).

    http://msdn.microsoft.com/library/default.aspx

    Enter 'substring' in the search box and see what you get back.

    Understand that you are going to get back items that aren't relevant (such as the first 'SUBSTRING' link in the results. This is for transact sql (database stuff). The next item in the result is 'String.SubString Method'. If you click on this link, you'll see at the top something like .Net Framework 1.1 and a link to other versions. Click on framework 3.5. This is the .Net framework (which C# is one of it's languages). In general, since you are coding in C#, you'll always want to look for results in the .Net framework.

    Next, search for the word 'split'. The first result here is 'String.Split'. When you click on it, you'll see .Net Framework 3.5, so you know it's in the .net framework and you're in the right place. Click on the first Split(Char[]) overload.

    Scroll down and take a look at the example section (be sure to click on the C# tab).

    You'll see the following:
    using System;

    Code:
    public class SplitTest {
        public static void Main() {
    
            string words = "This is a list of words, with: a bit of punctuation" +
                           "\tand a tab character.";
    
            string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });
    
            foreach (string s in split) {
    
                if (s.Trim() != "")
                    Console.WriteLine(s);
            }
        }
    }
    By reading the documentation, and creating a little test project that runs the sample code, you can get a pretty good idea of what the differences are between split and substring.

    Backing up a bit at first you need to understand the difference between a class and a method. Split() and SubString() are methods of the class String.

    In the future, if someone suggests to use something, if you don't know ask them if it's a class or a method (if it's a method, ask them what class it's in).

    Then you can go to Msdn and look it up, see the example code and try it out.

    Once you learn how to manipulate the strings, you need to find out how to get the data from the user. If you prompt the user to enter a date in the form of mm/dd/yyyy, you can use String.Split or String.SubString to extract out the month, day and year components.

    To prompt the user, check out Console.WriteLine to display text to the screen. To retrieve data from the user, check out Console.ReadLine in msdn.

    Good luck.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured