C++/date
Expert: Zlatko - 5/4/2010
QuestionHi Zelatko,
I've a program that it doesn't work.Could you help me.
It's a part of a program.I want to check two dates with this program that if are the correct date or not!
Regards,
Tina
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
string correctdate(string date);
int main()
{
string d1,date1;//,d2,date1,date2;
/*date1 = correctdate(d1);
date2 = correctdate(d2);*/
d1 = correctdate(date1);
//correctdate(d2);
//cout<<"date1"<<date1<<endl;
//cout<<"date2"<<date2<<endl;
return 0;
}
//------------------------------------------
//namn:correctdate
//------------------------------------------
string correctdate()
{
int year, month,day;
string date;
bool dateok ;
do
{
dateok = true;
cout<<"\t**********DATE**********\n";
cout<<"\t write a date (like 1111 11 11): ";
getline(cin,date);
date[4] =' ';
date[7] =' ';
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;
}
AnswerHello Tina.
Your program almost works. There are just a couple of problems with it. One problem is that you declared the function
string correctdate(string date);
but defined the function
string correctdate()
{
}
The function you defined took no parameters, so the program would not link. The linker was expecting a function that took a string.
The other problem is explained in my comments in the code. My comments start with ZM, so you can search for that.
Best regards
Zlatko
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
string correctdate(void);
int main()
{
string d1;
d1 = correctdate();
return 0;
}
//------------------------------------------
//namn:correctdate
//------------------------------------------
string correctdate(void)
{
// ZM initialize your local variables
int year = 0, month = 0, day = 0;
string date;
bool dateok ;
do
{
dateok = true;
cout<<"\t**********DATE**********\n";
cout<<"\t write a date (like 1111 11 11): ";
getline(cin,date);
/*ZM This will overwrite data, if the user inputs something like 2000 1 1
If the spaces are not there already, you cannot just put them in because doing
so will overwrite the user input.
date[4] =' '; // Remove this line
date[7] =' '; // Remove this line
If the spaces are missing, the iss>> will fail, and the variables
month or day will stay 0, because they are initialized to 0 above.
Then your program will report the error at the end.
*/
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;
}