C Programming

CLASS EXERCISE C21
Write a program in C that prompts the user for todays date, calculates tomorrows date, and displays the result. Use structures for todays date, tomorrows date, and an array to hold the days for each month of the year. Remember to change the month or year as necessary.


	#include <stdio.h>

	struct date {
		int day, month, year;
	};

	int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	struct date today, tommorrow;

	void gettodaysdate( void );

	void gettodaysdate( void )
	{
		int valid = 0;
		
		while( valid == 0 ) {
			printf("Enter in the current year (1990-1999)-->");
			scanf("&d", &today.year);
			if( (today.year < 1990) || (today.year > 1999) ) 
				printf("\007Invalid year\n");
			else
				valid = 1;
		}
		valid = 0;
		while( valid == 0 ) {
			printf("Enter in the current month (1-12)-->");
			scanf("&d", &today.month);
			if( (today.month < 1) || (today.month > 12) ) 
				printf("\007Invalid month\n");
			else
				valid = 1;
		}
		valid = 0;
		while( valid == 0 ) {
			printf("Enter in the current day (1-%d)-->", days[today.month-1]);
			scanf("&d", &today.day);
			if( (today.day < 1) || (today.day > days[today.month-1]) ) 
				printf("\007Invalid day\n");
			else
				valid = 1;
		}
	}

	main()
	{

		gettodaysdate();
		tommorrow = today;
		tommorrow.day++;
		if( tommorrow.day > days[tommorrow.month-1] ) {
			tommorrow.day = 1;
			tommorrow.month++;
			if( tommorrow.month > 12 ) {
				tommorrow.year++;
				tommorrow.month = 1;
			}
		}
		printf("Tommorrows date is %02d:%02d:%02d\n", \
			tommorrow.day, tommorrow.month, tommorrow.year );
	}

©Copyright B Brown. 1984-1999. All rights reserved.