Top DevX Stories
Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Search the forums:

Go Back   DevX.com Forums > DevX Developer Forums > C++

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 10-25-2009, 07:19 PM
fantasyfootball fantasyfootball is offline
Registered User
 
Join Date: Oct 2009
Posts: 9
Writing a program with these kind of things

I already did this work but my teacher told me to post it on this site and see if anybody else got the same thing. Follow the steps


Write a program to validate date. The program will prompt user to enter a date in the format of mm/dd/yyyy. The entered date is validated for month, day and year, and a message is displayed regarding whether the date is valid. The program will keep on running as long as user do not enter end-of-file as input (<Ctrl>-z on PC or <Ctrl>-d on Unix to mimic end-of-file on keyboard input)

Here are the rules that must be followed to verify a date.

year, yyyy, must be greater than 999 and less than 10,000
month, mm, must be between 1 & 12
day, dd, must be valid for the month and year.
month of 1, 3, 5, 7, 8, 10 and 12 must have days between 1-31
month of 4, 6, 9, and 11 must have days between 1-30
month of February, 2, has 28 days if it is NOT a leap year
has 29 days if it IS a leap year


A leap year is generally thought of as being any year divisible by 4. However, this is not always true. The exception is century years (divisible by 100). A century year is a leap year only if it is also divisible by 400.

Example:

A non-century is a year that is not divisible by 100. A non-century is a leap year if it is divisible by 4:

Year 1996 1996 % 4 == 0 IS leap year
Year 1991 1991 % 4 != 0 IS NOT leap year

A century year is always divisible by 4. It is leap year only if it is also divisible by 400.

Year 2000 2000 % 400 == 0 it IS a leap year
Year 1400 1400 % 400 != 0 it is NOT a leap year

You should have three functions. The following are the suggested prototypes:

void getDate( int&, int&, int&); // get month, day and year from user and store them
// in reference arguments sent by caller
int ckDate( int, int, int); // check date based on three int values sent by caller
// return an int indicating the status of the date
void displayMsg( int); // display a message based on the date status

Your ckDate function should return an integer value depending on the status of the date being ckecked:

Value: 0 = Good Date
1 = Bad Year
2 = Bad Month
3 = Bad Day not 1-31
4 = Bad Day not 1-30
5 = Bad Day not 1-29
6 = Bad Day not 1-28

Based on the return value of ckDate(), the corresponding message is displayed by the displayMsg() function.

Note:
It is important to figure out the algorithm before coding. When a program uses multiple functions, the algorithm is divided into levels. If the main calls ckDate function, then the algorithm for main is simply call ckDate to check date and don’t need mention about how to check a date. The detail of how to check a date would be the algorithm of ckDate function. The analysis of chapter 6 and 7 examples should be very helpful.

You are required to write the algorithm of the main function, i.e., level 0, and place it at the top of the code as comments.

Do not use any global variables!

Use meaningful and convenient variable names, proper indentation, appropriate comments, and good prompting messages. You are graded on both correctness and program style.

Note, don’t enter the leading 0 in case month or day is single digit, even the prompt asks for mm/dd/yyyy. Below is a sample run:

Enter a date (mm/dd/yyyy): 1/2/1998

1/2/1998 - Good Date

Enter a date (mm/dd/yyyy): 14/2/2000

14/2/2000 - Bad Month

Enter a date (mm/dd/yyyy): 2/33/1100

2/33/1100 - Bad Day not 1-28

Enter a date (mm/dd/yyyy): Ctr+Z

Good Bye!
Reply With Quote
  #2  
Old 10-26-2009, 11:53 PM
fantasyfootball fantasyfootball is offline
Registered User
 
Join Date: Oct 2009
Posts: 9
anyone know how to start it off

anyone know how to start it off
Reply With Quote
  #3  
Old 10-27-2009, 09:17 AM
jonnin jonnin is offline
Senior Member
 
Join Date: Dec 2003
Posts: 3,008
Quote:
Originally Posted by fantasyfootball View Post
anyone know how to start it off
You said you already did it? What are you asking? C++ already has date and time functions that you can use, or a fairly simple set of conditions can be used if you want. A series of if statements works.
Reply With Quote
  #4  
Old 10-27-2009, 11:06 AM
fantasyfootball fantasyfootball is offline
Registered User
 
Join Date: Oct 2009
Posts: 9
I cant

I cant do it, I dont know how to read it. I would like someone to do it so I can re make my mistakes.
Reply With Quote
  #5  
Old 10-27-2009, 01:50 PM
jonnin jonnin is offline
Senior Member
 
Join Date: Dec 2003
Posts: 3,008
Well, we cannot do it for you, its against the rules of the site to do homework for others. You have to give it a good try, post your broken code, and get some help after we are convinced you are really trying to do it.

This one is pretty easy though, so a few thoughts to get you rolling:

cin >> month;
cin >> day;
cin >> year;

if(month < 1 || month > 12)
return month_error;
else
if( day < 1 || day > 31) //catch the really stupid day errors like -21 or 163 etc.
return day_error;
else
.... //more if statements to determine if day is legal based on the month and such.


Thats all you need, a function that checks it and returns the error code, and the other functions asked for are trivial, a print statement and cin/cout statements to prompt the user and get a date, and a main to loop over the whole thing or whatever.


