Leap year program using if statements

This is a discussion on Leap year program using if statements within the C++ Programming forums, part of the General Programming Boards category; I am trying to work out a program where it takes a user inputed year and outputs if that year ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    35

    Question Leap year program using if statements

    I am trying to work out a program where it takes a user inputed year and outputs if that year is a leap year or not.

    Right now from what I have when I put leap years into the program it reads the output, "It is not a leap year." And when you input a year that is not a leap year it will output nothing and just end the program.

    What am I doing wrong?

    Thanks for the help in advance, looking forward to working through this.

    Code:
    #include <iostream>
    
    int year;
    int leap1;
    int leap2;
    int leap3;
    
    int main()
    {
    	std::cout << "Enter a year and I will determine if it's a leap year:  " ;
    	std::cin >> year ;
    
    	leap1 = year % 4 ;
    	leap2 = year % 100 ;
    	leap3 = year % 400 ;
    
    	if ( leap1 == 0 )
    		if ( leap2 != 0 )
    			if ( leap3 == 0 )
    			{
    				std::cout << "It is a leap year." << '\n' ;
    			}
    			else 
    			{
    				std::cout << "It is not a leap year." << '\n';
    			}
    
    	return (0);
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    I'm already half in the bag, but try something like below in lieu;

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    int year;
    
    int main()
    {
    	std::cout << "Enter a year and I will determine if it's a leap year:  " ;
    	std::cin >> year ;
    	
    	int leap;
    	leap = year % 4 ;
    
    	if ( leap == 0 )
    	{
    		
    				std::cout << "It is a leap year.";
    				std::cin.get();
    			 }
    			else 
    			{
    				std::cout << "It is not a leap year.";
    				std::cin.get();
    			}
    
      
      system("PAUSE");	
      return 0;
    }
    Last edited by Oldman47; 12-04-2009 at 04:45 PM. Reason: Nope, that isn't right... damn alcohol!

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    That makes sense to a degree but that is not how you completely determine if a year is a leap year. It has to be divisible by 4 but not 100 but 400 is the exception.

    That if I understand correctly will just check if it is divisible by 4.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Never heard of that. I thought a leap year was every 4, without exception. Who makes up these crazy rules?

    Then;
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    
    
    int main()
    {
        int year;
    	std::cout << "Enter a year and I will determine if it's a leap year:  " ;
    	std::cin >> year ;
    	
    	int leap;
    	int leap2;
    	int leap3;
    	leap = year % 4 ;
    	leap2 = year % 100 ;
    	leap3 = year % 400 ;
    
      if(leap2!=0)
       {
    	if ( leap == 0 || leap3==0)
    	{
    		
    				std::cout << "It is a leap year.";
    				std::cin.get();
    				std::cin.get();
    			 }
    			  }
    			else 
    			{
    				std::cout << "It is not a leap year.";
    				std::cin.get();
    				std::cin.get();
    			}
       	
      return 0;
    }
    Last edited by Oldman47; 12-04-2009 at 05:39 PM.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    That program there tells you the output, "It's a leap year" on all leap years but say nothing and exits when you input a non leap year.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Well, keep rearranging it until it works.

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    
    
    int main()
    {
        int year;
    	std::cout << "Enter a year and I will determine if it's a leap year:  " ;
    	std::cin >> year ;
    	
    	int leap;
    	int leap2;
    	int leap3;
    	leap = year % 4 ;
    	leap2 = year % 100 ;
    	leap3 = year % 400 ;
    
    	if (leap2!=0 && leap == 0 || leap3==0)
    	{
    				std::cout << "It is a leap year.";
    				std::cin.get();
    				std::cin.get();
    			   }
    			else 
    			{
    				std::cout << "It is not a leap year.";
    				std::cin.get();
    				std::cin.get();
    			}
    
      return 0;
    }
    Last edited by Oldman47; 12-04-2009 at 06:53 PM.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    Bingo. That was going to be what I tried next. I had four different way written down on a piece of paper and I was working through them.

    Thanks for the help.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    My final code:

    Code:
     
    #include <iostream>
    
    int year;
    int leap;
    int leap2;
    int leap3;
    
    
    int main()
    {
    	std::cout << "Enter a year and I will determine if it's a leap year:  " ;
    	std::cin >> year ;
    	
    	leap = year % 4 ;
    	leap2 = year % 100 ;
    	leap3 = year % 400 ;
    
      if(leap == 0 || leap3 == 0 && leap2 != 0 )
      {
    				std::cout << "It is a leap year." << '\n' ;
    				
    			 }
    		else 
    			{
    				std::cout << "It is not a leap year." << '\n' ;
      }
       	
      return 0;
    }

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Glad you got it. I probably could have helped a bit quicker if I wasn't drinking Michelobs. lol

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    Ehh. I was in no rush.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You shouldn't make all these variables global. There's no reason not to declare them in main(), directly before usage. Furthermore, the names leap, leap2 and leap3 are unintuitive. Given how short their initializers are, it's probably most readable to simply omit the variables and put the equivalent expressions directly where the usage is.

    Also, your if-condition is messed up. It will tell you that every year divisible by 4 is a leap year. Consider:
    leap == 0 -> divisble by 4. If this condition is true, the whole or-expression is true, and the second part is never evaluated.
    If the condition is not true, then leap3 cannot be 0 either (there are no numbers divisble by 400 but not 4), and the entire expression is false.

    You want to write, "if year is divisible by 4 and not by 100, or else if it is divisible by 400, then it's a leap year".

    You should also always use parentheses to indicate precedence when mixing || and &&. && has higher precedence than ||, but (1) not all programmers know this, (2) it's not that way in all programming languages, and (3) it's easy to get wrong even if you actually know it.
    Last edited by CornedBee; 12-06-2009 at 02:03 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. n00b needs help with small program
    By rightbrainer in forum C Programming
    Replies: 3
    Last Post: 04-15-2009, 05:52 PM
  2. Replies: 0
    Last Post: 04-08-2009, 04:23 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. problem with if statements (leap year)
    By radiantarchon28 in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2007, 02:40 PM
  5. a program to create months in a year newbie question
    By robasc in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 04:41 PM

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