loop help

This is a discussion on loop help within the C++ Programming forums, part of the General Programming Boards category; So I have to write a function to determine the number of days that have passed since 1/1/1753 and whatever ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    loop help

    So I have to write a function to determine the number of days that have passed since 1/1/1753 and whatever target year the user inputs. I've already included the algorithm to determine if a year is a leap year or not, but I'm still not sure how to write the loop part of my program.

    here's what I have so far: my isLeapYear function and main, which prompts the user for a target year.
    Code:
    bool isLeapYear()
    {
       int year;
       if (year % 400 == 0)
          return true;
       else if (year % 100 == 0)
          return false;
       else if (year % 4 ==0)
          return true;
       else
          return false;
    }
    
    int main()
    {
       int numDays;
       int year;
       cout << "Year: " << endl;
       cin >> year;
       cout << "Number of days; " << numDays;
       return numDays;
    }
    I don't expect the exact answers, but I do need help understanding how to write a loop and how to include a loop here.

  2. #2
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    wrote it out of my head

    Code:
    //directives here
    
    bool isLeapYear(int year)
    {
       if (year % 400 == 0)
          return true;
       else if (year % 100 == 0)
          return false;
       else if (year % 4 ==0)
          return true;
       else
          return false;
    }
    
    //main declaration and other code here
    int days = 0;
    for (; year >= 1973; year--)
    {
        (isLeapYear(year)) ? (days += 366) : (days += 365); //short version of if else statement
    }
    Last edited by codeprada; 02-10-2011 at 08:30 PM.
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    can you walk me through that please?

  4. #4
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    Quote Originally Posted by akahizzle View Post
    can you walk me through that please?
    hope this helps

    Code:
    #include <iostream>
    using namespace std;
    bool isLeapYear(int year)
    {
       //instead of declaring year in here 
       //you should just pass it as a parameter so that it checks that year
       //and returns the result
       if (year % 400 == 0)
          return true;
       else if (year % 100 == 0)
          return false;
       else if (year % 4 ==0)
          return true;
       else
          return false;
    }
    
    int countDays(int year)
    {
        //pass year here again.
        int days = 0; //this will hold the total amount of days 
        /*the fot loop below goes like this
        There is not first parameter because we are already passing year initialized with it's value
        The loop will run until year is not equal to or greater than 1973
        on each spin/cycle or execution of the for loop year is decremented by 1
        */
        for (; year >= 1973; year--)
        {
            //if the year is a leap year then 366 is added to the total of days
            //if not 365 is added
            (isLeapYear(year)) ? (days += 366) : (days += 365); //short version of if else statement
            /*
            OR
            if (isLeapYear(year) == true)
                days = days + 366; //or days += 366
            else
                days = days + 365; //or days += 365
            */
        }
        return days //return the amount of days to main
    }
    int main()
    {
       int numDays;
       int year;
       cout << "Year: " << endl;
       cin >> year;
       numDays = countDays(year); //function call to calculate days
       cout << "Number of days; " << numDays;
       return 0; //always return 0 at the end of your main
    }
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 05:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 10:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2009, 12:54 AM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

Tags for this Thread


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