Date in array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RayKnight

    Date in array?

    Is it possible for us to check if a date is valid? Should I declare the
    date thingy in the array?

    I'd want the user to check the date. Used cin, they keyed in dates
    like...

    015204, it worked...

    Is there anyway to check if the date the user put is valid?

    Thanks.

  • velthuijsen@hotmail.com

    #2
    Re: Date in array?

    > Is it possible for us to check if a date is valid?
    Yes
    [color=blue]
    > Should I declare the date thingy in the array?[/color]
    Depends on how you want to store the date, you can also for example
    store it in a single integer or a structure.
    [color=blue]
    > I'd want the user to check the date. Used cin, they keyed in dates
    > like...
    >
    > 015204, it worked...
    >
    > Is there anyway to check if the date the user put is valid?[/color]
    Yes.

    Before you go any further think of the following things:

    1) Spend some time looking up the different ways your OS can return a
    date.
    2) How you want the user to input the date.
    3) What do you need to do so you can compare the data retrieved from
    the OS with the data you received from the user.

    Then if you can't get your program to work, please post the program and
    the people around here might be able to better help you.

    Comment

    • john_andronicus@hotmail.com

      #3
      Re: Date in array?

      Yes of course it is possible.

      No it's probably not a good idea to put it in an array. But if that is
      the only way you can think of to make it work then go ahead.

      First you have to decide what is a valid date, this is not necessarily
      a simple task. Then you have to work out some rules that tell the
      difference between a valid date and an invalid one. Then you have to
      put those ruls into code. This is normal programming.

      Comment

      • RayKnight

        #4
        Re: Date in array?

        I mean, how do you split up the numbers if you put 'em together? I thought
        of 'pairing' the numbers up together. So it checks if it is above 31 and
        stuff after it check the month. And so on and so fore.

        Comment

        • John Harrison

          #5
          Re: Date in array?

          RayKnight wrote:[color=blue]
          > I mean, how do you split up the numbers if you put 'em together? I thought
          > of 'pairing' the numbers up together. So it checks if it is above 31 and
          > stuff after it check the month. And so on and so fore.
          >[/color]

          Well I think you are already making assumptions, you can't pair anything
          up unless your user puts in an even number of characters.

          I would input into a string

          string date;
          cin >> date;

          then check the length (I'm assuming you want a length of 6)

          if (date.size() != 6)
          cerr << "it's not a date";

          then check all digits

          if (date[0] >= '0' && date[0] <= '9' && date[1] >= '0' && date[1] <= '9'
          && ...)
          cerr << "it's not a date";

          then split into three numbers (I'm assuming day, month, year which is
          the European way of doing it)

          int day = 10*(date[0] - '0') + (date[1] - '0');
          int month = 10*(date[2] - '0') + (date[3] - '0');
          int year = 10*(date[4] - '0') + (date[5] - '0');

          then check the values of day, month, year.

          etc. etc.

          If you don't know how to use the string class in C++ then you should
          learn. Failing that you could use an array of char.

          John

          Comment

          • Karl Heinz Buchegger

            #6
            Re: Date in array?

            RayKnight wrote:[color=blue]
            >
            > I mean, how do you split up the numbers if you put 'em together? I thought
            > of 'pairing' the numbers up together. So it checks if it is above 31 and
            > stuff after it check the month. And so on and so fore.[/color]

            It all depends on how you represent the concept of 'date'.
            There are vsrious choices.

            You could eg. do:

            * hold day, month, year in seperate int variables:

            int day, month, year;

            cout << "Enter date as DD MM YYYY ";
            cin >> day >> month >> year;

            now you have 3 int variables, holding the day, the month and the
            year.
            Checking that should now be straightforward . Eg. month
            if( month < 1 || month > 12 )
            cout << "Invalid date, month is not in the range 1 .. 12\n"

            Here you rely on the user of your program to input 3 distinct numbers

            * hold the day, month, year in seperate variables but read just 1 integer

            int day, month, year;
            int TotalDate;

            cout << "Enter date as DDMMYYYY ";
            cin >> TotalDate;

            The user will input a number, eg. 05092005

            You now will have to split this number into pieces. For this / and %
            are usefull operations.

            Year = TotalDate % 10000;
            TotalDate /= 10000 // 5092005 / 10000 -> 509
            Month = TotalDate % 100
            TotalDate /= 100 // 509 / 100 -> 5
            Day = TotalDate;

            Now you can proceed as in the first case in doing your checks.

            * Well. Relying on the user to input numbers can work or may not work. It
            all depends on the user. If he plays the game safe and inputs the numbers
            as you want them, fine. If not: There isn't much you can do about it, then
            to recover from that error, put up an error message and continue. But that
            recovering can become a hard job.

            Most of the time it is far simpler, to let the user enter into a string.
            You still want the user to enter in a specific format. But the difference
            is: The standard input routines for numbers will cease to work if the user
            enters something strange, while the standard input functions for strings
            simply copy everything the user typed to the string variable. Your program
            now has a chance to inspect what the user actually typed, *before* you try
            to convert everything to numbers. If your program decides that the user
            didn't enter 3 numbers (or 1 number) then the program can output a gracefull
            error message and repeat the input without the need to worry about the standard
            input functions.
            Once you have a valid string, convert it into 3 (or 1) number and proceed as in
            one of the first 2 cases.

            * You could encapsulate all of this into a class of its own

            * and so on. From now on the possibilities are endless. But it all boils down to
            * You are the programmer
            * You decide on how you want to store a 'date'
            * You decide on how you want to input a 'date' from the user
            * Dont' be frightened by implementing one scheme, figuring out that it is not
            the best scheme since sliced bread, throw it away and implement another scheme.
            * Implementing something and throwing it away actually *is* a form of learning.

            --
            Karl Heinz Buchegger
            kbuchegg@gascad .at

            Comment

            Working...