Hi Zelatko, Thank you for your reply, it works very well;) correctdate is a part of the main program.In main program I want to calculate days between two dates.Could you help me why it doesn't work? I'me new in c++ and don't know how to use reference variables in main program.
Regards, Tina
using namespace std;
string correctdate(string date); bool leapyear(int year); int daysInMonth(int month, int year ); int dayFrom1900(int year, int month, int days); int days(string datum1, string datum2); int main() {
cout<<endl; /*string date1,date2; string d1 = correctdate(date1); string d2 = correctdate(date2); *///
string d1,date1,d2,date2; d1 = correctdate(date1); cout<<endl<<endl; cout<<"\tPlease write the second date."<<endl; d2 = correctdate(date2);
cout<<"total days are :"<<days(d1,d2);
return 0;
} //------------------------------------------ //namn:days //------------------------------------------
int days(string datum1 ,string datum2) {
string d1,d2;//date1,d2,date2; int year1,year2,month1,month2,day1,day2; int days1,days2; d1 = correctdate(date1); datum1[4] =' '; datum1[7] =' '; istringstream iss(datum1); iss>>year1>>month1>>day1; days1 = dayFrom1900(year1, month1, days1); cout<<endl<<endl; cout<<"\tPleas write a date."<<endl; //d2 = correctdate(date2); datum2[4] =' '; datum2[7] =' '; istringstream iss(datum2); iss>>year2>>month2>>day2; days2 = dayFrom1900(year2, month2, days2);
int total_days = days2 - days1; return total_days;
} //------------------------------------------ //namn:correctdate //------------------------------------------ string correctdate(string date) { int year, month,day; 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; } //------------------------------------------ //namn:leapyear //------------------------------------------ bool leapyear(int year) {
if (year % 400 == 0) return true; else if (year % 100 != 0) return false ; else (year % 4 == 0); return false ;
}
//------------------------------------------ //namn:daysinmonth //------------------------------------------ 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; }
}
}
//------------------------------------------ //namn:daysFrom 1900 //------------------------------------------ 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; }
Here are error messages: error C2374: 'iss' : redefinition; multiple initialization error C2088: '>>' : illegal for class
Hello Tina
In your days function, you have two instances of iss being declared. You have istringstream iss(datum1); and istringstream iss(datum2); So the compiler is telling you that you cannot have 2 variables of the same name in the same scope. The easiest thing to do is take the second iss, and rename it iss2. Be careful to rename it in the line iss>>year2>>month2>>day2; too. It should read iss2>>year2>>month2>>day2;
You have not added the corrections from my last message to the correctdate function so you should do that before trying you compile.
Good luck.
Zlatko
Hi 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
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; }
Hello 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.
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;
}
Advertisement