Like Tree7Likes

Leap year program error

This is a discussion on Leap year program error within the C Programming forums, part of the General Programming Boards category; I need to make a program to check leap year by using - if statement, logical operator and ternary operator ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    8

    Leap year program error

    I need to make a program to check leap year by using - if statement, logical operator and ternary operator from switch statement

    Please correct this code.. its not working

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        int year;
        char ch;
        printf("Enter your choice A, B or C.\n");
        switch(ch)
        {
            case 'a':
                {
                    printf("Enter a year to check if it is a leap year\n");
                    scanf("%d",&year);
                    if(year%400==0)
                        printf("%d is a leap year.\n", year);
                    else if(year%100==0)
                        printf("%d is not a leap year.\n", year);
                    else if(year%4==0)
                        printf("%d is a leap year.\n", year);
                    else
                        printf("%d is not a leap year.\n", year);  
                    break;
                }
            case 'b':
                {
                    printf("Enter a year to check if it is a leap year\n");
                    scanf("%d",&year);
                    if(year%400==0||year%4==0&&year%100!=0)
                        printf("%d is  leap year.\n", year);
                    else
                        printf("%d is not a leap year.\n", year);  
                    break;
                }
            case 'c':
                {
                    printf("Enter a year to check if it is a leap year\n");
                    scanf("%d",&year);
                    ((year%400==0||year%4==0)?printf("%d is a leap year.\n", year):printf("%d is not a leap year.\n", year));
                    break;
                }
            default:
                printf("%d is not a leap year.\n", year);
        }
    
        getch();
    
    }
    Last edited by nsriv; 12-16-2012 at 05:35 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What equation (in regular math) are you using to decide if a year is a leap year or not?

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    8
    Quote Originally Posted by Adak View Post
    What equation (in regular math) are you using to decide if a year is a leap year or not?
    the year should be divisible by 400 or 4 but not by 100

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    You need the user to actually enter a value for 'ch'. Currently when it runs ch is undefined.
    dennis.cpp and Adak like this.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by nsriv View Post
    the year should be divisible by 400 or 4 but not by 100

    Is that divisible by 400 *and* by 4, but not 100, or divisible by 400 *or* by 4, but not by 100?

    Not being stubborn, I don't recall this equation.

    You're handling the ch problem, right?
    Last edited by Adak; 12-16-2012 at 07:06 AM.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    8
    Quote Originally Posted by DeadPlanet View Post
    You need the user to actually enter a value for 'ch'. Currently when it runs ch is undefined.
    ch and year both are undefined.. and int year = 0 is also not working

    Quote Originally Posted by Adak View Post
    Is that divisible by 400 *and* by 4, but not 100, or divisible by 400 *or* by 4, but not by 100?

    Not being stubborn, I don't recall this equation.

    You're handling the ch problem, right?
    Im too confused about this.. but actually logic is not important.. only the program must run.

    I get this output after two warnings
    Enter your choice A, B or C.
    -858993460 is not a leap year.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by nsriv
    Im too confused about this.. but actually logic is not important.. only the program must run.
    Err... if the logic is not important and only the program must run, then submit this program:
    Code:
    int main(void) { return 0; }
    Don't dare? That's because the logic is important.

    "The most important single aspect of software development is to be clear about what you are trying to build." - Bjarne Stroustrup
    AndiPersti likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by Adak View Post
    Is that divisible by 400 *and* by 4, but not 100, or divisible by 400 *or* by 4, but not by 100?

    Not being stubborn, I don't recall this equation.
    Years that are divisible by 4 are leap years, unless the year is divisible by 100. Exceptions are when the year is divisible by 400 in which case the year is a leap year.

    @nsriv

    I'm going to come off as a dick here but here's the logic you should be following to debug your program.

    The output from my program when run is : "Enter your choice A, B or C.
    -858993460 is not a leap year. "
    I want to see what code was executed so I can follow it through in my head to see why I got that output.
    The program must have jumped straight to the 'default' case because all my other cases would result in different output.
    If my program is jumping straight to the 'default' case then 'ch' must not be 'a', 'b', or 'c'. What is ch then?

    I usually wouldn't give a response like this but if this is indeed your code then I believe that you're at the level where you should understand where you went wrong if you actually read it through properly.
    rags_to_riches likes this.

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    8
    DeadPlanet

    Yes scanf is missing for ch.. i added it on the code..
    but still program has errors.. year is not initialised

    Code:
    
    #include<stdio.h> #include<conio.h> void main() {     int year;     char ch;     printf("Enter your choice A, B or C.\n");     switch(ch)     {         case 'a':             {                 printf("Enter a year to check if it is a leap year\n");                 scanf("%d",&year);                 if(year%400==0)                     printf("%d is a leap year.\n", year);                 else if(year%100==0)                     printf("%d is not a leap year.\n", year);                 else if(year%4==0)                     printf("%d is a leap year.\n", year);                 else                     printf("%d is not a leap year.\n", year);                   break;             }         case 'b':             {                 printf("Enter a year to check if it is a leap year\n");                 scanf("%d",&year);                 if(year%400==0||year%4==0&&year%100!=0)                     printf("%d is  leap year.\n", year);                 else                     printf("%d is not a leap year.\n", year);                   break;             }         case 'c':             {                 printf("Enter a year to check if it is a leap year\n");                 scanf("%d",&year);                 ((year%400==0||year%4==0)?printf("%d is a leap year.\n", year):printf("%d is not a leap year.\n", year));                 break;             }         default:             printf("%d is not a leap year.\n", year);     }      getch();  }

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by nsriv View Post
    Im too confused about this.. but actually logic is not important.. only the program must run.
    I think this statement is your fundamental problem. If you just want it to work but don't care why, then why even bother trying to write this?

  11. #11
    Registered User
    Join Date
    Dec 2012
    Posts
    8
    Quote Originally Posted by c99tutorial View Post
    I think this statement is your fundamental problem. If you just want it to work but don't care why, then why even bother trying to write this?
    I said that cause i know the problem is in the code and not in the leap year logic..
    Here is the correct code

    Thanks for the help guys.

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        int year;
        char ch;
        printf("Enter your choice A, B or C.\n");
        scanf("%c",&ch);
        switch(ch)
        {
            case 'a':
                {
                    printf("Enter a year to check if it is a leap year\n");
                    scanf("%d",&year);
                    if(year%400==0)
                        printf("%d is a leap year.\n", year);
                    else if(year%100==0)
                        printf("%d is not a leap year.\n", year);
                    else if(year%4==0)
                        printf("%d is a leap year.\n", year);
                    else
                        printf("%d is not a leap year.\n", year);  
                    break;
                }
            case 'b':
                {
                    printf("Enter a year to check if it is a leap year\n");
                    scanf("%d",&year);
                    if(year%400==0||year%4==0&&year%100!=0)
                        printf("%d is  leap year.\n", year);
                    else
                        printf("%d is not a leap year.\n", year);  
                    break;
                }
            case 'c':
                {
                    printf("Enter a year to check if it is a leap year\n");
                    scanf("%d",&year);
                    ((year%400==0||year%4==0)?printf("%d is a leap year.\n", year):printf("%d is not a leap year.\n", year));
                    break;
                }
            
        }
    
        getch();
    
    }

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    826
    Can anybody tell me the perceived benefit of teaching students to solve problems using a monolithic main(), and reading inputs from standard input, as opposed to the standard, old, tried and true method of accepting command-line parameters?

    Is it true that something like the following is that much harder to understand? I'm not being snippy, I'm really wondering.
    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    int is_leap_year(const int year)
    {
        return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
    }
    
    int main(int argc, char *argv[])
    {
        int arg, year;
        char dummy;
    
        if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "/?") || !strcmp(argv[1], "--help")) {
            fprintf(stderr, "Usage: %s [ -h | --help | /? ]\n", argv[0]);
            fprintf(stderr, "       %s year [ year ... ]\n", argv[0]);
            fprintf(stderr, "This program reports whether 'year' is a leap year or not.\n");
            return 0;
        }
    
        for (arg = 1; arg < argc; arg++) {
            if (sscanf(argv[arg], " %d %c", &year, &dummy) != 1) {
                fprintf(stderr, "%s: Invalid parameter.\n", argv[arg]);
                return EXIT_FAILURE;
            } else
            if (is_leap_year(year)) {
                printf("%d is a leap year.\n", year);
            } else {
                printf("%d is not a leap year.\n", year);
            }
        }
    
        return EXIT_SUCCESS;
    }
    Maybe some other coding style would work better for students, no doubt. But if you consider how close to that example is real-world code, and how far the example code in this thread is to acceptable real-world code, there is a staggering difference.

    A particular example I think many students would find interesting, is internationalization (or i18n for short). If you use gettext for internationalization, then the internationalized version of the leap year program is
    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <locale.h>
    #include <libintl.h>
    #include <stdio.h>
    
    static const char appname[] = "leapyear";
    static const char localedir[] = "/usr/share/locale";
    
    int is_leap_year(const int year)
    {
        return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
    }
    
    int main(int argc, char *argv[])
    {
        int arg, year;
        char dummy;
    
        setlocale(LC_ALL, "");
        bindtextdomain(appname, localedir);
        textdomain(appname);
    
        if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "/?") || !strcmp(argv[1], "--help")) {
            fprintf(stderr, gettext("Usage: %s [ -h | --help | /? ]\n"), argv[0]);
            fprintf(stderr, gettext("       %s year [ year ... ]\n"), argv[0]);
            fprintf(stderr, gettext("This program reports whether 'year' is a leap year or not.\n"));
            return 0;
        }
    
        for (arg = 1; arg < argc; arg++) {
            if (sscanf(argv[arg], " %d %c", &year, &dummy) != 1) {
                fprintf(stderr, gettext("%s: Invalid parameter.\n"), argv[arg]);
                return EXIT_FAILURE;
            } else
            if (is_leap_year(year)) {
                printf(gettext("%d is a leap year.\n"), year);
            } else {
                printf(gettext("%d is not a leap year.\n"), year);
            }
        }
    
        return EXIT_SUCCESS;
    }
    Using the gettext tools (xgettext to extract the strings), and adding the Finnish translations, you get fi/leapyear.po :
    Code:
    # leapyear example program, Finnish translations
    # This file is in public domain.
    #
    msgid ""
    msgstr ""
    "Project-Id-Version: leapyear\n"
    "Report-Msgid-Bugs-To: \n"
    "POT-Creation-Date: 2012-12-16 21:34+0200\n"
    "PO-Revision-Date: 2012-12-16 21:34+0200\n"
    "Last-Translator: Nominal Animal <nominal-animal@example.com>\n"
    "Language-Team: Nominal Animal <nominal-animal@example.com>\n"
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    
    #: leapyear.c:21
    #, c-format
    msgid "Usage: %s [ -h | --help | /? ]\n"
    msgstr "Käyttö: %s [ -h | --help | /? ]\n"
    
    #: leapyear.c:22
    #, c-format
    msgid "       %s year [ year ... ]\n"
    msgstr "        %s vuosi [ vuosi ... ]\n"
    
    #: leapyear.c:23
    #, c-format
    msgid "This program reports whether 'year' is a leap year or not.\n"
    msgstr "Tämä ohjelma kertoo, onko 'vuosi' karkausvuosi vai ei.\n"
    
    #: leapyear.c:29
    #, c-format
    msgid "%s: Invalid parameter.\n"
    msgstr "%s: Ei ole vuosiluku.\n"
    
    #: leapyear.c:33
    #, c-format
    msgid "%d is a leap year.\n"
    msgstr "%d on karkausvuosi.\n"
    
    #: leapyear.c:35
    #, c-format
    msgid "%d is not a leap year.\n"
    msgstr "%d ei ole karkausvuosi.\n"
    which can be edited using any PO file editor, then compiled using msgfmt and finally installed system-wide (in my case to /usr/share/locale/fi/LC_MESSAGES/leapyear.po). After that, if I have a Finnish locale, I get the output in Finnish, and in English everywhere else:
    Code:
    $ export LANGUAGE=en_US:en
    $ ./leapyear
    Usage: ./leapyear [ -h | --help | /? ]
           ./leapyear year [ year ... ]
    This program reports whether 'year' is a leap year or not.
    $ export LANGUAGE=fi_FI:fi
    $ ./leapyear
    Käyttö: ./leapyear [ -h | --help | /? ]
            ./leapyear vuosi [ vuosi ... ]
    Tämä ohjelma kertoo, onko 'vuosi' karkausvuosi vai ei.
    It seems to me that this path would be a much more useful path for students. Not only is it that much closer to real world applications, but it also provides optional branching to internationalization; perhaps a few students would find interesting on-line projects to participate in. (While most translations are very straightforward, sometimes translations across language family boundaries mean small changes are needed to the source code -- things like parameter order et cetera --, which there is a clear path to transition from translation to development.)

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by Nominal Animal View Post
    Can anybody tell me the perceived benefit of teaching students to solve problems using a monolithic main(), and reading inputs from standard input, as opposed to the standard, old, tried and true method of accepting command-line parameters?
    Writing as in your code example would force students to design the solution to the problem in such a way as it would actually be reusable in other programs (like your is_leap_year function), while writing just a monolithic main might be seen as less effort.

    Writing a set of functions and/or re-usable classes is obviously better from a practical standpoint, but if the homework problem (or in some cases, an assignment in the field) doesn't explicitly require this level of reusability... it might be hard to justify the extra time investment from a logistics standpoint.

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    Can anybody tell me the perceived benefit of teaching students to solve problems using a monolithic main(), and reading inputs from standard input, as opposed to the standard, old, tried and true method of accepting command-line parameters?
    I think that it may be so that the author of the code can start writing code before they learn about command line inputs -> Nothing more than to help lessen the learning curve.
    Salem and stahta01 like this.
    Fact - Beethoven wrote his first symphony in C

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    826
    Quote Originally Posted by Click_here View Post
    I think that it may be so that the author of the code can start writing code before they learn about command line inputs
    I would understand that, but they all seem to use either
    Code:
    scanf("...", ...);
    or
    Code:
    fgets(array, 10, stdin);
    with absolutely no care for results or error checking. To me, that is the same as a driving instructor telling you to floor it, so we can go really fast: we'll deal with the carnage when we hit a wall.

    The only difference is that errors in programs are not perceived as as harmful. You think you won't be physically harmed by any bugs in your code. Sure, the programmer might not, but the amount of resources and money lost due to programmers neglect is astounding.

    So, why easy and fast, instead of slower but more powerful start?

    Anyway, sorry to bring it up here, as it is completely off topic. I see I'm quite alone in perceiving the serious issues caused by this sort of programming "education". Carry on; I'll go elsewhere.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. leap year program
    By camel-man in forum C Programming
    Replies: 5
    Last Post: 05-23-2011, 07:50 PM
  2. Leap year program using if statements
    By brian75 in forum C++ Programming
    Replies: 10
    Last Post: 12-06-2009, 01:57 PM
  3. why does this program say 1700 is a leap year?
    By newbcore in forum C Programming
    Replies: 7
    Last Post: 12-19-2008, 01:03 AM
  4. Program showing if a year its leap year or not.
    By Cyberman86 in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2008, 08:00 AM
  5. Help with leap year program.
    By IxTanGxI in forum C Programming
    Replies: 8
    Last Post: 02-20-2006, 07:49 PM

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