1. ## 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. 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.

3. 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.

4. 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.

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.

5. 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.