Getting the Following Date

This is a discussion on Getting the Following Date within the C Programming forums, part of the General Programming Boards category; Perhaps I could make my program a lot more simpler by condensing it and using the time.h header file, but ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Post Getting the Following Date

    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.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You cant return more than one variable at a time like that. Here is a way you can have a function modify more than one variable though:

    Code:
    void foo(int *days, int *month, int *year)
    {
      *days = 12;
      *month = *month + 1;
      *year = 1;
    }
    
    int main()
    {
      int days = 0, month = 0, year = 0;
      foo(&days,&month,&year);
      printf("%d %d %d\n",days,month,year);
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Neat

    So if I have that function and utilize pointers, I should be able to...change my day, month, and year values? Are my switch and boolean functions okay? I wouldn't want to change anything I don't have to. Thanks for the info, that should prove useful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 08:55 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 04:23 PM
  4. CDate Class - handle date manipulation simply
    By LuckY in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 08:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21