Error Checking If Statements on months and days

Pages: 12
My Problem is that my code for working out the number of seconds that have passed since 2000/01/01 once the user has typed up a date after 2000/01/01
My if statment works with the year where it loops and asks again if the year is before 2000, but this won't work with the month or day.

//duration.cpp
//For calculating time that has elapsed since 2000/01/01 and 00:00:00
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()

{
int year, month, days, seconds, hours, minutes, answer;

cout << "This program with calculate the time in seconds since 2000/01/01 and 00:00:00" << endl;
do
{
cout << "Please enter a date (Year Month Days): " << endl;
cin >> year >> month >> days;


if (year<2000)
{
cout << "The year must be at least 2000!" << endl;
}


if ((month<=0)&&(month>12))

{
cout << "The month must be between and inclusive of 1 and 12!" << endl;
}

if (((month==9)&&(days<=0)&&(days>30))||((month==4)&&(days<=0)&&(days>30))||((month==6)&&(days<=0)&&(days>30))||((month==11)&&(days<=0)&&(days>30)))

{
cout << "The date for this month must be between 1 and 30 inclusive!" << endl;
}

if (((month==1)&&(days<=0)&&(days>31))||((month==3)&&(days<=0)&&(days>31))||((month==5)&&(days<=0)&&(days>31))||((month==7)&&(days<=0)&&(days>31))||((month==8)&&(days<=0)&&(days>31))||((month==10)&&(days<=0)&&(days>31))||((month==12)&&(days<=0)&&(days>31)))

{
cout << "The date for this month must be between 1 and 31 inclusive!" << endl;
}

}


//work out the seconds for years,month and days

while (year<2000);
{
answer=(((year - 2000)*1892160000)+((month - 1)*157680000)+((days - 1)*5184000));
{

cout << "The seconds since " << year << " " << month << " " << days << " is " << answer << endl;
}

}
return 0;
}


Any suggestions?

Sorry if it seems a mess all the code.
I think you should probably break up your validation into smaller loops to validate user input rather than collecting the entire entry, then validating and requiring the user to reenter some data that was correct. I'll try and post some code when I have a moment.

Return 0;
Oh right, thanks a lot :)
Try something like this for each condition (month, day, year):

1
2
3
4
5
6
7
do
{
    cout << "\nPlease enter a year from 2000 up: ";
    cin >> year;
    if (year < 2000)
          cout << "Invalid year.";
} while (year < 2000);


If you break each of your ifs into smaller do..while loops you can validate after each entry without requiring the user to reenter all info. Program control will move from loop to loop as each condition is validated and eventually satisfied. You can then reach your calculations and output the results.

Hope this helps.
Thank you very much, it does work. But i think my IF statements might not be correct as when i put an incorrect month it goes straight to the date section.
Post your code and I'll take a look.
//newduration.cpp
//For calculating time that has elapsed since 2000/01/01 and 00:00:00
#include <iostream>
#include <iomanip>

using namespace std;

int main()

{
int year, month, days, seconds, minutes, hours, answer;

cout << "This program with calculate the time in seconds since 2000/01/01 and 00:00:00" << endl;

do
{
cout << "Please enter the year: ";
cin >> year;

if (year<2000)
{
cout << "Year must be 2000 or greater" << endl;
}
}

while (year<2000);

do
{
cout << "Please enter a month: ";
cin >> month;

if ((month <=0)&&(month > 12))
{
cout << "The month must be between and inclusive of 1 and 12!" << endl;
}
}

while ((month <=0)&&(month > 12));

do
{
cout << "Please enter a date: ";
cin >> days;

if (((month==9)&&(days<=0)&&(days>30))||((month==4)&&(days<=0)&&(days>30))||((month==6)&&(days<=0)&&(days>30))||((month==11)&&(days<=0)&&(days>30)))
{
cout << "The date for this month must be between 1 and 30 inclusive!" << endl;
}

if(((month==1)&&(days<=0)&&(days>31))||((month==3)&&(days<=0)&&(days>31))||((month==5)&&(days<=0)&&(days>31))||((month==7)&&(days<=0)&&(days>31))||((month==8)&&(days<=0)&&(days>31))||((month==10)&&(days<=0)&&(days>31))||((month==12)&&(days<=0)&&(days>31)))
{
cout << "The date for this month must be between 1 and 31 inclusive!" << endl;
}
}

while (((month==9)&&(days<=0)&&(days>30))||((month==4)&&(days<=0)&&(days>30))||((month==6)&&(days<=0)&&(days>30))||((month==11)&&(days<=0)&&(days>30)));

cout << "I now need a time" << endl;

do
{

cout << "Enter the number of hours (in 24hr): ";
cin >> hours;

if (hours>23&&hours<0)
{
cout << "The time must be somewhere between and inclusive of 0 and 23";
}
}

while (hours>23&&hours<0);


return 0;
}


Thats as far as i got.
Thanks.





1
2
3
4
5
6
7
8
9
10
11
do
{
cout << "Please enter a month: ";
cin >> month;

if ((month <=0)&&(month > 12))
{
cout << "The month must be between and inclusive of 1 and 12!" << endl;
}
}


