Any Ideas?

This is a discussion on Any Ideas? within the C Programming forums, part of the General Programming Boards category; can anyone see wot ive done wrong in this piece of code? it doesnt recognise leap years accurately. code: struct ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    Any Ideas?

    can anyone see wot ive done wrong in this piece of code? it doesnt recognise leap years accurately.

    code:
    struct dat{

    int day;
    int month;
    int year;
    };

    main()
    {
    // char looping ='y';
    // while (looping == 'y'||looping == 'Y')

    printf("***************************\n");
    printf("*A DATE CALCULATOR PROGRAM*\n");
    printf("***************************\n\n");

    int days, leap;
    struct dat t;

    printf("Please enter a date from which you wish calculate\n");
    printf("how many days to the 25/12/2002\n");


    scanf("%d/%d/%d", &t.day,&t.month,&t.year);
    leap = ( t.year % 4 == 0 && t.year % 100 != 0 || t.year % 400 == 0 );
    if (t.day > 31 ||
    t.day <= 0)

    printf("ERROR, there are no months with that many days\n");

    else if (t.month > 12 ||
    t.month <= 0)

    printf("ERROR, there are no years with that many months\n");

    else if (t.month == 1||
    t.month == 3||
    t.month == 5||
    t.month == 7||
    t.month == 8||
    t.month == 10||
    t.month == 12 &&t.day>31)

    printf("ERROR, the month you have entered doesnt have that many daysin it.\n");

    else if (t.month == 4||
    t.month == 6||
    t.month == 9||
    t.month == 11 && t.day > 30)

    printf("ERROR, the month you have entered doesnt have that many days in it.\n");

    else if (t.month == 2 && t.day == 29&& leap != 0)

    printf("ERROR, the year you entered isnt a leapyear.\n");

    else if (t.month == 2 && t.day > 29 && leap == 0)

    printf("ERROR, although this is a leap year, there arnt that many days in February\n");

    //else
    //{
    //int y,m,d,l = 0;
    //if (t.year>=2002
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    In C all variables *must* be declared at the beginning of a block. Anything else is an error:
    Code:
    printf("***************************\n"); 
    printf("*A DATE CALCULATOR PROGRAM*\n"); 
    printf("***************************\n\n"); 
    
    int days, leap; 
    struct dat t;
    should be
    Code:
    int days, leap; 
    struct dat t;
    
    printf("***************************\n"); 
    printf("*A DATE CALCULATOR PROGRAM*\n"); 
    printf("***************************\n\n");
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    cheers, but it compiles and builds this program on my compiler, its just that it doesnt recognise the correct leap years, ie it thinks 2001 was one and 2000 wasnt.?
    Brad.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    thanks prelude, ive solved it now , it was a mix up of terms...

    code:
    else if (t.month == 2 && t.day == 29&& leap != 0)

    printf("ERROR, the year you entered isnt a leapyear.\n");

    else if (t.month == 2 && t.day > 29 && leap == 0)

    printf("ERROR, although this is a leap year, there arnt that many days in February\n");

    should have been:
    else if (t.month == 2 && t.day == 29&& leap == 0)

    printf("ERROR, the year you entered isnt a leapyear.\n");

    else if (t.month == 2 && t.day > 29 && leap != 0)

    printf("ERROR, although this is a leap year, there arnt that many days in February\n");

    Brad.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > cheers, but it compiles and builds this program on my compiler
    Then fix it so that it compiles your code as 'C' and not as C++

    You're writing C++ code and you don't know it

    Or your posts are on the wrong board

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ideas, how do you get them? xD
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-22-2009, 02:53 AM
  2. cool ideas for a game
    By Shadow12345 in forum Game Programming
    Replies: 7
    Last Post: 05-18-2004, 08:37 PM
  3. Company Name Ideas
    By brunomiranda in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 12-16-2003, 04:15 PM
  4. idea's idea's idea's
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-29-2002, 12:30 AM
  5. Small app ideas
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-15-2002, 07:57 PM

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