Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

What's wrong with this C++ code (very small)

  • 02-03-2006 09:22PM
    #1
    Closed Accounts Posts: 1,299 ✭✭✭


    I want to calculate the total number of days present between a certain date and the year 1900.
    I have already allowed for leap years in my year_lenght function

    int totaldaysforyears (int year){

    int yearlength = 0;
    while (year!=1900){
    yearlength = yearlength + year_length(year);
    year--;
    }
    return yearlength;
    }


Comments

  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    I changed it to this, but no improvement, do I have to include Ampersants ? (&)?
    [SIZE="1"]#include <iostream>
    using namespace std;
    bool leap_year (int year);
    int month_length (int month,int year);
    int year_length (int year);
    int daysperyears(int year);
    int dayspermonths(int month);
    
    int main () {
    int month,year;
    cout << "Enter year " ;
    cin >> year;
    cout << "Enter month ";
    cin >> month;
    cout <<"Number of days in those years is " <<daysperyears (year)<<endl;
    
    cout <<month_length(month,year);
    system ("pause");
        return 0;
    }
    
    
    
    
    
    int month_length (int month, int year) {
      int m = month;
        if (m==2); 
          if (leap_year(year)) return 29;
            else 
            return 28;
        else if{ (m==9 || m==4 || m==6 || m==11)
             }
                return 30;
            else return 31;
    }
    
    
    bool leap_year (int year) {
         int y =year;
    //returns true if year is a leap year
        if (y % 4 == 0) //all others are definitely not leap
            if (y < 1583) return true;
            else //after 1583
                if ((y % 100 == 0) && (y % 400 != 0))
                    //e.g. 1900
                    return false;
                else //e.g. 1904 or 2000
                    return true;
        else //not divisible by 4
            return false;
    }
    
    int year_length (int year) {
        int y = year;
        if (leap_year (y)) 
            return 366;
         else return 365;
        }
        
        
        int daysperyears(int year){
        int y;
        int c=year;
            while (year>1900){
             y = y + year_length(year);
              c--;
              }
        
    return y;
       
    }
    
    int dayspermonths(int month){
        int y ;
        int m = month;
            while (m>0){
             y = y + month_length(m);
              m--;
              }
        
    return y;
       
    }
    [/SIZE]
    

    It says to few arguments to function int month_length?


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    put your code in [ CODE][ /CODE] tags and tell us exactly what the error is.


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    done.


  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    int month_length (int month,int year);
    y = y + month_length(m);
    It says to few arguments to function int month_length?
    ...


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    Ok I am finished now but the final and ultimate calculation dayofyear(day,month,year)

    is continuosly coming back with the wrong value, can you see why?
    #include <iostream>
    using namespace std;
    
    
    bool leap_year (int year);
    int year_length (int year);
    int daysperyears(int year);
    int dayspermonths(int month,int year);
    int month_length (int month, int year);
    int dayofyear (int& day, int& month, int& year);
    int main () {
        int day, year,month;
    
    cout << "Enter year " ;
    cin >> year;
    cout << "Enter month ";
    cin >> month;
    cout << "Enter day ";
    cin >> day;
    cout <<"Number of days in those years is " <<daysperyears (year)<<endl;
    cout <<"Number of days in those months is " <<dayspermonths (month,year)<<endl;
    cout <<"days is "<<day<<endl;
    cout << "Total number of days is " << dayofyear ( day,  month,  year);
    
    system ("pause");
        return 0;
    }
    
    int daysperyears(int year){
        int y;
            while (year>1900){
             y = y + year_length(year);
              year--;
              }
        
    return y;
       
    }
    
    int year_length (int year) {
        if (leap_year (year)) 
            return 366;
         else return 365;
        }
        
    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))
                    //e.g. 1900
                    return false;
                else //e.g. 1904 or 2000
                    return true;
        else //not divisible by 4
            return false;
    }
    
    
    
    
    
    
    int month_length (int month, int year) {
        if (month==2) 
          if (leap_year(year)) return 29;
            else return 28;
        else if (month==9 || month==4 || month==6 || month==11)
              
                return 30;
            else return 31;
    }
    
    
    int dayspermonths(int month,int year){
        int y;
            while (month>0){
             y = y + month_length(month,year);
              month--;
              }
        
    return y;
       
    }
    
    
    int dayofyear (int& day, int& month, int& year){
        
        int y;
        y = (day+ dayspermonths(month,year) + daysperyears(year));
    return y;
    }
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 919 ✭✭✭timeout


    Surly you don't need the &? I mean you have not needed anywhere else in the code.


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    I just put it in, i don't undertsand it, but it didn't seem to make a difference in or out!


  • Registered Users, Registered Users 2 Posts: 919 ✭✭✭timeout


    try seperating it out and printing out so you can see whats going on.
    int dayofyear (int day, int month, int year){
        
        int y,tempD,tempY;
        cout<<month <<" - "<<year;
        tempD = dayspermonths(month,year);
        cout<<"per month - " <<tempD;
        tempY = daysperyears(year);
        cout<<"per year - " <<tempY;
        y = (day+ tempM + tempY);
        cout<<"total - " <<y;
    
    return y;
    
    

    [Edit]Please note I don't have c compilier so have not tested your code or the above alteration


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    ok the days and month calculate but the daysperyears function wont calculate inside the dayofyear function.


  • Registered Users, Registered Users 2 Posts: 919 ✭✭✭timeout


    But it does work the first time throught it? at this point
    cout <<"Number of days in those years is " <<daysperyears (year)<<endl;


  • Advertisement
  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    yes it definitely works there.

    Thanks for your help.


  • Registered Users, Registered Users 2 Posts: 919 ✭✭✭timeout


    And the year value is correct in the above altered code?
    I.E.
    2 - 1905
    per month - 52
    per year -
    Well stick in a bit of test code again to see the output:
    int daysperyears(int year){
        int y=0;
            while (year>1900){
              cout<<"in daysperyears - " <<year;
              y = y + year_length(year);
              cout<<"in daysperyears Y value - " <<y;
              year--;
              }
        
    return y;
    
    [edit] forgetting my ;


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    Now I'm getting the following errors;
    91 C:\Documents and Settings\Administrator\My Documents\bobo\Untitled6.cpp call of overloaded `dayspermonths(int&, int&)' is ambiguous

    91 C:\Documents and Settings\Administrator\My Documents\bobo\Untitled6.cpp call of overloaded `dayspermonths(int&, int&)' is ambiguous

    91 C:\Documents and Settings\Administrator\My Documents\bobo\Untitled6.cpp call of overloaded `dayspermonths(int&, int&)' is ambiguous
    #include <iostream>
    using namespace std;
    
    
    bool leap_year (int year);
    int year_length (int year);
    int daysperyears(int year);
    int dayspermonths(int month,int year);
    int month_length (int month, int year);
    int dayofyear (int day, int month, int year);
    int main () {
        int day, month,year;
    
      cout << "Enter a date to be converted to a number:";
      cin >> day >>month>>year;
    
    
    
    cout <<"Number of days in those years is " <<daysperyears (year)<<endl;
    cout <<"Number of days in those months is " <<dayspermonths (month,year);
    cout << "Total number of days is " << dayofyear ( day,  month,  year);
    system ("pause");
        return 0;
    }
    
    
    int daysperyears(int year){
        int y=0;
            while (year>1900){
              cout<<"in daysperyears - " <<year;
              y = y + year_length(year);
              cout<<"in daysperyears Y value - " <<y;
              year--;
              }
        
    return y;
    }
    int year_length (int year) {
        if (leap_year (year)) 
            return 366;
         else return 365;
        }
        
    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))
                    //e.g. 1900
                    return false;
                else //e.g. 1904 or 2000
                    return true;
        else //not divisible by 4
            return false;
    }
    
    
    
    
    
    
    int month_length (int month, int year) {
        if (month==2) 
          if (leap_year(year)) return 29;
            else return 28;
        else if (month==9 || month==4 || month==6 || month==11)
              
                return 30;
            else return 31;
    }
    
    
    int dayspermonths(int& month,int& year){
        int y;
            while (month>0){
             y = y + month_length(month,year);
              month--;
              }
        
    return y;
       
    }
    
    
    int dayofyear (int day, int month, int year){
        
        int y,tempD,tempY;
        cout<<month <<" - "<<year;
        tempD = dayspermonths(month,year);
        cout<< "per month - " <<tempD;
        tempY = daysperyears(year);
        cout<<"per year - " <<tempY;
        y = (day+ tempD + tempY);
        cout<<"total - " <<y;
    
    return y;
    
    }
    


  • Registered Users, Registered Users 2 Posts: 21 suavek


    you get those errors because you have declared two different functions sharing the same name: int dayofyear (int day, int month, int year) and int dayofyear (int& day, int& month, int& year). I mean that's the way your compiler sees things.

    [EDIT] Shame on me, I pasted the wrong fragment of code :p It's the dayspermonths function. Timeout got it right.


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    I don't quiet get you, which one should I change?


  • Registered Users, Registered Users 2 Posts: 919 ✭✭✭timeout


    Its the dayspermonth, you brought back in the &. There is no need for it so leave it out.

    [edit]i download and installed a compiler and that code runs fine with the & removed,also added <<endl for readability, man this brings back memories :D What dates you giving it?


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    Ok cool thank you all so much i have it now, ji also had to change a bit in the month length function to make it >1 rather than 0.


Advertisement