Can somebody test this code please

This is a discussion on Can somebody test this code please within the C Programming forums, part of the General Programming Boards category; /*Compiler playing up using turbo c++ 4*/ #include <stdio.h> int isleapyear(int year) { if(year%4 == 0 && year % 100 ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    13

    Can somebody test this code please

    /*Compiler playing up using turbo c++ 4*/

    #include <stdio.h>

    int isleapyear(int year)
    {
    if(year%4 == 0 && year % 100 != 0 || year % 400 == 0)
    {
    return 1;
    }
    else
    return 0;

    }

    int isvaliddate(int day,int month,int year)
    {
    int maxnumdays;

    if(day<0) return 0;

    switch(month)
    {
    case 2:
    maxnumdays=28;
    if(isleapyear(year)) maxnumdays++;
    break;
    case 4:
    case 6:
    case 9:
    case 11:
    maxnumdays=30;
    break;
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
    maxnumdays=31;
    break;
    default:
    return 0;
    }

    if(day>maxnumdays) return 0;
    return 1;
    }

    int _tmain(int argc, tchar* argv[], tchar* envp[])
    {
    int day,month,year;
    int result;

    if(argc!=2)
    {
    printf("Usage: %s DD:MM:YYYY\n",argv[0]);
    return 1;
    }

    if(sscanf(argv[1],"%02d:%02d:%04d",&day,&month,&year)!=3)
    {
    printf("Bad input. Must be DD:MM:YYYY\n");
    return 1;
    }

    if((year<1800)||(year>1999))
    {
    printf("Year must be in range [1800;1999]\n");
    return 1;
    }

    if(isvaliddate(day,month,year)==0)
    {
    printf("%d:%d:%d is not a valid date\n",day,month,year);
    return 1;
    }

    result=year%100; // 1. Take the last two digits of the year.
    result*=5; // 2. Add a quarter of this, neglecting any remainder.
    result/=4;
    result+=day; // 3. Add the day of the month.

    switch(month)
    {
    case 1: // For January add 1 (if leapyear add 0)
    if(isleapyear(year)==0) result++;
    break;
    case 2: // febuary add 4 (if leapyear add 3)
    result+=4;
    if(isleapyear(year)) result--;
    break;
    case 9: // september add 6
    case 12: // december add 6
    result++;
    case 6: // june add 5
    result++;
    case 3: // march add 4
    case 11: // november add 4
    result++;
    case 8: // august add 3
    result++;
    case 5: // may add 2
    result++;
    case 10: // october add 1
    result++;
    // default:
    // case 4: // april add 0
    // case 7: // july add 0
    }

    if(year<1900) result+=2; // 5. For 1800 - 1899 add 2


    printf("%d:%d:%d is a ",day,month,year);

    switch(result%7) // 6. Divide the result by 7 and the remainder gives the day of the week:
    {
    case 0:
    printf("Saturday"); // 0 = saturday.
    break;
    case 1:
    printf("Sunday"); // 1 = sunday
    break;
    case 2:
    printf("Monday"); // 2 = monday
    break;
    case 3:
    printf("Tuesday"); // 3 = tuesday
    break;
    case 4:
    printf("Wednesday"); // 4 = wednesday
    break;
    case 5:
    printf("Thursday"); // 5 = thursday
    break;
    case 6:
    printf("Friday"); // 6 = friday
    }

    printf("day\n");
    return 0;
    }
    Last edited by andy bee; 10-09-2001 at 01:42 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    28
    I think the test for a leap year should be more like

    if(year%4 == 0 && year % 100 != 0 || year % 400 == 0)
    {
    return 1;
    }
    else
    return 0;


    as the year being tested needs to match more than 1 of the criteria for an identification to be made
    Last edited by SPOOK; 10-09-2001 at 12:48 PM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    printf("Usage: %s DD:MM:YYYY\n",argv[0]);

    You missed of the string to print.

    Do you have some input which you think doesn't work?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    13
    I have changed program above to hold suggestions.
    Same error comes up as below :-

    int _tmain(int argc, tchar* argv[], tchar* envp[])

    error is:- line 49 : ) expected.

    only shows this not much wrong i think.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    28

    code

    could you edit your post using code tags to make the logic easier to follow as the lack of indentation makes your intentions unclear, also could you write a sentance to explain what you want the program to do

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    13

    Re: code

    Originally posted by SPOOK
    could you edit your post using code tags to make the logic easier to follow as the lack of indentation makes your intentions unclear, also could you write a sentance to explain what you want the program to do

    The following alogrithm can be used to find the day of the week for any date between 1st jan 1800 and 31st dec 1999.
    1. Take the last two digits of the year.
    2. Add a quarter of this, neglecting any remainder.
    3. Add the day of the month.
    4. For January add 1 (if leapyear add 0)
    febuary add 4 (if leapyear add 3)
    march add 4
    april add 0
    may add 2
    june add 5
    july add 0
    august add 3
    september add 6
    october add 1
    november add 4
    december add 6
    5. For 1800 - 1899 add 2
    1900 onwards add 0
    6. Divide the result by 7 and the remainder gives the day of the week:
    1 = sunday, 2 = monday, 3 = tuesday, 4 = wednesday, 5 = thursday, 6 = friday and 0 = saturday.

    For example :- 27TH AUGUST 1815 IS ;
    15 + 3 + 27 + 3 + 2 = 50
    50 / 7 = 7 remainder 1 = sunday.

    I urgently need to write a program that will accept the date , using the format of DD:MM:YYYY. Then using the above method (only) calculate which day of week it falls on and display this day.
    You can assume that the date input is valid.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    28
    andy bee

    check your message inbox

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please test my code!!!
    By MK27 in forum Linux Programming
    Replies: 3
    Last Post: 07-09-2008, 10:41 PM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  3. test code
    By egomaster69 in forum C Programming
    Replies: 3
    Last Post: 12-20-2004, 06:04 PM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

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