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;
}
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.
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.
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.
//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";
}
}
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.
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;
}
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.
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.........
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...
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