There are more elegant solutions to this problem, of course. And even the problem is broken, because your error codes do not "combine" to allow several conditions (ideally, if the day and month and year are all 3 bad, you can tell the user this, but by design flaw, you can only report one error!). The way to do this is with bits, say a char of 8 bits, 1 in a location means you have an error, 00000000 = no error, 00001010 is 2 errors, etc. But thats unrelated, just do as it asks.
Reply With Quote
  #6  
Old 10-28-2009, 04:21 PM
fantasyfootball fantasyfootball is offline
Registered User
 
Join Date: Oct 2009
Posts: 9
Hear is my work Is the LAST part correct

#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;

int month;
int day;
int year;
int messageNumber;
void getDate( int&, int&, int&);
int ckDate( int, int, int);
void displayMsg( int);

int main()
{
getDate(month, day, year);
messageNumber = ckDate (month, day, year);
displayMsg (messageNumber);
}

void getDate( int& month, int& day, int& year)
{
cout << "Enter a date (mm/dd/yyyy): ";
cin >> month, day, year;
}

int ckDate( int month, int day, int year)
{
int messageNumber;
if( year >= 999 && year <= 10000)
{
if ( month >= 1 && month <= 12)
{
switch(month)
{
case 1,3,5,7,8,10,12:
if (day >= 1 && day <= 31)
messageNumber = 0; //Good date message
else
messageNumber = 3;// bad day not 1-31
break;
case 2:
if (year % 4 == 0 && year % 100 > 0)
{
if (day >= 1 && day <= 29)
messageNumber = 0; // Good date message
else
messageNumber = 4; // bad day not 1-29
}
else
{
if (day >= 1 && day <= 28)
messgeNumber = 0; // good date message
else
messageNumber = 5; // bad day not 1-28
}
break;
case 4,6,9,11:
if (day >1 && day <= 30)
messageNumber = 0; // Good date message
else
messageNumber = 6; // bad day not 1-30
break;
}
}
else
{
messageNumber = 2; //Bad Month
}
}
else
{
messageNumber=1; // Bad year
}
return messageNumber;
}

void displayMsg( int messageNumber)
{
switch (messageNumber)
{
case 0:
cout << month << '/' << day << '/' << year << " - good date message" << endl;
break;
case 1:
cout << month << '/' << day << '/' << year << " - good date" << endl;
break;
case 2:
cout << month << '/' << day << '/' << year << " - good date" << endl;
break;
case 3:
cout << month << '/' << day << '/' << year << " - bad month" << endl;
break;
case 4:
cout << month << '/' << day << '/' << year << " - good date" << endl;
break;
case 5:
cout << month << '/' << day << '/' << year << " - bad day not 1-28" << endl;
break;
case 6:
cout << month << '/' << day << '/' << year << " - good date" << endl;
break;
}
}
Reply With Quote
  #7  
Old 10-28-2009, 04:35 PM
jonnin jonnin is offline
Senior Member
 
Join Date: Dec 2003
Posts: 3,008
Without checking every possible logical branch, it looks good to me.

Compile it and fix any syntax errors if you have not yet done so (I didnt see any, but the compiler can see things a person will miss in a quick read) and test it with some correct dates and some dates that are incorrect for the various cases (day 30 month 2, day 31 month 4, etc) and make sure it seems to work in all situations.

It should be really close, but convince yourself that it works and practice your debugging if it does not =)


-----------EDIT

What the heck I compiled it for you.

first, case with commas didnt take.

Try it like this:

case 1:
case 3:
//etc, for each of 5,7,8,10,12:
//case 12 looks like the body you already have:
case 12:

if (day >= 1 && day <= 31)
messageNumber = 0; //Good date message
else
messageNumber = 3;// bad day not 1-31
break;

You will have to do that for any case with commas that you have.

You misspelled a variable name: messgeNumber

-------------

execution:

Enter a date (mm/dd/yyyy): 12/34/5678
12/0/0 - good date
Press any key to continue . . .

looks like something is not quite right... time to debug it.

Ah.... you cannot use commas much in this language. There are one or two places, such as variables (int x,y,z for example) but in general, they do not work.

This does not work:

cin >> month, day, year;

It only reads in the month.
it should be
cin >> month >> day >> year;

but this does not work either, because the input is padded with / so:
char ch;
cout << "Enter a date (mm/dd/yyyy): ";
cin >> month >> ch >> day >> ch >> year; ///read and discard the padding
I leave the rest of it to you.

Last edited by jonnin; 10-28-2009 at 04:50 PM.
Reply With Quote
Reply

Bookmarks

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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
File writing program OOP sitha C++ 6 12-05-2006 07:41 AM
Help with Server program sedricbenson@ho C++ 2 11-07-2006 09:18 AM
Connecting to a Server Program HELP sedricbenson@ho C++ 2 11-07-2006 08:58 AM
processing additional data when program is running Ervin Rodriguez Database 1 05-16-2003 09:45 AM
Stop the program until copying has been completed Sinni VB Classic 1 11-26-2002 01:56 PM


All times are GMT -4. The time now is 11:24 PM.


Sponsored Links



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.