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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C++ program

  • 04-02-2008 7:58pm
    #1
    Registered Users, Registered Users 2 Posts: 929 ✭✭✭


    ok, im only starting to learn c++, and am having trouble writing code for my most recent program. That is, you have to enter a number like 25000 (which by the rules of the programme should be 25000 days from the 1/1/1900). I was able to do the oppisite of that i.e. enter in the date and then let the program compute the day number. this is the code for that programme:

    #include <iostream>
    using namespace std;

    int daynumber (int day, int month, int year);

    int year_length (int year);

    int month_length (int m, int y) ;

    bool leap_year (int year);

    int main () {
    int day, month, year;
    cout << "Enter a date:";
    cin >> day >> month >> year;
    cout << day << "/" << month << "/" << year;
    cout << " is day number ";
    cout << daynumber(day, month, year) << endl;
    system("pause");
    return 0;
    }

    int daynumber (int day, int month, int year) {
    int y, m, daynumber;
    daynumber = 0;
    for (y = 1900; y < year; y++)
    daynumber = daynumber + year_length (y);
    for (m = 1; m < month; m++)
    daynumber = daynumber + month_length(m, year);
    daynumber = daynumber + day;
    return daynumber;
    }

    int year_length (int year) {
    if (leap_year (year))
    return 366;
    else return 365;
    }

    int month_length (int m, int y) {
    if (m==2)
    if (leap_year(y)) return 29;
    else return 28;
    else if (m==9 || m==4 || m==6 || m==11)
    //sept, april, june or november
    return 30;
    else return 31;
    }

    bool leap_year (int year) {
    //returns true if year is a leap year
    if (year % 4 == 0) //all others are definitely not leap
    if (year < 1583) return true;
    else //after 1583
    if ((year % 100 == 0) && (year % 400 != 0))

    return false;
    else
    return true;
    else
    return false;

    }

    but i need to do the oppisite of that and am having trouble figuring it out. any help would be appreciated :)


Comments

  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    Well, the brute force method would work...!

    Ie: repeated subtraction.

    while (daysleft > year_length(y))
    {
    y++;
    daysleft -= year_length(y);
    }

    etc.. Then on to months and days....

    PS:
    Don't do the if else style you've done on month_length. One day you'll get bitten by:
    if (something)
       if (one)
           do action
    else
       do another
    

    It looks like the else goes with the (something) test, but it doesn't, it goes with the (one) test... Else goes with the last elseless if.

    Do this (style wars aside)
    if (something)
    {
       if (one)
           do action
    }
    else
       do another
    



    Conor.


  • Registered Users, Registered Users 2 Posts: 929 ✭✭✭sternn


    thanks, hopefully il get it to work, iv been trying to get it for quite a while now!
    im just slightly confused as to how to deal with the dayNumber...


  • Registered Users, Registered Users 2 Posts: 21,611 ✭✭✭✭Sam Vimes


    carveone wrote: »
    PS:
    Don't do the if else style you've done on month_length. One day you'll get bitten by:
    if (something)
       if (one)
           do action
    else
       do another
    

    It looks like the else goes with the (something) test, but it doesn't, it goes with the (one) test... Else goes with the last elseless if.
    [/code]

    i got some good advice when i started c++. some people will tell you you don't have to use braces on if statements. ignore them. braces make the code easier to read and debug


Advertisement