c++ show correct dates

April 30, 2010 at 07:26:31
Specs: Windows XP
Hi
Could you please guide me why it doesn't work!this program takes two dates and check them if they are correct and show these two in output.
Regards,
Tina

#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;

int correctdate(string date);
int main()
{
int date1 = correctdate( date);
int date2 = correctdate(date);
cout<<"date1"<<date1<<endl;
cout<<"date2"<<date2<<endl;
return 0;
}
//------------------------------------------
//namn:correctdate
//------------------------------------------
int correctdate(string date)
{

int year, month,day;
bool dateok = true;
do
{
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;
}


See More: c++ show correct dates

Report •


#1
April 30, 2010 at 15:07:23
looks like you kind of got mixed up about the var. types being used.
Your correctdate is declared as an int, but returns a string,
and you don't need to send anything to correctdate if you just want the string var. date, that just muddies the waters and obfuscates the logic:
string correctedate();
also, a logic error, your boolean dateok value needs to be set to true inside the do-loop.
declare it at the top, before the loop, with no value, then
assign it as the first step in the do-loop:
string correctdate();
int day,month,year;
string date;
bool dateok;
do
{
dateok=true;

(or you could just do all the declarations at the initial point prior to main() so they aren't re-declared everytime correctdate is called).


Report •

#2
May 4, 2010 at 00:14:11
Hi ,
Thanks for your reply.I did all that you wrote ( I think ;) ) and run program,but it has two errors that I couldn't understand why.I tried to change it but it shows the same errors.

Thanks.

#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;

string correctdate(string date);
int main()
{
string d1,d2,date1,date2;
/*date1 = correctdate(d1);
date2 = correctdate(d2);*/
correctdate(d1);
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;
}

and error message:


1>Linking...
1>correctdate.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl correctdate(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?correctdate@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@Z) referenced in function _main
1>C:\CCP\ccp\correctdate\Debug\correctdate.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\CCP\ccp\correctdate\correctdate\Debug\BuildLog.htm"
1>correctdate - 2 error(s), 0 warning(s)


Report •

#3
May 4, 2010 at 06:02:37
You declare a
string correctdate(string date);

But define a
string correctdate()

Pick one and be consistent.

Report •

Related Solutions

#4
May 4, 2010 at 12:56:27
Hi ,
Thank you very much. It works ;))))
Could you write me that when and how can one use reference variables in a program which has some functions ?
I read about reference variables ,but I didn't understand how it really works.

Thanks.


Report •

#5
May 4, 2010 at 13:19:06
yes, you have an excessive profusion of "dates" running around confusing things. You only really need one, or three at most if you need access to both of them later on:
string correctdate(),d1,d2,date;
int main()
{
d1=correctdate();
d2=correctdate();
cout<<"date1"<<d1<<endl;
cout<<"date2"<<d2<<endl;
...

Report •

#6
May 4, 2010 at 14:18:16
References are used when you want to return more than one thing (really common in Win32), or you take some object/variable in and you modify it.

Report •

Ask Question