simple function question

This is a discussion on simple function question within the C++ Programming forums, part of the General Programming Boards category; I wrote a function that tells you how many days you've lived so far. I can't help having the feeling ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    simple function question

    I wrote a function that tells you how many days you've lived so far. I can't help having the feeling that this code could be cleaner. Here's what i've done:

    Code:
    #include <iostream>
    using namespace std;
    
    int doIt(int yearBorn)
    {
    
    int currentYear = 2010;
    int daysInAYear = 365;
    
    int yearCount = yearBorn;
    int differenceOfYears;
    int result;
    
    int x;
    
    for(x=0;yearCount != currentYear; x++,yearCount++)
    {
    differenceOfYears = x + 1;
    }
    
    result = differenceOfYears * daysInAYear;
    
    return result;
    
    }
    
    int main()
    {
    
    cout<<"You've lived "<<doIt(1990)<<" days.";
    
    cin.get();
    
    }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    You don't need a for-loop here. Taking the difference between two numbers is a single operation (minus). Once you remove the loop, your code will shrink to about 3 lines, the two definitions for your magic numbers and a one liner for calculating the result.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    That code is a bit messy. Here's how I would do it.

    Code:
    #include <iostream>
    using namespace std;
    
    int doIt(int yearBorn) {
         int currentYear = 2010;
         int daysInAYear = 365;
         int yearCount = yearBorn;
         int differenceOfYears;
         int result;
         int x;
         for(x=0; yearCount != currentYear; x++, yearCount++)
              differenceOfYears = x + 1;
         result = differenceOfYears * daysInAYear;
         return result;
    }
    
    int main() {
         int x;
         cout << "What year where you born? ";
         cin >> x;
         cout << "You've lived " << doIt(x) << " days." << endl;
         cin.get();
         return 0;
    }
    If main terminates successfully, return 0.
    Last edited by Babkockdood; 08-04-2010 at 04:23 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Babkockdood's example does not implement what nvoigt suggested, so I guess that is left to you to do

    However, it does illustrate that you should indent your code properly. Furthermore, I suggest that you rename doIt to something more sensible, e.g., computeDaysFromYear. It is also good to see that you named your constants, but then you should also declare them const, possibly static const.

    Quote Originally Posted by Babkockdood
    If main terminates successfully, return 0.
    An explicit return statement is unnecessary in C++ with respect to the global main function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Well, as I said, don't loop. If you have 9 apples and lose 3, do you really go "I got 9 left. No, 8 left. No, 7 left. No, 6 left. Eureka!". That's not the way mathematics work. 9 - 3 is 6. No loop here.

    The number of days you lived is the current year, minus the year of your birth, multiplied by the number of days in a year. Try to put that into code. It will be much cleaner.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function name basic question help
    By kenryuakuma in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 07:48 AM
  2. A simple question of a C++ function
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2007, 06:49 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. multidimensional array function - simple question
    By boltz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2005, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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