The problem in my code I'm trying to fix is the last two for statements.My program always messes up the either the low/high value I check and display because they change for some reason. I'm fairly new to this so I'm not sure where I've messing up. The temp.txt values are irrelevant as long as there are 12 float values. Anyone see where I went wrong?
In C++, it is valid to define your variables anywhere in a function.
There are two schools of thought regarding this practice, and the common advice given by instructors is to place all of your variable definitions at the beginning of a function so that they may be easily located.
Others advocate defining your variables immediately before you first use them and only after all initialization data is available. It can be said that code written this way is easier to read because the definition and usage of a variable are close together. It is also sometimes claimed that this reduces the potential for introducing errors into your program by minimizing the chances that you will accidentally alter a variable's value between the lines where you initialize it and the lines that you use it.
I would relocate the definitions of highest and lowest like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
float highest = tempArray[0];
for (int i = 1; i < SIZE; i++)
{
if (tempArray[i] > highest)
highest = tempArray[i];
}
cout << "The highest temperature in farenheit is: " << highest << endl;
float lowest = tempArray[0];
for( int i = 1; i < SIZE; i++)
{
if (tempArray[i] < lowest)
lowest = tempArray[i];
}
cout << "The lowest temperature in farenheit is: " << lowest << endl;
You may also notice that I've changed the initial value of i in each for statement to skip over offset 0, since we don't need to compare the first value to itself.
As a compromise, you could leave your definitions where they are, and reinitialize them after you have read in your temperature data.