user3473994
user3473994

Reputation: 41

scanning in 2 different types of inputs for a date

I have a perfectly working code that is able to tell me what day of the week it is when i input a certain date, however i am stuck as to how to take in the inputs. I have to be able to take in both of these inputs in the forms:

mm/dd/yyyy~~~~~~~~~~~~~~~Example: 03/04/2014

or

Month dd, yyyy~~~~~~~~~~~~Example: March 04, 2014

I am having trouble as to how i should make my program be able to take in both these different kinds of inputs and spit out the correct weekday no matter which input is used.

#include <stdio.h>
int main(){
int inputyear, daynumb, days_passed_since_anchor, inputmonth, days_in_month,totdays_for_7,rmd_for_7;

days_in_month=days_passed_in_months(inputmonth, inputyear);
//call  function to get days in all the previous months in the current year, if we are in march then there were 59 days before this month if its a non leap year.

      days_passed_since_anchor = (inputyear-1905) * (365.25);

      daynumb = days_in_month + inputday;

      totdays_for_7 = days_passed_since_anchor + daynumb;

      rmd_for_7=totdays_for_7%7;
      if(rmd_for_7==2){
          printf("Monday \n");
        }
      if(rmd_for_7==3){
          printf("Tuesday \n");
        }
      if(rmd_for_7==4){
          printf("Wednesday \n");
        }
      if(rmd_for_7==5){
          printf("Thursday \n");
        }
      if(rmd_for_7==6){
          printf("Friday \n");
        }
      if(rmd_for_7==0){
          printf("Saturday \n");
        }
      if(rmd_for_7==1){
          printf("Sunday \n");
        }
      return 0;

}

Upvotes: 0

Views: 209

Answers (1)

Deduplicator
Deduplicator

Reputation: 45704

mm/dd/yyyy~~~~~~~~~~~~~~~Example: 03/04/2014
Month dd, yyyy~~~~~~~~~~~~Example: March 04, 2014 

Please observe that one format starts with numbers, the other with a name. So, try this:

char buffer[32];
if(3 != fscanf(file, "%d/%d/%d", &month, &day, &year))
  if(3 != fscanf(file, "%31s %d, %d", buffer, &day, &year))
    abort()
  else
    month = mapmonthname(buffer);
/* Now do fun things... */

Upvotes: 2

Related Questions