So I rewrote it based on what you guys said, this is what I got. I think it's correct except I am getting a warning "Variable 'num' is uninitialized when used here" based on the "num" i colored blue. Xcode tries to fix this by changing where I define num to a line saying double num = 0.0 . Is that correct/something I should do? Or should I just ignore that (and if I am supposed to do that why exactly is it needed?) Note the program works correctly without this change.
It is not quite right yet, but you are very close.
1) The issue with your variable num is that it is not initialized before you use it when you pass it to the function larger. In simpler terms, you did not explicitly specify its value after declaring the variable. Sometimes when you do this the variable is given a default value, but sometimes it can be a random number, it depends on the object you are declaring. It is good practice to always explicitly assign the value of the variable at declaration when possible. When not possible, you need to make sure you explicitly give it a value before you use it. This makes it clear what the initial value is and can avoid debugging headaches if your compiler is not as forgiving as the one you are using in Xcode. You did not see this problem with max because you assigned it a value by reading from input using the line cin >> max;
2) Your function larger should not be reading from input to assign the value of num, that defeats the purpose of passing the value of num as one of the function parameters. Take out the cin and cout lines from your larger function and move them inside your for loop in main, just before your call to larger. This also helps to keep your function larger more general and modular.
3) Don't forget to return 0 at the end of main to indicate the program exited successfully. I over looked this too until lloyddean's post. His method for return is nice and is very clear to the reader, but both methods are common.
4) You should consider moving your comments to the line above the code you are commenting instead of the same line. Professionally, it is still common for software teams to have a policy of limited line width and inline comments like that just waste useful space. Placing it above maintains the useable width. Also, it is a little bit easier to read the comments above the code.
5) You should also consider using brackets with your if statements even if they are one line. Many times you will find that when you write an if statement you will come back to it later and add more code. Also, it is easier to read and visually recognize which code is included in the if statement when you use brackets.
If you are interested in good guidelines for styling, then you could take a look at the Google C++ style guide,
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml. This is not the final word on styling, but having some consistent method that is commonly used can be very helpful. Professionally, you will come across people and teams that use different styling methods. The most important thing is that you are consistent and try to make your code as readable as possible.
You almost have everything right, so here is your code with my suggestions:
Code:
#include <iostream>
using namespace std;
// Use function larger
double larger(double max, double num)
{
// Compare new numbers to saved max
if ( num > max )
{
// If new number is larger than previous max, it will become the new max
max = num;
}
return max;
}
int main()
{
// Define max used to describe max integer
double max;
// Define number to describe each inputted number being compared
double num;
// Call for first number
cout << "Enter a number: ";
// Consider this number max as it is the only number at this point
cin >> max;
// Use for loop expression to repeat following step 14 times
for (int counter = 2; counter <= 15; counter++)
{
// Call for new numbers
cout << "Enter another number: ";
// Consider these new numbers num
cin >> num;
// Call function larger()
max = larger(max , num);
}
// Report max number from series
cout << "The max number is " << max << endl;
return 0;
}
Note that I did not initialize max and num at declaration, but they are both initialized using cin >> before they are used.