This is syntactically invalid:
You probably wanted to write a while loop.
This is syntactically valid but misleading:
Code:
for (double x : temps) sum += x;
{
cout << "Average temperature: " << sum/temps.size() << '\n';
// compute median temperature:
sort(temps.begin(), temps.end()); // sort temperatures
if (temps[temp] % 2 == 0)
{
cout << "Median temperature: " << temps[temps.size()/2] << '\n';
}
else
{
cout << "Median temperature: " << temps[(temps.size() + 1) / 2] << "\n";
}
}
In more conventional formatting, it is equivalent to:
Code:
for (double x : temps)
{
sum += x;
}
{
cout << "Average temperature: " << sum/temps.size() << '\n';
// compute median temperature:
sort(temps.begin(), temps.end()); // sort temperatures
if (temps[temp] % 2 == 0)
{
cout << "Median temperature: " << temps[temps.size()/2] << '\n';
}
else
{
cout << "Median temperature: " << temps[(temps.size() + 1) / 2] << "\n";
}
}
You probably don't need the extra code block as that block can be placed directly in the main function's scope. If you do want another code block, then you might as well move it to a separate function as it is a reasonably good candidate to be another function.
This is problematic: temps[temp] % 2. temps[temp] is a double, but operator% is not defined for double operands. You probably wanted to write temps.size() % 2 instead.