Thread: Help to Generalize my C Program.

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    48

    Help to Generalize my C Program.

    I have a C Program that allows a user to enter two dates, compares the two, then prints out which date is earlier than the other. I would now like to generalize the program so that i can have the user enter as many dates as they want until they enter 0/0/0 which will signal the stopping point. Then i just need the program to print only the earliest date out of all of the dates the user enters. Not too sure how to accomplish this. Here is my starting code:
    Code:
        /*
          Namme: Connor Moore
          Date: 1/29/2013
          File: compareNDates.c
          Purpose: This program prompts a user to enter
           any number of dates until the user enters 0/0/0.
           The program will then print out the date that is
           the earliest.
        */
    
    
        #include <stdio.h>
    
        void compare();
    
        int main (void) {
    
        int mm = 1, dd = 1, yyyy = 1, mm2 = 0, dd2 = 0, yyyy2 = 0;
    
        /* Prompt for and get the first date*/
        printf("Enter first date (mm/dd/yyyy): ");
        scanf("%d /%d /%d", &mm, &dd, &yyyy);
        }
    
        /* Prompt for and get the second date*/ 
        printf("Enter second date (mm/dd/yyyy): ");
        scanf("%d /%d /%d", &mm2, &dd2, &yyyy2);
       
        /*Function Call*/
        compare(mm, mm2, dd, dd2, yyyy, yyyy2);
    
        return 0;
    
        }
    
    /*Function that takes in each section of the dates
    entered by the user and compares them starting
    with the years, then the months, then the days.
    After comparison it prints out which is earlier.
    Also added in what to do in case a user enters
    the same dates.*/
    
    void compare(int mm, int mm2, int dd, int dd2,
             int yyyy,int  yyyy2) {
        if (yyyy < yyyy2) {
        printf("%2d/%2d/%4d is earlier than %2d/%2d/%4d\n",
           mm, dd, yyyy, mm2, dd2, yyyy2);
        }
    
        else if (yyyy > yyyy2) {
    
        printf("%2d/%2d/%4d is earlier than %2d/%2d/%4d\n",
           mm2, dd2, yyyy2, mm, dd, yyyy);
        }
        else {
    
        if (mm < mm2) {
    
        printf("%2d/%2d/%4d is earlier than %2d/%2d/%4d\n",
           mm, dd, yyyy, mm2, dd2, yyyy2);
        }
    
        else if (mm > mm2) {
    
        printf("%2d/%2d/%4d is earlier than %2d/%2d/%4d\n",
           mm2, dd2, yyyy2, mm, dd, yyyy);
        }
    
        else {
    
        if (dd < dd2) {
        printf("%2d/%2d/%4d is earlier than %2d/%2d/%4d\n",
           mm, dd, yyyy, mm2, dd2, yyyy2);
        }
    
        else if (dd2 > dd) {
    
        printf("%2d/%2d/%4d is earlier than %2d/%2d/%4d\n",
           mm2, dd2, yyyy2, mm, dd, yyyy);
        }
    
        else {
    
        printf("The dates you entered are the same\n");
    
        }
        }
        }
    }
    

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    My approach would be to have 3 variables minYear, mindMonth, minDay which would have the data of the earliest date as the user inputs again and again.
    Example:
    15/05/1992
    15/02/2000
    15/05/2012
    0/0/0
    • I read the first date. The earliest date is the 1st date.
    • I read the 2nd date.Compare it with the min variables. If the 2nd date is earlier than the min variables, then assign the 2nd date to the min variables. Else, read next date.
    • I read the third date.Compare it with the min variables. If the 3rd date is earlier than the min variables, then assign the 3rd date to the min variables. Else, read next date.
    • I read 0/0/0. Print the min variables and exit.


    I would use a do-while loop.

    Also if you know structs, this application is ideal for them
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Idk structs. and are you saying use a do-while loop for getting the dates? am i still going to be able to use the function i have made?

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Not sure i understand how to set up the do-while loop... so do { ....... while(???), what goes in the while loop?

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    It might help to first write a program which is basically the same but simpler -- write a program that asks the user for simple integers until the user enters the special integer -999. Afterwards, print the smallest integer that was entered. If you can make the program I just described, then you basically do the same thing for the date problem (except with the date you have three items to compare).

    Also it might help if you have a compare function that works similarly to integers. For example, if you have integers x and y, then the following is how it is usually done:

    Code:
    if (x < y) {
       printf("x comes before y\n");
    }else{
        printf("x does not come before y\n");
    }
    It is nice if we can basically do the same for a user defined type like a date. The conventional way is to make a comparison function which returns -1 if the first object is less than the second one, 0 if they are the same, and 1 if the first object is greater than the second object. The advantage is that the above integer test looks similar:

    Code:
    if (compare(mm_a, dd_a, yyyy_a, mm_b, dd_b, yyyy_b) < 0) {
        printf("Date a comes before date b\n");
    }else{
        printf("Date a does not come before date b\n");
    }

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    3
    Why not using time.h library? I think it can save you a lot of work in programming and maintaining the code, and I'm pretty sure you can convert from one format to many formats using it.

    Using ctime(), strptime() and tm structure can save you some time for this task.
    more details regarding time.h can be found here

    And if I posted here irrelevant information, I am terribly sorry.
    Last edited by zeroice115; 01-29-2013 at 04:31 PM.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by zeroice115 View Post
    Why not using time.h library?
    This is a good suggestion. The time.h defines a struct tm which includes members for years, months, days, etc. Then the functions you need are

    strftime(3): format date/time - Linux man page (convert a struct tm into a string)
    strptime(3) - Linux man page (convert a string into a struct tm)
    mktime(3) - Linux man page (convert a struct tm into a time_t)
    difftime(3): calculate time diff - Linux man page (compare two values of time_t)

    Hmm... maybe the number of functions in time.h is intimidating for novice users. Maybe for learning purposes, design your own simple solution, and then come back to see how you can do something similar using the functions above.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  2. Replies: 1
    Last Post: 03-16-2010, 10:17 AM
  3. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM