1

I am attempting to write code that checks the validity of a date [day and month only], inputs that date [day and month] into a "Date" structure, and then, finally, checks that the code in fact did what I expected it to do in a test function, by printing "SUCCESS" if successful, or "FAILURE" if not;

I have tried everything, and cannot think of what the problem might be; I am getting no warnings, so debugger didnt help. the code compiles and runs, however, there is nothing being printed on screen to let me know if it is actually working or not;

Any feedback would be greatly appreciated, thank you!

#include <stdio.h>
#include <stdlib.h>
#include "PatientData.h"
#define INVALID -100
#define SUCCESS 0

int main()

{

int datechecker(int month, int day) {
    int ret = SUCCESS;
    month = 2;
    day = 2;
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
            || month == 10 || month == 12)
    {
        if(!(day >= 1 && day <= 31))
          ret = INVALID;
    }
    else if (month == 4 || month == 6 || month == 9 || month == 11)
    {
        if(!(day >= 1 && day < 31 ))
            ret = INVALID;
    }

    else
    {
        ret = INVALID;
    }
return(ret);
}


Date CreateDate(int day, int month)
{

Date date;

int ret = datechecker(2,2);
if (ret == SUCCESS)
{
date.Day = day;
date.Month = month;
}
else
{
    date.Day = INVALID;
}

return date;

}


void CreateDateTest ()
{
Date date = CreateDate(2,2);
if (date.Day == 2)
{
    printf("PASSED");
}
else
{
    printf("FAILED");
}
}

return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Zeus
  • 31
  • 3
  • It looks like you can never have a date in February; it will always be invalid. Pity that you're forcing the date to 2nd February. – Jonathan Leffler Sep 19 '15 at 05:42
  • 1
    Guess you are not a fan of leap years?! – Ed Heal Sep 19 '15 at 05:42
  • lol, shoot, didn't even realize I used February and did not include it in the code; GD IT!!!!!! will report back – Zeus Sep 19 '15 at 18:39
  • So I changed it to call a function with day and month variables that were actually defined in the date checker function, still didnt work :\ – Zeus Sep 19 '15 at 19:56

1 Answers1

2

Well, you have three functions defined inside main, called CreateDate, CreateDateTest, and datechecker. However, the lone executable statement inside main is return 0, which is what your program is faithfully doing.

Your code cannot be run as such since PatientData.h is not available, but I suspect it will work once you call CreateDateTest from main.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ram Rajamony
  • 1,717
  • 15
  • 17
  • 1
    [Nested functions are not standard C](http://stackoverflow.com/questions/2608158/nested-function-in-c). GCC allows them as an extension, but it seems unlikely this is what the OP really wants, anyway. – Nate Eldredge Sep 19 '15 at 04:52
  • I created a header file and included it in both the script where I define the functions [which is the same as the code in the question], and what they do, as well as the script that has my test function; However, for the script that includes my test function [same test function as in the code above, – Zeus Sep 19 '15 at 05:02
  • @Zeus: Even if you use (accidentally) non-standard features, you still have to call functions before they are executed. Since you must be using GCC, it is worth adding warning options to your compilation. You might consider the merits of `gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror …`; they're demanding, but they prevent a lot of problems too. Adding `-pedantic` and `-Wshadow` can also be helpful on occasion. – Jonathan Leffler Sep 19 '15 at 06:03
  • I'm using the eclipse IDE which comes loaded with warning option features; – Zeus Sep 19 '15 at 18:48