Leap Year

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karafire2003
    New Member
    • Apr 2007
    • 10

    Leap Year

    Hey there...i was wondering if someone can help me with my program. I'm doing the leap year program thing. i thought i had it all figured out, but it ALWAYS says that it's not a leap year. here's my code...if you can help i'd gladly appreciate it.
    int year, test4, test100, test400;
    printf("The amazing leap year teller thingie thing!!!\nPleas e enter the year to be tested: ");
    scanf("year");
    test4 = year%4;
    test100 = year%100;
    test400 = year%400;
    if((test4 == 0 && test100 != 0) || test400 == 0)
    printf("DING!! Leap Year");
    else
    printf("ERROR! You have not chosen a leap year.\nPlease try again.\n", year);
    system("pause") ;
    return 0;
    }

    thanks!
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by karafire2003
    Hey there...i was wondering if someone can help me with my program. I'm doing the leap year program thing. i thought i had it all figured out, but it ALWAYS says that it's not a leap year. here's my code...if you can help i'd gladly appreciate it.
    int year, test4, test100, test400;
    printf("The amazing leap year teller thingie thing!!!\nPleas e enter the year to be tested: ");
    scanf("year");
    test4 = year%4;
    test100 = year%100;
    test400 = year%400;
    if((test4 == 0 && test100 != 0) || test400 == 0)
    printf("DING!! Leap Year");
    else
    printf("ERROR! You have not chosen a leap year.\nPlease try again.\n", year);
    system("pause") ;
    return 0;
    }

    thanks!
    The problem is in your scanf call. You have "scanf("year"); ", that doesn't work. Scanf takes two arguements, the type of data, and the variable in which to store that data. For example in your case, try:
    Code:
    scanf("%d", year); //puts an integer in varibale "year"

    Comment

    • karafire2003
      New Member
      • Apr 2007
      • 10

      #3
      i tried that...and now after entering a year the program crashes.
      thanks the idea still :)

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by karafire2003
        i tried that...and now after entering a year the program crashes.
        thanks the idea still :)
        Hm, that's wierd. I assume you're getting a segmentation fault? Can you post your full code here so we can see what's going on?

        Comment

        • karafire2003
          New Member
          • Apr 2007
          • 10

          #5
          here's the whole thing.
          # include <stdio.h>
          int main()

          {
          int year, test4, test100, test400;
          printf("The amazing leap year teller thingie thing!!!\nPleas e enter the year to be tested: ");
          scanf("year");
          test4 = year%4;
          test100 = year%100;
          test400 = year%400;
          if((test4 == 0 && test100 != 0) || test400 == 0)
          printf("DING!! Leap Year");
          else
          printf("ERROR! You have not chosen a leap year.\nPlease try again.\n", year);
          system("pause") ;
          return 0;
          }

          when i did what you recommended...t here were no complier errors or anything...the program runs, but after a 4 digit number is entered...it crashes.
          thanks for your time...REALLY!! !

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by karafire2003
            here's the whole thing.
            # include <stdio.h>
            int main()

            {
            int year, test4, test100, test400;
            printf("The amazing leap year teller thingie thing!!!\nPleas e enter the year to be tested: ");
            scanf("year");
            test4 = year%4;
            test100 = year%100;
            test400 = year%400;
            if((test4 == 0 && test100 != 0) || test400 == 0)
            printf("DING!! Leap Year");
            else
            printf("ERROR! You have not chosen a leap year.\nPlease try again.\n", year);
            system("pause") ;
            return 0;
            }

            when i did what you recommended...t here were no complier errors or anything...the program runs, but after a 4 digit number is entered...it crashes.
            thanks for your time...REALLY!! !
            Ok, I got it:
            Instead of "scanf("%d, year);" put "scanf("%d" , &year);".
            You have to do this because scanf takes a pointer to a variable not just a regular variable.
            I tried it and it works.

            Comment

            • karafire2003
              New Member
              • Apr 2007
              • 10

              #7
              OH MYGOD...thank you SOOOOOOOOOOOOOO OO much.
              how did you figure it out? that way i'll be able to figure it out later on my own. thanks again!!!

              Comment

              • ilikepython
                Recognized Expert Contributor
                • Feb 2007
                • 844

                #8
                Originally posted by karafire2003
                OH MYGOD...thank you SOOOOOOOOOOOOOO OO much.
                how did you figure it out? that way i'll be able to figure it out later on my own. thanks again!!!
                You are welcome.
                I went to:

                It is a good place to learn and go back to for reference (my case).
                It also has a lot functions descriptions and how to use them. It's one of the best for C or C++.

                Comment

                Working...