Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problems in C

Options
  • 22-10-2004 8:19pm
    #1
    Closed Accounts Posts: 4,943 ✭✭✭


    I'm trying to do a program for homework (one to work out whether a year is a leap year or not), but i'm having a few problems.

    Firstly, a leap year is any year divisible by 4, OR a centenary year divisible by 400.

    How do i check if a number IS divisible easily by four. I've tried the following code (as a seperate program) to look for a way to do this. I am thinking along the lines that when i divide the date by 4, the output should be an integer, and i was testing this idea in this quasi program.
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    
      /* Declare variables*/
      float number;
    
    
      /* Accept Inputs */
      printf("\nEnter value of the number: ");
      scanf("%f", &number);
    
      /* Calculations */
      if number == int
      {
        printf{"number is an integer");
      }
      else
      {
      printf("ffs you ****. This ain't an integer!");
      }
     
     return 0; 
    }
    

    But i get an error compiling it. I get an error in line 16 "parse error before number". I assume that its because i used "int" in a stupid way, but its all i can think of.


Comments

  • Closed Accounts Posts: 612 ✭✭✭Phil_321


    (pseudocode)

    int year;
    bool leapyear = false;

    Get the input from the user.

    if (year%4 == 0)
    {
    leapyear = true;
    }


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Ok, thanks to the above post, i really managed to get cracking on the problem. However, there are still 2 niggly bugs i can't seem to squash, even though i thought i coded it right.

    Bug1) It is still showing up "DATE is/is not a leapyear" for dates that are out of range. I don't want it to display that, as the date is OUT OF RANGE!

    Bug2) All centenary years are showing up as leapyears. But i could swear i have it coded to stop that :( Give me pointers on where to look, not exact solutions (unless i did something really stupid, or its incredibly complicated :p)
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    
      /* Declare variables*/
      int date;
    
    
      /* Accept Inputs */
      printf("\nType in the year, between 1800ad and 9999ad, in the form XXXX: ");
      scanf("%d", &date);
    
      /* Calculations*/
      if (date < 1800 || date > 9999)
        {
        printf("\nDate is out of range");
        }
      
      if (date%1000 == 0)
        {
          if (date%400 == 0)
    	{
    	printf("\n%d is a leapyear", date);
    	}
          else
    	{
    	printf("\n%d is not a leapyear", date);
    	}
        }
      else if (date%4 == 0 && date%1000 !=0)
        {
          printf("\n%d is a leapyear", date);
        }
      else if (date%4 != 0 && date%1000 != 0)
        {
          printf("\n%d is not a leapyear", date);
        }
      
      return 0;
    }
    


    EDIT: I tried using a do - while loop to only do the second half of the code IF the date was in range, but i couldn't get it working. That should solve bug 1 IF i could get it to work.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    I got it sorted. It turns out i misunderstood what a centenary was. Its actually, 100, 200, 300 etc, not 1000, 2000, 3000 etc.

    Also, i sorted bug 1 by sticking a big else in front of the second half of the code. Feel free to poke out bugs or make coding tips if you're bored.
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    
      /* Declare variables*/
      int date;
    
    
      /* Accept Inputs */
      printf("\nType in the year, between 1800ad and 9999ad, in the form XXXX: ");
      scanf("%d", &date);
    
      /* Calculations*/
      if (date < 1800 || date > 9999)
        {
        printf("\nDate is out of range");
        }
      
      else
        {
      if (date%100 == 0)
        {
          if (date%400 == 0)
    	{
    	printf("\n%d is a leapyear", date);
    	}
          else
    	{
    	printf("\n%d is not a leapyear", date);
    	}
        }
      else if (date%4 == 0 && date%100 !=0)
        {
          printf("\n%d is a leapyear", date);
        }
      else if (date%4 != 0 && date%100 != 0)
        {
          printf("\n%d is not a leapyear", date);
        }
        }
      return 0;
    }
    


  • Registered Users Posts: 2,534 ✭✭✭FruitLover


    You could maybe set up a loop so that when an invalid year is entered, it prompts for another date and rescans, instead of dumping out of the program. Also, nested if statements are messy and can be hard to follow. Now that you have the program working, see if you can figure out a way to make the code a bit clearer.


  • Registered Users Posts: 950 ✭✭✭jessy


    Why is everyone including the math’s library?


  • Advertisement
Advertisement