GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Nov-2008, 09:41
minwei86 minwei86 is offline
New Member
 
Join Date: Oct 2008
Posts: 13
minwei86 is an unknown quantity at this point

Add days to date without affecting the original input date


I know how to add days to date, but i just wan to display the after-added date. I still need the original date. What can i go about it

CPP / C++ / C Code:
void Date::addDay()
{
    int DaysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    string months [12] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};


    for(int i=0;i<12;i++)
    {
    if(mth == months[i])
    count = i;
    }
    bool flag;
    int theDays;
    cout<<"Enter how many days to be added"<<endl;
    cin>>theDays;"\n";

    if(isLeapYear())
    DaysInMonth[1] = 29;
    int tempDays = day;
    tempDays += theDays;

    do
    {
        flag = false;
        if(tempDays>DaysInMonth[count])
        {
            tempDays-=DaysInMonth[count];
            count++;
            flag = true;
        }
        if(count > 12)
        {
            count=1;
            year++;
        }
    }
    while(flag);
    day = tempDays;

    cout<<year<<"/"<<day<<"/";
    switch(count)
    {
        case 0 : cout<<"jan"<<endl;
                 break;
        case 1 : cout<<"feb"<<endl;
                 break;
        case 2 : cout<<"mar"<<endl;
                 break;
        case 3 : cout<<"apr"<<endl;
                 break;
        case 4 : cout<<"may"<<endl;
                 break;
        case 5 : cout<<"jun"<<endl;
                 break;
        case 6 : cout<<"jul"<<endl;
                 break;
        case 7 : cout<<"aug"<<endl;
                 break;
        case 8 : cout<<"sep"<<endl;
                 break;
        case 9 : cout<<"oct"<<endl;
                 break;
        case 10 : cout<<"nov"<<endl;
                 break;
        case 11 : cout<<"dec"<<endl;
                 break;
    }


}


void Date::returnDate() const
{
    cout<<"date is "<<day<<"-"<<mth<<"-"<<year<<endl;
}

When i input a date, it will do all sort of operations before going to returnDate() to print out.

I able to calculate the add days to date, but i dun wan it to affect the input date when i print out. What should i do?
  #2  
Old 12-Nov-2008, 11:05
minwei86 minwei86 is offline
New Member
 
Join Date: Oct 2008
Posts: 13
minwei86 is an unknown quantity at this point

Re: add days to date without affecting the original input date


anyone have some idea or hint to give me?

when i enter example 20 jan 2008...then i add 20 days to it...the result is 9 feb 2008...but i wan when i prompt to print the date, it still contain 20 jan 2008
  #3  
Old 12-Nov-2008, 15:03
minwei86 minwei86 is offline
New Member
 
Join Date: Oct 2008
Posts: 13
minwei86 is an unknown quantity at this point

Re: Add days to date without affecting the original input date


anyone can help...give me some advice also can...
  #4  
Old 12-Nov-2008, 16:34
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Add days to date without affecting the original input date


One possible way is to store the original day into a temporary variable, do the necessary calculations using that variable instead. This will leave the original value intact.

Oh, and just for suggestion -- instead of repeating the month's in a hard-coded way, because you have declared an array of months, consider replacing the switch with something like this:
CPP / C++ / C Code:
if ( count >= 0 && count < 12 ) // verify the index range!
{
     cout << months[count] << endl;
}
else
{
     cout << "Err, uh, the month is out of range?" << endl;
}
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #5  
Old 12-Nov-2008, 16:55
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: add days to date without affecting the original input date


Quote:
Originally Posted by minwei86
when i enter example 20 jan 2008...then i add 20 days to it...the result is 9 feb 2008...but i wan when i prompt to print the date, it still contain 20 jan 2008
As you are beginning to find out, dealing with calendar dates isn't necessarily trivial.

Personally, I think you will find it simpler to exploit the functions found in ctime.h -- especially the functions:Examples can be found at the links given above.
  #6  
Old 12-Nov-2008, 21:49
minwei86 minwei86 is offline
New Member
 
Join Date: Oct 2008
Posts: 13
minwei86 is an unknown quantity at this point

Re: Add days to date without affecting the original input date


Quote:
Originally Posted by TurboPT
One possible way is to store the original day into a temporary variable, do the necessary calculations using that variable instead. This will leave the original value intact.

Oh, and just for suggestion -- instead of repeating the month's in a hard-coded way, because you have declared an array of months, consider replacing the switch with something like this:
CPP / C++ / C Code:
if ( count >= 0 && count < 12 ) // verify the index range!
{
     cout << months[count] << endl;
}
else
{
     cout << "Err, uh, the month is out of range?" << endl;
}


how to store it inside a temp variable can teach me, demo me example.
  #7  
Old 13-Nov-2008, 05:33
minwei86 minwei86 is offline
New Member
 
Join Date: Oct 2008
Posts: 13
minwei86 is an unknown quantity at this point

Re: Add days to date without affecting the original input date


anyone know how to store original day into temp variable...can someone guide me
  #8  
Old 13-Nov-2008, 10:43
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Add days to date without affecting the original input date


Again, this is only one possibility...
CPP / C++ / C Code:
int day = 0;  // your name might differ, but assume this exists somewhere?

// somewhere appropriate...(another variable for a 'temp', call it whatever you want)
int another_day = 0;

// then merely use 'day' in the expression...
// do whatever calc. is needed...
// THIS IS ONLY A BASIC EXAMPLE ( considering your 20 days, from Jan 20, or 'months[0]' )
another_day = (day + 20) % months[0]; // 9, assuming day=20

// TO DO, for you: handling the proper month/year to go along with the 'another_day' change. 
Maybe that'll give you a start?

EDIT:
Like ocicat already pointed out, this should starting 'painting the picture' of date handling not being so trivial.
Unless you are limited by the assignment, check into the functions that he offered in post #5.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble integrating console code into GUI Barman007 Java Forum 18 15-May-2008 14:05
Scanning dilemma (fscanf and fgets) aijazbaig1 C Programming Language 10 29-Mar-2008 05:58
Write the declaration for a Date object Sosy C++ Forum 6 26-Oct-2007 10:21
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Script needed for letting user input a few days of data for tracking and analysis. tradertt MySQL / PHP Forum 3 06-Mar-2003 03:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:51.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.