date conversion (HELP)

i want to convert a date into a number
for example, 1Jan2012 would be changed to 1, 1feb2012 would be changed to 32
i have typed some codees but wrong output
can somebody help me??


#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int i ;


int date_converter(int month_cid[], int day_cid[], int month_cod[], int day_cod[])
{
int date_in[2],date_out[2],i;
for (i=0;i<2;i++)
{
if (month_cid[i]==1)
{date_in[i] = day_cid[i];}
if (month_cid[i]==2)
{date_in[i] = 31 + day_cid[i];}
if (month_cid[i]==3)
{date_in[i] = 60 + day_cid[i];}
if (month_cid[i]==4)
{date_in[i] = 91 + day_cid[i];}
if (month_cid[i]==5)
{date_in[i] = 121 + day_cid[i];}
if (month_cid[i]==6)
{date_in[i] = 152 + day_cid[i];}
if (month_cid[i]==7)
{date_in[i] = 182 + day_cid[i];}
if (month_cid[i]==8)
{date_in[i] = 213 + day_cid[i];}
if (month_cid[i]==9)
{date_in[i] = 244 + day_cid[i];}
if (month_cid[i]==10)
{date_in[i] = 274 + day_cid[i];}
if (month_cid[i]==11)
{date_in[i] = 305 + day_cid[i];}
if (month_cid[i]==12)
{date_in[i] = 335 + day_cid[i];}
if (month_cod[i]==1)
{date_out[i] = day_cod[i];}
if (month_cod[i]==2)
{date_out[i] = 31 + day_cod[i];}
if (month_cod[i]==3)
{date_out[i] = 60 + day_cod[i];}
if (month_cod[i]==4)
{date_out[i] = 91 + day_cod[i];}
if (month_cod[i]==5)
{date_out[i] = 121 + day_cod[i];}
if (month_cod[i]==6)
{date_out[i] = 152 + day_cod[i];}
if (month_cod[i]==7)
{date_out[i] = 182 + day_cod[i];}
if (month_cod[i]==8)
{date_out[i] = 213 + day_cod[i];}
if (month_cod[i]==9)
{date_out[i] = 244 + day_cod[i];}
if (month_cod[i]==10)
{date_out[i] = 274 + day_cod[i];}
if (month_cod[i]==11)
{date_out[i] = 305 + day_cod[i];}
if (month_cod[i]==12)
{date_out[i] = 335 + day_cod[i];}
}
return date_out[i], date_in[i];
}


int main ()
{
int month_cid[] = {1,10,11}, month_cod[] = {1,10,11}, day_cid[] = {3,10,15} , day_cod[] = {15,17,19};
int date_in[2], date_out[2];

date_converter(month_cid, day_cid,month_cod, day_cod);

for (i=2;i<3;i++)
{ cout << date_in[i] << endl;}

system("PAUSE");
return 0;
}
Last edited on
The code has a few errors which will need to be corrected. I'm not entirely sure what you're trying to accomplish, so we'll take this a few steps at a time.

Your date_in is an array with 2 elements. You can access each element by its zero-based index.

So the line cout << date_in[2] << endl; is pointing to an array element that is beyond the end of your array. date_in[0] points to the first element. date_in[1] points to the second. date_in[2] points to adjacent memory, and is not what you want to do.

Also, your code has two methods. date_converter and main. Within each methods, the arrays date_in and date_out are declared. These have what is called "local scope" which means they cannot be seen outside their methods, unless you pass them as a parameter.

The return line at the end of the date_converter method says return date_out[i], date_in[i];. I don't think that does what you think it does.

So pass date_in and date_out as parameters, and try to get it working. If you get stuck, post your new version of your code here with [cod e] and [/cod e] tags to help with formatting.

Go for it!
Topic archived. No new replies allowed.