need help with program

V

vgame64

Hi, I have been struggling with writing a program for a few hours. The
requirements are that: """You will be writing a program which will
determine whether a date is valid in
terms of days in that month. We are assuming that the year will be
valid 4 digit integer.
So you don't have to think much about that(in terms of validation)
except for the month
of February. If the month is February, then you have to check whether
that year is Leap
year or not before checking the date.
Create a class called Date. Include three private variables : date,
month and year
(declare as int type). Include following member functions:
1. A constructor Date(month. date, year)which will initialize the date,
month and
year of the object with user specified values.
2. valid_Date(): which will determine whether the date is valid or
not."""
That and we have to use a switch statement for the month validation.


I have the following:

"Date.h"
#include<iostream>

class Date
{
public:
Date(int,int,int);
void valid_Date();

private:
int month,date,year;
};







"Date.cpp"

#include <iostream>
#include<string>
using namespace std;




#include "Date.h"





Date::Date(int n1,int n2, int n3)

{
if ( n1 > 0 && n1 <= 12 )
month = n1;
else {
month = 1;
cout << "Month " << n1 << " invalid.";
}

date = n2;
year = n3;

cout << endl;
}



void Date::valid_Date()


{







if (month = 02)
{
if (year%2 == 0)
cout << "Your date is " << month << date << year << endl;
}
else
cout << "Your date is invalid!" << endl;



}

int main(void)
{
int m, d, y;


char ch;

cout << "Input a date as dd/mm/yy: ";
cin >> d >> ch >> m >> ch >> y;

switch (month) {
case 1: cout << "January"; break;
case 2: cout << "February"; break;
case 3: cout << "March"; break;
case 4: cout << "April"; break;
case 5: cout << "May"; break;
case 6: cout << "June"; break;
case 7: cout << "July"; break;
case 8: cout << "August"; break;
case 9: cout << "September";break;
case 10: cout << "October"; break;
case 11: cout << "November"; break;
case 12: cout << "December"; break;
}


return 0;
}



My mind wandered and tried multiple different approaches..and I ended
up with that crap...can some1 help me make the program work to the reqs
placed above..thanks!
 
J

John Carson

Hi, I have been struggling with writing a program for a few hours. The
requirements are that: """You will be writing a program which will
determine whether a date is valid in
terms of days in that month. We are assuming that the year will be
valid 4 digit integer.
So you don't have to think much about that(in terms of validation)
except for the month
of February. If the month is February, then you have to check whether
that year is Leap
year or not before checking the date.
Create a class called Date. Include three private variables : date,
month and year
(declare as int type). Include following member functions:
1. A constructor Date(month. date, year)which will initialize the
date, month and
year of the object with user specified values.
2. valid_Date(): which will determine whether the date is valid or
not."""
That and we have to use a switch statement for the month validation.


I have the following:

"Date.h"
#include<iostream>

class Date
{
public:
Date(int,int,int);
void valid_Date();

private:
int month,date,year;
};







"Date.cpp"

#include <iostream>
#include<string>
using namespace std;




#include "Date.h"





Date::Date(int n1,int n2, int n3)

{
if ( n1 > 0 && n1 <= 12 )
month = n1;
else {
month = 1;
cout << "Month " << n1 << " invalid.";
}

date = n2;
year = n3;

cout << endl;
}



void Date::valid_Date()


{







if (month = 02)

You have made an assignment, not checked for equality.
{
if (year%2 == 0)

This just checks for an even year. No point in doing that.
cout << "Your date is " << month << date << year << endl;
}
else
cout << "Your date is invalid!" << endl;



}

int main(void)
{
int m, d, y;


char ch;

cout << "Input a date as dd/mm/yy: ";
cin >> d >> ch >> m >> ch >> y;

switch (month) {
case 1: cout << "January"; break;
case 2: cout << "February"; break;
case 3: cout << "March"; break;
case 4: cout << "April"; break;
case 5: cout << "May"; break;
case 6: cout << "June"; break;
case 7: cout << "July"; break;
case 8: cout << "August"; break;
case 9: cout << "September";break;
case 10: cout << "October"; break;
case 11: cout << "November"; break;
case 12: cout << "December"; break;
}


return 0;
}



My mind wandered and tried multiple different approaches..and I ended
up with that crap...can some1 help me make the program work to the
reqs placed above..thanks!

You have done most of the work. Two suggestions.

1. Instead of

int month,date,year;

make it

int month,day,year;

i.e., replace date by day so that you are not using date in two different
senses.

2. You need to verify that the day supplied is <= the number of days in the
month. Thus your switch statement should be setting a maxDays variable.
 
V

vgame64

2. You need to verify that the day supplied is <= the number of days in
the
month. Thus your switch statement should be setting a maxDays variable.


I am sorry if this is obvious...but my brain is dead at this hour...how
do I go about doing that?

Thanks so much for the help
 
V

vgame64

Oh and once I do this....will it recognize the conditions i have
stated...because as of right now, when I run the program....It asks me
to input the data...and then it ends...^_~
 
J

Jim Langston

