0

hey, i have this program that when a user types in a date (day/month/year)and it works out the next date.
It's meant to find out whether its a leapyear and therefore on the 28/2/2012 it should print out the 29/2/2012.
At the end of the year 31/12/year it is meant to roll over to 1/1/year + 1.
All the months that have 30 days (April, June, September, November) should roll over to the first of next month and months that have 31 days should roll over to the first too.
My code is:

#include <stdio.h>

int day,month,year;
int nextday = 1;
int nextmonth = 1;
bool leapyear;

int main(){
	printf("Enter the date in the form day/month/year: ");
	scanf("%d/%d/%d",&day,&month,&year);
	
	//find out if year is leap year
	if(year % 4 == 0){
		if(year % 100 == 0){
			if(year % 400 == 0){
				leapyear = true;
			}else{
				leapyear = false;
			}
		}else{
			leapyear = true;
		}
	}else{
		leapyear = false;
	}
	
	//calculate whether the next day in Feb in a leap year
	
	if((leapyear = true) && (month == 2,02)){
		if(day < 29){
			day++;
			printf("The date of the next day is: %d/%d/%d",day,month,year);
			return 0;
		}else{
			printf("The date of the next day is: %d/",nextday);
			month++;
			year = year;
			printf("%d/%d",month,year);
			return 0;
		}
	}
	
	//calculate the next day in Feb if not a leap year
	
	if((leapyear = false) && (month == 2,02)){
		if(day < 28){
			day++;
			printf("The date of the next day is: %d/%d/%d",day,month,year);
			return 0;
		}else{
			printf("The date of the nextday is: %d/",nextday);
			month++;
			year = year;
			printf("%d/%d",month,year);
			return 0;
		}
	}
	
	//calculate the next day in months that have 30 days
	
	if(month == 4, 6, 9, 11){
		if((day > 0) && (day < 30) && (month != 2,02)){
			month = month;
			year = year;
			day++;
			printf("The date of the next day is: %d/%d/%d", day,month,year);
			return 0;
		}else{
			month++;
			printf("%d/%d/%d",day,month,year);
		}
	}
	
	//calculate the next day/new year at the end of the year
	
	if((day == 31) && (month == 12)){
		printf("The date of the next day is: %d/",nextday);
		printf("%d/",nextmonth);
		year++;
		printf("%d",year);
		return 0;
	}
	//calculate the next month
	if((day == 31) && (month != 12)){
		printf("The date of the next day is: %d/",nextday);
		month++;
		year = year;
		printf("%d/%d",month,year);
		return 0;
	}
}

my problems are:
that the 31/12/year rolls over to the 1/13/year.
the 28/2/1999 (non leapyear) goes to the 29/2/1999.
the 30/5/year (month with 31 days) rolls over to 1/6/year (should roll over to 31/5/year).

im sure there are more problems but these are all i can find at the moment. i do not understand why i have these problems so help will be majorly appreciated.

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by Adak
0

Welcome to the forum!

You obviously need to add some logic here - hardly a surprise that month needs to be set to 1, along with the day, at the end of the year:

if((day == 31) && (month == 12)){
         printf("The date of the next day is: %d/",nextday);
         printf("%d/",nextmonth);
         year++;
         printf("%d",year);
   
         return 0;
       }

You need to get your head into the calendar, and your program. These errors are low hanging fruit, just waiting for you.

Don't get tired, and don't get lazy - get in there!

Edited by Adak: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.