C++/date

Advertisement


Question
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
#include <iostream>
#include <iomanip>
#include <sstream>
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

Answer
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

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.