Hi, I have been struggling with writing a program for a few hours. The
requirements are that: """You will be writing a program which will
determine whether a date is valid in
terms of days in that month. We are assuming that the year will be
valid 4 digit integer.
So you don't have to think much about that(in terms of validation)
except for the month
of February. If the month is February, then you have to check whether
that year is Leap
year or not before checking the date.
Create a class called Date. Include three private variables : date,
month and year
(declare as int type). Include following member functions:
1. A constructor Date(month. date, year)which will initialize the date,
month and
year of the object with user specified values.
2. valid_Date(): which will determine whether the date is valid or
not."""
That and we have to use a switch statement for the month validation.


I have the following:

"Date.h"
#include<iostream>

class Date
{
public:
Date(int,int,int);
void valid_Date();

private:
int month,date,year;
};







"Date.cpp"

#include <iostream>
#include<string>
using namespace std;




#include "Date.h"





Date::Date(int n1,int n2, int n3)

{
if ( n1 > 0 && n1 <= 12 )
month = n1;
else {
month = 1;
cout << "Month " << n1 << " invalid.";
}

date = n2;
year = n3;

cout << endl;
}



void Date::valid_Date()


{







if (month = 02)
{
if (year%2 == 0)
cout << "Your date is " << month << date << year << endl;
}
else
cout << "Your date is invalid!" << endl;



}

int main(void)
{
int m, d, y;


char ch;

cout << "Input a date as dd/mm/yy: ";
cin >> d >> ch >> m >> ch >> y;

switch (month) {
case 1: cout << "January"; break;
case 2: cout << "February"; break;
case 3: cout << "March"; break;
case 4: cout << "April"; break;
case 5: cout << "May"; break;
case 6: cout << "June"; break;
case 7: cout << "July"; break;
case 8: cout << "August"; break;
case 9: cout << "September";break;
case 10: cout << "October"; break;
case 11: cout << "November"; break;
case 12: cout << "December"; break;
}


return 0;
}

My mind wandered and tried multiple different approaches..and I ended
up with that crap...can some1 help me make the program work to the reqs
placed above..thanks!

First, when copying from VC++, copy the program, open up notepad
(start->run->notepad) and paste it, then copy it again. That way it
preserves your whitespace and easier to read in forums.

Second, the orignal specs said that year would be "valid four digit integer"
but you are only requesting 2 digits in cout << "Input a date as dd/mm/yy:
"; Easy to fix to dd/mm/yyyy.

Well, you developed your class with rudimentory date checking, but you never
instatize your class. After
cin >> d >> ch >> m >> ch >> y;
you should perhaps do
Date date(d, m, y);

I modified the program and did this and it reported that the month was
invalid for month 13 (good).

Now you need to work on your valid_Date. You need to build some sort of
table with the months of the year following the old addage "30 days has
September, April, June and November. All the rest have 31 except for
February which has 28 except on leap years it has 29" or something like
that. So you need a simple std::vector maybe with the values 31, 28, 31,
30, etc... for the 12 months. For any month other than february, just make
sure the day of the month is not 0 and <= this month's value.

For February you get the fun of figuring out when a leap year is.
Incidently, it's every 4 years, not every 2. And then there are century
rules. And the rules are:
Every year divisible by 4 is a leap year.
But every year divisible by 100 is NOT a leap year
Unless the year is also divisible by 400, then it is still a leap year.

So you need to take the 4 digit year, and follow those rules to see if
February has 28 or 29 days. Of course you only have to do this if the month
is february and the day is 29.
 
X

xPy

the first function returns the last day of the month depending on the
year (et) and the month (min).
the second function (disekto) returns true if the year is a leap
year... it is ysed in the first function to define the last day of
february....
if it seems like greek, it's because it is...

unsigned int telMera(int et , int min)
{
unsigned int fv;
if(min==1 || min==3 || min==5 || min==7 || min==8 || min==10 ||
min==12)
fv=31;
else if(min==4 || min==6 || min==9 || min==11)
fv=30;
else
if(disekto(et)) fv=29;
else 28;
return fv;
}


bool disekto(int et)
{
bool fv;
if(et%400==0) fv=true;
else if (et%4==0 && et%100!=0) fv=true;
else fv=false;
return fv;
}
 
J

Jim Langston

xPy said:
i have a whole class if you are intrested... but its all in greek....

Actually, xPy, this is his homework. We should give him pointers and show
him where he's going off course, but not do his homework for him, otherwise
how will he learn?
 
J

John Carson

2. You need to verify that the day supplied is <= the number of days
in the
month. Thus your switch statement should be setting a maxDays
variable.


I am sorry if this is obvious...but my brain is dead at this
hour...how do I go about doing that?

By defining a variable

int maxDays;

and setting its value for each month in the switch statement.
 
J

John Carson

Oh and once I do this....will it recognize the conditions i have
stated...because as of right now, when I run the program....It asks me
to input the data...and then it ends...^_~

Your program will execute the statements in main() and then end (it will
also call the constructors and --- later --- the destructors for any global
objects).

Global objects aside, if you want it to use code that you declare/define
outside of main(), then you need to bring that code into main() by declaring
variables (including those of class type) within main() and/or making
function calls within main().
 
