0

This is my code but for some reason it is not printing anything out in the complier. Please help! :(

#include <stdio.h>
int leapYear (void);
int leapYear ()
{
    printf("Please enter a year: ");
    scanf("%d", &year);
    if ((year % 400) == 0)
    {
        printf("%d is a leap year \n", year);
    }
    else
        printf("%d is not a leap year \n", year);
    return (year);
}

int main()
{
    int leapYear();
}
Erin D
  • 1
  • 1
  • 1
    where is year declared? you dont call leapYear with any parameters. There are many gaps in your understanding here. – Bwebb Nov 30 '18 at 01:50
  • I thought it was declared in the int leapYear (int year)? – Erin D Nov 30 '18 at 01:51
  • this: `int main() { int leapYear(); }` is only another prototype for the function: `leapYear()`. Suggest: `int main() { int yes_no = leapYear( 1946 ); }` – user3629249 Nov 30 '18 at 02:01
  • If you really want to user to enter the year, then the prototype should be: `int leapYear( void );` and the signature should be: `int leapYear()` – user3629249 Nov 30 '18 at 02:03
  • 3
    Please do NOT post code in a comment to your question, Rather post a EDIT in the question itself – user3629249 Nov 30 '18 at 02:08
  • the question states that the returned value from the sub function will be 'y' or 'n' However, then the returned type should be: `char`, not `int` Note: the question states that the sub function should return a 'y' or n' but it actually returns an `int` representing the year entered by the user. – user3629249 Nov 30 '18 at 02:13
  • OT: when calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was successful – user3629249 Nov 30 '18 at 02:14
  • 1
    here is the correct algorithm for determining if a specific year is a leap year. *Since 1752, in this country, years exactly divisible by 100 are only leap years when they are also exactly divisible by 400. So 1800 and 1900 were not leap years, neither will 2100 or 2200 be leap years.* – user3629249 Nov 30 '18 at 02:20

1 Answers1

1
#include <stdio.h>
#include <assert.h>

int isleap(int year)
{
    assert(year > 0 && "isleap: Year must be positive");
    return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}

int main(void)
{
    int isleap(int); // declare the function, naming the types of the arguments

    printf("%s\n", isleap(2002) ? "Leap year" : "Not leap year");
}

Your algorithm for leap years is wrong. I have modified it. A year is leap if at least one of these conditions is true:

  • The year is divisible by four but not by 100.
  • The year is divisible by 400.

On a side note, it is better to separate the algorithm and the display of its results in two different functions. isleap just tells us if a given year is leap. main relies upon isleap to report to it this, then prints an appropriate message. This makes our programs easier to read (by humans) and more extensible.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
  • why are you declaring the function leapYear inside of main? that function isnt used in the code you provided, what is it declared at all? – Bwebb Nov 30 '18 at 02:17
  • @Bwebb My bad, I intended to put `isleap`. – lost_in_the_source Nov 30 '18 at 02:18
  • Why are you declaring functions in main even if its the one you intended? Is this some java syntax bleeding over to C or something im not familiar with in C? – Bwebb Nov 30 '18 at 02:19
  • @Bwebb It is valid C code. In fact, this is a principle of data hiding: if, for whatever reason, we don't want any function but `main` to access `isleap`, we place its declaration inside `main` rather than at the global scope – lost_in_the_source Nov 30 '18 at 02:22
  • you are correct, but it looks unusual for C. https://stackoverflow.com/questions/33468027/declaring-a-function – Bwebb Nov 30 '18 at 02:25