C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 26th, 2004, 02:01 PM
yaz420 yaz420 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 2 yaz420 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 43 sec
Reputation Power: 0
Validating Dates in C++

I am currently writing a simple numerology program. I am having problems validating the dates entered by a user. The major source of the problem is how to make sure the date the user entered is actually within the month (for example: 4/31 can’t be entered). This is what I currently have for checking dates:
Code:
int month, day, year;

do
{

	// In here I have the program asking the user for the date. I also have an if/else
	statement telling the user which part of the date is wrong.


}
while( month<1 || month>12 || day<1 || day>31 || year<1800 || year>3500);


Any hints would be great. If you give some type of example of how I could do this I ask that the example be basic c++. I can’t use functions on this program. I am pretty limited on what I can and cannot use.

Reply With Quote
  #2  
Old September 27th, 2004, 03:17 PM
The Phantom The Phantom is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: In the Dark City
Posts: 46 The Phantom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to The Phantom
lol

Can you use strings??
use it then use the cout << month << "/" << day << endl;

Reply With Quote
  #3  
Old September 30th, 2004, 04:40 PM
Kernel Mustard Kernel Mustard is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 32 Kernel Mustard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
With the exception of the freak month (Feb) that can be handled with a special case, we know that every second month has
the same amount of days up until aug, at which point things are reversed. Do you know the modulus operator "%" (remainder of integer division), we can use it like so:

int month = the number of the month (with Jan being 1, feb 2 ...)
int day = day to check;

if ( month <= 7) { //less than or equal to july
if ((month % 2) == 0) {
//every second month from feb to june
if (month == 2) {
//deal with feb
if (day > 28) //or 29, or however you choose to deal with this one
DO SOMETHING HERE
}
else
if (day > 30)
DO SOMETHING HERE
}
else {
//every second month jan to july (31 days)
if (day > 31)
DO SOMETHING HERE
}
}

else { //greater than july
if ((month % 2) == 0) {
//every second month from aug to dec (31 days)
if (day > 31)
DO SOMETHING HERE
}
else {
//every second month sept to nov (30 days)
if (day > 30)
DO SOMETHING HERE
}

}


Hope this helps,
Kernel Mustard

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Validating Dates in C++


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway