473,382 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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.

Sep 1 '05 #1
5 3768
> Is it possible for us to check if a date is valid?
Yes
Should I declare the date thingy in the array? Depends on how you want to store the date, you can also for example
store it in a single integer or a structure.
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?

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.

Sep 1 '05 #2
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.

Sep 2 '05 #3
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.

Sep 2 '05 #4
RayKnight wrote:
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.


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
Sep 2 '05 #5
RayKnight wrote:

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.


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
kb******@gascad.at
Sep 5 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ZMAN | last post by:
Hello all. Is there an easy way to populate this array with only the working days (no Sat or Sun) from the last 60 days. for($i = 1; $i <= 60; $i++) { $pastDates = date("Y-m-d", mktime(0,...
4
by: Treetop | last post by:
I have a script for my church that we use for the weekly events. I currently have it as week of Feb 1, 2003 at the top, then list Sun - Sat below the date. I have been asked to put the date next...
2
by: Tjerk | last post by:
Hello all, I have the script below to change an image depending on the date upto january it worked fine but then it just stopped working does anybody have an idea how I can make it work again or...
1
by: zoneal | last post by:
Hi, I am quite new in VB.NET and I was wondering if someone could help me with this problem: I declared the following array: dim ArrayList() as date Problem is, whenever I add a date to the...
2
by: johndcal | last post by:
Hello All, I have a date value that I pull from a .csv file. After reading the file and storing the values in an array the value of the date could be found in $array, for example....
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
9
by: mistral | last post by:
Need help to remove list of days from date script. Need format "June 07, 2006" <SCRIPT LANGUAGE="JavaScript"> <!-- Begin // Get today's current date. var now = new Date();
5
by: mabond | last post by:
Hi Having trouble filling a datetime filed in SQL table with a value from an array. The original source is a comma-delimited text file where the date and time values are in two columns. I...
1
by: aRTx | last post by:
<? /* Directory Listing Script - Version 2 ==================================== Script Author: Artani <artan_p@msn.com>. www.artxcenter.com REQUIREMENTS ============ This script requires...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.