C++/c++
Expert: Zlatko - 5/11/2010
QuestionHi Zlatko,
Thanks for your answer.I changed program .
Regards
Tina
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
string correctdate(void);
bool leapyear(int year);
int daysInMonth(int month, int year );
int dayFrom1900(int year, int month, int days);
int days(const string &date1, const string &date2);
int main()
{
cout<<endl;
string d1,date1,d2,date2;
d1 = correctdate();
cout<<endl<<endl;
cout<<"\tPlease write the second date."<<endl;
d2 = correctdate();
cout<<endl;
cout<<"total days are :"<<days(d1,d2)<<endl;
return 0;
}
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
int days(const string &date1 ,const string &date2)
{
int year1,year2,month1,month2,day1,day2;
int days1=0,days2=0;
istringstream iss(date1);
iss>>year1>>month1>>day1;
days1 = dayFrom1900(year1, month1, days1);
cout<<endl<<endl;
istringstream iss2(date2);
iss2>>year2>>month2>>day2;
days2 = dayFrom1900(year2, month2, days2);
int total_days = days2 - days1;
return total_days;
}
//------------------------------------------
//name:correctdate
//------------------------------------------
string correctdate(void)
{
int year, month,day;
string date;
bool dateok ;
do
{
dateok = true;
cout<<"\t**********DATE**********\n";
cout<<"\t write a date (yyyy mm dd): ";
getline(cin,date);
istringstream iss(date);
iss>>year>>month>>day;
if (year<1900 || year>3000)
{dateok = false;}
if (month<1 || month>12)
{dateok = false;}
if(day <1 || day >31)
{dateok = false;}
cout<<endl;
cout<<"\tYear = "<<year<<endl;
cout<<"\tMonth = "<<month<<endl;
cout<<"\tDay = "<<day<< " is a ";
if (dateok)
{cout<<"date is okey ."<<endl;}
else
{cout<<" date is not okey.try again !!!"<<endl<<endl;}
}while(!dateok);
return date;
}
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
bool leapyear(int year)
{
if (year % 400 == 0)
return true;
else if (year % 100 != 0)
return false ;
else (year % 4 == 0);
return false ;
}
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
int daysInMonth(int month, int year )
{
int day = 0;
if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 ||month==12)
{ day = 31;
return day ;
}
else if (month==4 || month==6 || month==9 || month==11)
{
day = 30;
return day ;
}
else
{
if (leapyear(year))
{day = 29;
return day ;
}
else
{
day = 28;
return day;
}
}
}
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
int dayFrom1900(int year, int month, int days)
{
int ar = 1900;
int monad = month;
int dag = 0;
do
{
if(leapyear(ar))
{dag += 366;}
else
{dag += 365;}
ar += 1;
}while(ar<year);
if (ar == year)
{
do{
dag = dag + daysInMonth(monad, ar);
monad -= 1;
}while(monad ==0);
}
if ( monad == 0 )
{
days = dag + days;
}
return days;
}
AnswerHello Tina.
I had a quick look at the program. I found only 1 serious problem.
Try 1999 1 1, and 2000 1 1, you get back 0 days.
The problem was in dayFrom1900. The monad loop was while(monad == 0). It should be while (monad > 0) or while (monad != 0). I prefer the first.
I fixed the program. My comments start with //ZM
Generally you did a good job.
Best regards
Zlatko
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
string correctdate(void);
bool leapyear(int year);
int daysInMonth(int month, int year );
int dayFrom1900(int year, int month, int days);
int days(const string &date1, const string &date2);
int main()
{
cout<<endl;
string d1,date1,d2,date2;
d1 = correctdate();
cout<<endl<<endl;
cout<<"\tPlease write the second date."<<endl;
d2 = correctdate();
cout<<endl;
cout<<"total days are :"<<days(d1,d2)<<endl;
return 0;
}
int days(const string &date1 ,const string &date2)
{
int year1,year2,month1,month2,day1,day2;
int days1=0,days2=0;
istringstream iss(date1);
iss>>year1>>month1>>day1;
days1 = dayFrom1900(year1, month1, days1);
cout<<endl<<endl;
istringstream iss2(date2);
iss2>>year2>>month2>>day2;
days2 = dayFrom1900(year2, month2, days2);
int total_days = days2 - days1;
return total_days;
}
//------------------------------------------
//name:correctdate
//------------------------------------------
string correctdate(void)
{
int year, month,day;
string date;
bool dateok ;
do
{
dateok = true;
cout<<"\t**********DATE**********\n";
cout<<"\t write a date (yyyy mm dd): ";
getline(cin,date);
istringstream iss(date);
iss>>year>>month>>day;
if (year<1900 || year>3000)
{dateok = false;}
if (month<1 || month>12)
{dateok = false;}
if(day <1 || day >31)
{dateok = false;}
cout<<endl;
cout<<"\tYear = "<<year<<endl;
cout<<"\tMonth = "<<month<<endl;
cout<<"\tDay = "<<day<< " is a ";
if (dateok)
{cout<<"date is okey ."<<endl;}
else
{cout<<" date is not okey.try again !!!"<<endl<<endl;}
}while(!dateok);
return date;
}
bool leapyear(int year)
{
if (year % 400 == 0)
return true;
else if (year % 100 != 0)
return false ;
//ZM This semicolon at the end of the line below means that the else has no consequent. You should remove the entire line
else (year % 4 == 0);
return false ;
}
int daysInMonth(int month, int year )
{
int day = 0;
if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 ||month==12)
{ day = 31;
return day ;
}
else if (month==4 || month==6 || month==9 || month==11)
{
day = 30;
return day ;
}
else
{
if (leapyear(year))
{day = 29;
return day ;
}
else
{
day = 28;
return day;
}
}
//ZM should have a return as the last line. I know that the logic above forces a return in all cases.
return day;
}
int dayFrom1900(int year, int month, int days)
{
int ar = 1900;
int monad = month;
int dag = 0;
do
{
if(leapyear(ar))
{dag += 366;}
else
{dag += 365;}
ar += 1;
}while(ar<year);
if (ar == year)
{
do{
dag = dag + daysInMonth(monad, ar);
monad -= 1;
}while(monad > 0); //ZM Correction, was monad==0
}
if ( monad == 0 )
{
days = dag + days;
}
return days;
}