Perhaps I could make my program a lot more simpler by condensing it and using the time.h header file, but that's a path I'd like to stray from because my professor hasn't endorses the use of many header files (I know...)
My DayAfter function is all screwed up because I am making the function return two values simultaneously, and I cannot do that. Is there a way around it? Thanks!
Here is my code; it's pretty lengthy. I appreciate any recommendations, even if they deal with having to write more functions.
Code:
/*Project #33
Herbert Ortiz
Due: September 27, 2004
This program finds the data following that which the user input*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef enum Bool {false=0, true} bool; //enables boolean functions
int GetMonth(int month); //obtain today's month with this
int GetDay(int day); //func prototype for current day
int GetYear(int year); //func prototype for current year
int DaysInMonth(int month, int year); //this will set up correct parameters
int DayAfter(int month, int day, int year); //the desired output prototype
bool IsDayValid(int day); //validation of date is necessary
bool IsMonthValid(int month); //this too
void HandleError(int ErrorCode); //in case of invalid entry
void DisplayResults(int month, int day, int year); //the output display prototype
main()
{
int month, day, year, ErrorCode; //local variables declared
month=GetMonth(month); //function that calls current month
day=GetDay(day); //function call for current day
year=GetYear(year); //function call for current year
if(IsDayValid(day)&&IsMonthValid(month)) //as long as these conditions are satisfied...
{
DisplayResults(month, day, year);
day=DayAfter(month, day, year);
}
}
int GetMonth(int month) //the function header and definition for current month
{
printf("Enter the current month: "); //prompt 1
scanf("%d", &month); //month is stored in memory and is evaluated
return month; //this input value will be displayed in main()
}
int GetDay(int day) //function header and definition for current day
{
printf("Enter the day today: "); //prompt 2
scanf("%d", &day); //variable for day is evaluated and stored
return day; //returns value in main()
}
int GetYear(int year) //function header and definition for current year
{
printf("Enter this current year: "); //prompt 3
scanf("%d", &year); //year is stored in memory for later use
return year; //year value returned to main() for onscreen display
}
int DaysInMonth(int month, int year) //function that defines date boundaries
{
int day; //day variable declared
switch(month)
{
case 1: //January has 31 days
day=31;
break;
case 2: //February (of last year) had 28 days
day=28;
break;
case 3: //March has 31 days
day=31;
break;
case 4: //April has 30 days
day=30;
break;
case 5: //May has 31 days
day=31;
break;
case 6: //June has 30 days
day=30;
break;
case 7: //July has 31
day=31;
break;
case 8: //August also has 31 (this year)
day=31;
break;
case 9: //September has 30 days
day=30;
break;
case 10: //October has 31 days
day=31;
break;
case 11: //November has 30 days
day=30;
break;
case 12: //December has 31 days
day=31;
break;
default:
printf("Error, try again"); //in case of invalid month value
}
return day;
}
bool IsDayValid(int day) //must check if day input is appropriate
{
if(1<=day<=31)
{
return true;
}
else
{
return false;
}
}
bool IsMonthValid(int month) //we must check for the month too
{
if(1<=month<=12)
{
return true;
}
else
{
return false;
}
}
void HandleError(int ErrorCode) //any errors? no problem!
{
printf("\nError\n");
printf("Error found");
}
int DayAfter(int month, int day, int year)
{
if(day<=DaysInMonth(month, year)) //restrictions apply, results may vary
{
day=day++; //the day is increased if it will not leap into the next month
return day;
}
else if(day>DaysInMonth(month, year)) //another condition may apply
{
month=month++;
day=1;
return month, day;
}
else if(month>12)
{
month=1; //If one goes from Dec to Jan, the month value is 1 (for Jan) and 1 is added to year
year=year++;
return month, year;
}
return day, month, year;
}
void DisplayResults(int month, int day, int year) //the end result, the output
{
day=DayAfter(month, day, year);
printf("The following date is %d %d %d", month, day, year);
scanf("%d%d%d", &month, &day, &year);
getch();
}
Thanks again! I know I'm relentless.