Originally Posted by
lpaulgib
Is there a reason why I should declare the integer in the actual loop rather than outside the loop in the beginning? I thought it would be cleaner code to have all the integers declared in the beginning. Is there a functional reason to do it inside the loop? I had forgotten to take the x variable out just because it was being used, then i cleaned out that loop...
And also my indention and tabbing. Does it look clean? So far the only people that normally read the code i write is myself. And of course it looks good to me. Do you see any potential problems?
Personally, I think it is much cleaner code in a for loop to declare the iterator inside. That way you don't have to go searching for the declaration elsewhere in the code, and so you can use the same name if you need to for a different variable outside the loop.
As to your indentation and tabbing, I'm not particularly fond of it. I personally like to do something like this in a code block:
Code:
for (int i = 0; i < exampleNum; i++) {
//put 4 spaces starting from 'f' in 'for' before this line of code if it were code instead of a comment
//do stuff on this line...
//and do stuff on this line....
//etc..
}
That way its easier to read when you're scanning the code with your eyes. Its easier to pick out where the code block begins and ends. You crammed your for loop code all on the same columns, plus you put extra spaces in places where there shouldn't be any extra spaces, making it look like its a new code block or something, i.e.:
Code:
int familyfunct(int combinedages, int famtot){
cout<<"\nAbout to do the math.....\n\n\n";
int average = (combinedages/famtot);
cout<< average << endl << endl;
if(average>80){
cout<<"You're all old!" << endl;
}
if(average< 20){
cout<<"You're a young family!" << endl;
}
if(average< 30){
cout<<"You're getting older!" << endl;
}
else{
cout<<"You're pretty normal" << endl;
}
}
You should also be aware that both the middle if statements will execute if the average is less than 20, meaning the output would be:
You're a young family!
You're getting older!
I don't think that was intentional.
You should use "else if" instead of "if" in the second middle one.