Thread: How to add Days to a Date?

  1. #1
    Bufferlo
    Guest

    Question How to add Days to a Date?

    I want my program to have a date input in the format of (dd, mm, yyyy), then another input in terms of the number of day(just an integer). And then I want my programm to have an out put of another date (in form of dd, mm, yyyy again). The algorithm is something like: Output_Date = Input_Date + days. I cannot find a simple algorithm.
    Can anyone helps? Thanks.

  2. #2
    His posts are far and few Esparno's Avatar
    Join Date
    Mar 2002
    Posts
    100
    The algorithm is something like: Output_Date = Input_Date + days. I cannot find a simple algorithm.
    uh...that is the algorithm, all you have to do is slap a ; at the end and you will be all set, oh, and make the cin statements, i ASSume u know how to do that.... but u know what happens when u ASS-U-ME
    Signature is optional, I didnt opt for one.

  3. #3
    Bufferlo
    Guest

    Cool

    Furthermore, I don't want to use Julian Days.
    Can anyone use Georgian Calenar to formulate an relatively easy algorithm?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If it was simple you'd probably be able to figure it out or have found it yourself since you know there is a difference between the Julian and Georgian calendar. I suggest you use the search button located in the upper right hand corner of this screen to do a search of the board using calendar as the search key. I am confident you will find a number of pertinent posts. Together with whatever other resources you have available, use them as you will to develop the best algorthym you can devise. Then post your work with pertinent questions, error codes, etc..

  5. #5
    Unregistered
    Guest

    Lightbulb Untested answer

    This solution doesn't check input, and I don't have anything to test it with. It follows the Gregorian rules I found here:

    http://www.genealogy.org/~scottlee/cal-overview.html

    and I wrote it with MSVC++6. If you're using say GCC, change _snprintf to snprintf.

    Code:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    // this follows Gregorian rules
    bool isLeapYear(int year)
    {
    	if (year%400==0) return true;
    	if (year%100==0) return false;
    	return (year%4==0);
    }
    
    int main() 
    {
    	int daysPerMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    
    	int day,month,year, daysToAdvance;
    	char line[81];
    	cout << "Enter date (dd, mm, yyyy): ";
    	cin.getline(line, 80);
    	sscanf(line, "(%i, %i, %i)", &day, &month, &year);
    	cout << "Enter days to advance: ";
    	cin >> daysToAdvance;
    
    	for (int i=0;i<daysToAdvance;i++)
    	{
    		day++;
    		if (day > daysPerMonth[month-1] || ( month==2 && day==29 && !isLeapYear(year) ) )
    		{
    			day = 1;
    			month++;
    			if (month==13)
    			{
    				month = 1;
    				year++;
    			}
    		}
    	}
    
    	char output[80];
    	_snprintf(output, 80, "(%02i, %02i, %04i)", day, month, year);
    	cout << "Result: " << output << endl;
    
    	fflush(stdin);
    	getchar();
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. Vector vs. array.
    By matsp in forum C++ Programming
    Replies: 37
    Last Post: 06-23-2008, 12:41 PM
  4. Adding to a string date
    By knutso in forum C Programming
    Replies: 11
    Last Post: 01-20-2004, 11:18 AM
  5. Help Me Out!! Pls
    By Joanna in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2001, 05:08 AM