Hello everybody,
Thanks for taking a minute to read this. Below is code that should enable the user to put in values. These values should then be stored into the vector. The vector should be read, and output should come out of it accordingly: highest value, lowest value, average value. My test-input is:
"76.5, 73.5,71.0,73.6,70.1,73.5,77.6, 85.3 88.5, 91.7, 95.9, 99.2, 98.2, 100.6, 106.3, 112.4,110.2, 103.6, 94.9, 91.7, 88.4, 85.2, 85.4, 87.7"
output is:
"Average temperature: 76.5
High temperature: 76.5
Low temperature: 76.5"
I suspect it has something to do with the program only using the first value and ignoring the rest. This might be because of 'cin', or maybe something is wrong with the loops?
If anyone has some idea's / suggestions i'd be most grateful.
Regards,
Minxx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include "std_lib_facilities.h"
vector <double> input{};
double Sum_Temp = 0;
double temps;
int main()
{
//input from temp sensor
while (cin >> temps)
{
input.push_back(temps);
for (auto& x : input) Sum_Temp += x;
{
//compute and print average temperature
cout << "Average temperature: " << Sum_Temp / input.size() << '\n';
}
for (auto&x : input)
{
//compute and print the highest temperature
cout << "High temperature: " << *max_element(input.begin(), input.end()) << '\n';
//compute and print the lowest temperature
cout << "Low temperature: " << *min_element(input.begin(), input.end()) << '\n';
//stop the loop
break;
}
}
keep_window_open();
}
|