Thread: simple function question

  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,130
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,130
    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, 02: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