Thread: A program which create errors to calculate leap year?

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    5

    A program which create errors to calculate leap year?

    Code:
    #include <iostream>
    using namespace std;
    int calculate()
    {
    	int days,m,y;
    	cout<<"enter the date(mm-dd-yy)";
    	cin>>m>>day>y;
    	if(m==1,m==3,m==5,mm==7,m==8,m==10,m==12)
    	{
    		days=31;
    		else if(m==4,m==6,m==9,m==11)
    		{
    			days=30;
    		}
    		else if(y%4==0&&m==2)
    		{
    			days=29;
    		}
    		else if(m==2&&day<=28)
    		{
    			cout<<"yout date is correct";
    			else
    			{cout<<"invalid date";
    			}
    			void main()
    			{
    				int a;
    				cout<<"enter the date(mm-dd-yy)
    					a.calculate();
    				cout<<a;
    			}
    
    		}
    There r some error in dis program . i use if statements and i think void main function calls in the end of dis program that creates errors into it.plz guide me to remove these errors.thx

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You should start by indenting your code properly. Note that the main function should be defined outside of the calculate() function, and it should be declared with a return type of int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    There is a small error in the very first if statement. You have written
    mm=7
    whereas you have defined the variable to be 'm'.
    Also you cannot define a function within another so main should be defined outside of calculate and main returns int not void.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    5
    Quote Originally Posted by BEN10 View Post
    There is a small error in the very first if statement. You have written
    mm=7
    whereas you have defined the variable to be 'm'.
    Also you cannot define a function within another so main should be defined outside of calculate and main returns int not void.
    thx for ur guideline i correct my small errors and function but its still give some errors so plz can u tell me correct syntax for this function becz im new in this C++ programming

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    5
    Quote Originally Posted by laserlight View Post
    You should start by indenting your code properly. Note that the main function should be defined outside of the calculate() function, and it should be declared with a return type of int.
    thx for ur guidline

  6. #6
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    by the way i think this is not right
    Code:
    cin>>m>>day>y;
    this should be
    Code:
    cin>>m>>days>>y;
    does it not give you any compile time error/?
    where did you declare day!? where is the mm declaration!? i assume they are mistyped!
    and there are couple of errors !( dangling! if and lost braces! i think)
    your main function should be sth like this

    Code:
    #include <iostream>
    using namespace std;
    
    int calculate(void);
    int main()
    {
    	int a;
    	cout<<"enter the date(mm-dd-yy)";
    	
    	cout<<calculate(); //because actually you didnt return anything from your function! you must specify the return value! rather than 0, if there is no return value , why bother declaring it as int calculate()?,  
    	return 0;
    }
    line 35 :
    Code:
    cout<<"enter the date(mm-dd-yy);
    lacks a " mark at the end of the string! before ;.
    Code:
    cout<<"enter the date(mm-dd-yy)";
    line 36:
    Code:
    a.calculate();
    this is not a class! and you did not make an object out of that class! so this is wrong! instead you should have written :
    Code:
    a = calculate();
    which implies that the returned value
    form function "calculate" should be copied into variable 'a' . but in your case this is wrong too! because in calculate it is not clear which variable should be returned!
    so i would write
    Code:
    cout<< calculate();
    and as the others mentioned before, use int main () instead of void main()
    ---------------
    to omit the error:
    expected primary-expression before "else"|
    you get , you must put enclose the if statements in braces so this means
    instead of
    Code:
        if(m==1,m==3,m==5,m==7,m==8,m==10,m==12)
        {
            days=31;
        
        else if(m==4,m==6,m==9,m==11)
        {
            days=30;
        }
    you must write

    Code:
    if( m == 1 || m == 3 || m == 5 || m == 7 ||  m == 8 ||  m == 10 || m==12 )
    	{
    		days = 31;
    	}
    by the way please note that , i replaced all of comas ',' with '||' which i think i makes more sense! because i think you indented to OR the conditions! and for that we use '||' , if you use coma, that just make a logic error!it wont or the conditions!
    the same thing goes here too
    instead of
    Code:
    else if(m==2&&day<=28)
        {
            cout<<"yout date is correct";
                
                else
            {
                        cout<<"invalid date";
            }
    i guess you should write:
    Code:
        else if(m==2&&days<=28)
        {
            cout<<"yout date is correct";
        }
                    else
                    {
                            cout<<"invalid date";
                    }
    . so the whole program without any compile time error is as :
    Code:
    #include <iostream>
    using namespace std;
    
    int calculate(void);
    int main()
    {
    	int a;
    	cout<<"enter the date(mm-dd-yy)";
    	
    	cout<<calculate();
    	return 0;
    }
    
    int calculate()
    {
    	int days,m,y;
    	cout<<"enter the date(mm-dd-yy)";
    
    	cin>>m>>days>>y;
    
    	if( m == 1 || m == 3 || m == 5 || m == 7 ||  m == 8 ||  m == 10 || m==12 )
    	{
    		days = 31;
    	}
    
    	else if( m == 4 || m == 6 || m == 9 || m == 11 )
    	{
    		days = 30;
    	}
    	else if( y % 4 == 0 && m == 2 )
    	{
    		days = 29;
    	}
    	else if( m == 2 && days <= 28 )
    	{
    		cout<<"yout date is correct";
    	}
            else
             {
                    cout<<"invalid date";
            }
    
    
    return 0;
    }
    but remember i think this code has some logic errors! so thats on you!
    and once more again! , i just tried to omit the compile time errors! and i must say that there are some sever logic errors and also problems in your program! your program is not complete , is it!?
    and i think its a good idea , to let a professional ( admin or other pros here) see this thread and guide you.
    by the way someone verify i didnt screw up sth ( if did say sth wrong please tell me.):d
    Last edited by Masterx; 05-02-2009 at 01:03 PM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    5
    Quote Originally Posted by Masterx View Post
    by the way i think this is not right
    Code:
    cin>>m>>day>y;
    this should be
    Code:
    cin>>m>>days>>y;
    does it not give you any compile time error/?
    where did you declare day!? where is the mm declaration!? i assume they are mistyped!
    and there are couple of errors !( dangling! if and lost braces! i think)
    your main function should be sth like this

    Code:
    #include <iostream>
    using namespace std;
    
    int calculate(void);
    int main()
    {
    	int a;
    	cout<<"enter the date(mm-dd-yy)";
    	
    	cout<<calculate(); //because actually you didnt return anything from your function! you must specify the return value! rather than 0, if there is no return value , why bother declaring it as int calculate()?,  
    	return 0;
    }
    line 35 :
    Code:
    cout<<"enter the date(mm-dd-yy);
    lacks a " mark at the end of the string! before ;.
    Code:
    cout<<"enter the date(mm-dd-yy)";
    line 36:
    Code:
    a.calculate();
    this is not a class! and you did not make an object out of that class! so this is wrong! instead you should have written :
    Code:
    a = calculate();
    which implies that the returned value
    form function "calculate" should be copied into variable 'a' . but in your case this is wrong too! because in calculate it is not clear which variable should be returned!
    so i would write
    Code:
    cout<< calculate();
    and as the others mentioned before, use int main () instead of void main()
    ---------------
    to omit the error:
    you get , you must put enclose the if statements in braces so this means
    instead of
    Code:
        if(m==1,m==3,m==5,m==7,m==8,m==10,m==12)
        {
            days=31;
        
        else if(m==4,m==6,m==9,m==11)
        {
            days=30;
        }
    you must write

    Code:
    if( m == 1 || m == 3 || m == 5 || m == 7 ||  m == 8 ||  m == 10 || m==12 )
    	{
    		days = 31;
    	}
    by the way please note that , i replaced all of comas ',' with '||' which i think i makes more sense! because i think you indented to OR the conditions! and for that we use '||' , if you use coma, that just make a logic error!it wont or the conditions!
    the same thing goes here too
    instead of
    Code:
    else if(m==2&&day<=28)
        {
            cout<<"yout date is correct";
                
                else
            {
                        cout<<"invalid date";
            }
    i guess you should write:
    Code:
        else if(m==2&&days<=28)
        {
            cout<<"yout date is correct";
        }
                    else
                    {
                            cout<<"invalid date";
                    }
    . so the whole program without any compile time error is as :
    Code:
    #include <iostream>
    using namespace std;
    
    int calculate(void);
    int main()
    {
    	int a;
    	cout<<"enter the date(mm-dd-yy)";
    	
    	cout<<calculate();
    	return 0;
    }
    
    int calculate()
    {
    	int days,m,y;
    	cout<<"enter the date(mm-dd-yy)";
    
    	cin>>m>>days>>y;
    
    	if( m == 1 || m == 3 || m == 5 || m == 7 ||  m == 8 ||  m == 10 || m==12 )
    	{
    		days = 31;
    	}
    
    	else if( m == 4 || m == 6 || m == 9 || m == 11 )
    	{
    		days = 30;
    	}
    	else if( y % 4 == 0 && m == 2 )
    	{
    		days = 29;
    	}
    	else if( m == 2 && days <= 28 )
    	{
    		cout<<"yout date is correct";
    	}
            else
             {
                    cout<<"invalid date";
            }
    
    
    return 0;
    }
    but remember i think this code has some logic errors! so thats on you!
    and once more again! , i just tried to omit the compile time errors! and i must say that there are some sever logic errors and also problems in your program! your program is not complete , is it!?
    and i think its a good idea , to let a professional ( admin or other pros here) see this thread and guide you.
    by the way someone verify i didnt screw up sth ( if did say sth wrong please tell me.):d
    thx for ur guidline i understood tht there r some logical error.i hope to try best to solve this prob but can u tell me this program with different statements like switch or somthin else.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    Nit pick:
    You are not handling the years properly for leap years. Centuries (xx00) need special handling.

  9. #9
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Every hundredth year is not a leap year, but every four hundredth one is actually a leap year. And yes, you've got some severe logic errors. It only accepts dates which are not a leap year nor a full century and whose month is february.

  10. #10
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by Brafil View Post
    Every hundredth year is not a leap year, but every four hundredth one is actually a leap year. And yes, you've got some severe logic errors. It only accepts dates which are not a leap year nor a full century and whose month is february.
    yep , and check this out , this will help you more
    CodeProject: Leap Year. Free source code and programming help
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    5
    Quote Originally Posted by Masterx View Post
    yep , and check this out , this will help you more
    CodeProject: Leap Year. Free source code and programming help
    thx now i understand its logic

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Help with leap year program.
    By IxTanGxI in forum C Programming
    Replies: 8
    Last Post: 02-20-2006, 08:49 PM
  3. Trying to create a payroll calculation program
    By nadeni0119 in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2003, 04:14 PM
  4. function
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-02-2002, 07:38 PM
  5. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 11:50 PM