M

Marcelo Pinto

(e-mail address removed) wrote:

[snip]
"Date.h"
#include<iostream>

You don't need this include in your header file.
class Date
{
public:
Date(int,int,int);
void valid_Date();

I believe valid_Date should be bool.

[snip]
private:
int month,date,year;

I would have named the variables: month_, day_, year_
};

"Date.cpp"

#include <iostream>
#include<string>
using namespace std;

#include "Date.h"

Date::Date(int n1,int n2, int n3)

n1, n2 and n3 are really bad names for parameters. You should provide
meaninful names. I would have made Date::Date(int month, int day, int
year), provided you have made the member variable names like I advised
above.
{
if ( n1 > 0 && n1 <= 12 )
month = n1;
else {
month = 1;
cout << "Month " << n1 << " invalid.";
}

date = n2;

Here you should have verified if n2 (day in my suggestion) is valid (1
<= day <= max day of month as suggested elsewhere in this thread).
year = n3;

cout << endl;
}

void Date::valid_Date()
{
if (month = 02)

ATTENTION:
1. it is an assignement not a comparison
2. when you initiates a number with 0 it is an octal number. In this
case there is no problem because 02 == 2, but beware that 010 = 8. When
you initiates a number with 0x it is an hexadecimal number.
{
if (year%2 == 0)
cout << "Your date is " << month << date << year << endl;
This test and its conclusion don't make sense.
}
else
cout << "Your date is invalid!" << endl;



}

int main(void)

Drop the void.

{
int m, d, y;


char ch;

cout << "Input a date as dd/mm/yy: ";

The indication for the year should be with 4 digits as pointed out by
someone else.

[snip]

HTH,

Marcelo Pinto.
 
D

Default User

Oh and once I do this....will it recognize the conditions i have
stated...because as of right now, when I run the program....It asks me
to input the data...and then it ends...^_~

You've probably noticed that most of the people here when they reply to
a message some have nice quotes and lines that let you know who said
what and all that. Wouldn't you like to do that as well? Of course, and
by following the instruction in my .sig below, you can! All for the low
low price of: FREE.


Brian
 
V

vgame64

Thanks for the help...i figured some of it out, but never got it fully
running. Now I am just interested in seeing what the proper code for
it would look like just for my own learning purpose if anyone can
refine the code to make it work.
 
O

osmium

Thanks for the help...i figured some of it out, but never got it fully
running. Now I am just interested in seeing what the proper code for
it would look like just for my own learning purpose if anyone can
refine the code to make it work.

I guess enough time has passed so that you have already turned in your
solution. Here is one way to do it:
-------------------
#include <iostream>

using namespace std;

// member(s) of this class might throw exceptions,
// they all have the same form:
// throw(string message)
class Date
{
public:
Date(int m, int d, int y) :dd(d), mm(m), yy(y) { }
bool valid(); // valid_date is too noisy. Screw him.
private:
int mm;
int dd;
int yy;
bool leap(); // private. does not clutter the mind of the user
};
//-----------------------
bool Date::valid()
{
// Sweden was the last "European" country to switch
// from Julian to Gregorian, in 1753.
if(yy<1582)
{throw(string("The year specified dates to the era in which the\n"
"Julian(old) calendar was in use.\nTherefore the date is
invalid.\n") ); }
if(yy<1754)
{throw(string("The Gregorian calendar was not in universal use prior
to 1754.\n"
"Consequently there is insuffient information to
determine whether\n"
"the date provided is valid or not.\n") ); }
if(yy<1919)
{throw(string("This date is probably valid. However Russia did not
switch\n"
"to the Gregorian (current) calendar until 1918.\n"
"So if the date is from Russia, no opinion is offered as
to the validity.\n"
"Otherwise it is valid.\n")); }
if(dd<1)
return false;
if(mm<1 || mm>12)
return false;
switch(mm)
{
case 2: if(dd>29)
return false;
if(dd<=28)
return true;
return (dd==29) ? leap() : false;
//...........
case 4: case 6: case 9: case 11:
return (dd>30) ? false : true;
//..................
default : return (dd>31) ? false : true;
} // end switch
}
//---------------
bool Date::leap()
{
if(yy%4 !=0)
return false;
else if((yy%100) == 0)
if(yy%400 == 0)
return true;
else
return false;
else
return true; // ordinary leap year
}
//=================
int main()
{
cout << "Enter month, day, year\n"
<< "Example: 2 17 2006\n\n";
while(1)
{
int m,d,y;
cin >> m >> d >> y;
if(!cin)
return 0; // user signalled EOF, exit program
Date date(m, d, y);
try
{
if(date.valid() )
cout << "Date is valid\n";
else
cout << "Date is invalid\n";
}
catch(string msg)
{cout << msg;}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help with code plsss 0
Help in this program. 2
I need help 1
Something is wrong 1
Taskcproblem calendar 4
Seek for help..linked list..urgent!!! 1
Function Help 1
Need Help with Repository Program (Beginner) 1

Members online

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,255
Latest member
TopCryptoTwitterChannels

Latest Threads

Top