The reason program control moves on to the next loop if bad data is entered is because in the above code you are validating that if month <= 0 AND if month > 12, then its invalid. You need to change the && to || (or). Also take a look at your other if statements and see if you run into the same problem.
hey i have a problem with the same program i dont know what formula to use
@magmatic

What problems are you running into?
actualy i want to make the same program but including the leap years and i need the formula for example how i will get the number of seconds elepased from 2000 until the given date by the user??? thank u
Everything is working now, apart from the IF statments for the dates with Leap years


do
{
cout << "Please enter a date: ";
cin >> days;

if (((month==9)&&(days<=0)&&(days>30))||((month==4)&&(days<=0)&&(days>30))||((month==6)&&(days<=0)&&(days>30))||((month==11)&&(days<=0)&&(days>30)))
{
cout << "The date for this month must be between 1 and 30 inclusive!" << endl;
}

else if(((month==1)&&(days<=0)&&(days>31))||((month==3)&&(days<=0)&&(days>31))||((month==5)&&(days<=0)&&(days>31))||((month==7)&&(days<=0)&&(days>31))||((month==8)&&(days<=0)&&(days>31))||((month==10)&&(days<=0)&&(days>31))||((month==12)&&(days<=0)&&(days>31)))
{
cout << "The date for this month must be between 1 and 31 inclusive!" << endl;
}

//Leap Year consideration

else if((month==2)&&(year%400==0)||(!(year%100==0)&&(year%4==0))&&(days<=0)&&(days>29))

{
cout << "This date for this month must be between 1 and 29 inclusive!" << endl;
}

else if((month==2)&&(days<=0)&&(days>28))

{
cout << "This date for this month must be between 1 and 28 inclusive!" << endl;
}

}

while(((month==9)&&(days<=0)&&(days>30))||((month==4)&&(days<=0)&&(days>30))||((month==6)&&(days<=0)&&(days>30))||((month==11)&&(days<=0)&&(days>30)));
while(((month==1)&&(days<=0)&&(days>31))||((month==3)&&(days<=0)&&(days>31))||((month==5)&&(days<=0)&&(days>31))||((month==7)&&(days<=0)&&(days>31))||((month==8)&&(days<=0)&&(days>31))||((month==10)&&(days<=0)&&(days>31))||((month==12)&&(days<=0)&&(days>31)));
while((month==2)&&(year%400==0)||(!(year%100==0)&&(year%4==0))&&(days<=0)&&(days>29));
while((month==2)&&(days<=0)&&(days>28));

It just keeps accepting anything
and if it is a leap year and i put a date in it just stops working.
This won't help you with your problem, but you said
Sorry if it seems a mess all the code.


I quote Grey Wolf (Apr 26, 2008 at 8:57pm)
How to: Put code into your postings
http://www.cplusplus.com/forum/articles/1624/


When posting code in your question or answer; displaying the code in a nicely formatted manner helps all those involved in the tread to quickly understand the code.

To do this; follow one of these methods:

Method one:
In the edit window move the cursor to the position where you want to place the code.
Click the ‘#’ button (on the right of the edit window).
Back in the edit window, move the cursor to the position between the opening and closing code tags.
Then type or cut and paste your code.

Method two
In the edit window type or cut and pates your code.
Highlight all the code and then click the ‘#’ button (on the right of the edit window).

Method three
There is also the plain old fashioned way of typing [c0de] before your code and [/c0de] at the end. NB. use the correct spelling for code.

Last edited on
Don't worry about all that i've got it done, thanks for the help.

My problem is how to calculate the seconds elapsed between the 2000 and the date typed by the user....i mean what statmet to use and how can i calculate this couse this part of code here is for checking.....
Ledzepp4eva can u post the full program couse im really confused now i really dont know how to keep going.........
C has facilities for converting a date to the number of seconds since 1970-01-01 00:00:00Z.
You can convert both dates and then subtract.

http://www.cplusplus.com/reference/clibrary/ctime/
Last edited on
How did you manage to sort it in the end Ledzepp.

Im intrigued!
i assume everyone asking the questions is at UCLAN. lolz. Believe it or not iv just finished my program, and was going to post on this forum to see if anyone could help me tweek it. But I will say Zep, nice bit of coding. My finished piece is almost 200 lines, but i suppose thats because my solution is a little bit more elaborate. If you dont mind...could you explain this bit of code to me...

answer=(((year - 2000)*1892160000)+((month - 1)*157680000)+((days - 1)*5184000));

i can just about guess what it does...but how did you come up with it??
Nanny can u help with the calculating part couse....i done the checking part but now my problem is the calculation i dont know how to do that...
That expression is completely messed up.
* A day has 86,400 seconds, not 86400*60=5184000.
* Months don't have a fixed number of seconds.
* Years have approx. 31,556,952 seconds, not 1,892,160,000, which is around the number of seconds in 60 years

To get a second count from a date, use mktime() (http://www.cplusplus.com/reference/clibrary/ctime/mktime.html ). Of course, you'll first have to fill a tm structure. Here's what it should contain: http://www.cplusplus.com/reference/clibrary/ctime/tm.html
Pages: 12