program doesnt process all vector values

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();

}
That's strange, it works for me funnily enough, did you forget to save and compile?

I also cleaned it up a bit:

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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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; // This was correct
		// You didn't need the extra codeblock here
		cout << "Average temperature: " << Sum_Temp / input.size() << '\n';
		Sum_Temp = 0; // You needed to reset the sum back to 0, otherwise averages will keep adding

		// You didn't need the for loop here too
		//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';
	}
}
I rebuild in visual studio before I press 'Debug" and give the program a test. I also save regularly, as far as I can tell this doesn't a difference. I tried to copy and paste your code but even then it does exactly the same as my code: only takes out the first value (76.5).

Are you a 100% positive my code was correct?
This is because your input is comma-separated, not space separated. So you first extract 76.5, you print average, high, and low (all 76.5). Then you try to extract the next double in the condition of the loop, but this time, you extract a ',', which is not a valid double, so the input stream goes into a failed state, and the condition of the loop evaluates to false, so you exit the while loop and your program terminates.
No, your code is not correct.

The reason it only reads the first value is because of the commas. You need to read them separately.

Also, the whole design of the program looks wrong. I assume you want to print the average, max and min just once at the end of the program. The way you have it they will print over and over again with each input value.

So the input could look something like:
1
2
3
4
5
    while (cin >> temps) {
        input.push_back(temps);
        char comma;
        cin >> comma; // eat the comma
    }


And your stats output would go after that, although you could accumulate the sum in the input loop, if you want (but don't print anything there).
My apologies both of these guys are correct. I completely missed you had comma separated numbers. I was using space separation so your code was running like a charm. My own bad.

Thanks you all for the replies. I tinkered with it all day today, however I have been unsuccesfull.
I have now tried using input without comma's, for example: "3 4 5 6"
It then loops four times, with the last answer being the correct answer.

Here is my latest code.

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
38
39
40
41
42
#include "std_lib_facilities.h"


vector <double> input{};
double Sum_Temp;
double temps;
double Low;
double High;
double Average;

int main()
{
	while (cin >> temps)
	{
		input.push_back(temps);


		for (auto& x : input) Sum_Temp += x;
		Average = Sum_Temp / input.size();
		Sum_Temp = 0;

		//compute the highest temperature
		High = *(max_element(input.begin(), input.end()));

		//compute the lowest temperature
		Low = *(min_element(input.begin(), input.end()));



		//print the highest temperature
		cout << "High temperature: " << High << '\n';

		//print the lowest temperature
		cout << "Low temperature: " << Low << '\n';

		//print average temperature
		cout << "Average temperature: " << Average << '\n';

	}
			
	keep_window_open();
}
So what do you want, only the last loop to be displayed?

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
38
#include "std_lib_facilities.h"


vector <double> input{};
double Sum_Temp;
double temps;
double Low;
double High;
double Average;

int main()
{
	while (cin >> temps)
	{
		input.push_back(temps);
	}
	// As tbp suggested move them out of the input loop

	for (auto& x : input) Sum_Temp += x;
	Average = Sum_Temp / input.size();
	Sum_Temp = 0;

	//compute the highest temperature
	High = *(max_element(input.begin(), input.end()));

	//compute the lowest temperature
	Low = *(min_element(input.begin(), input.end()));

	cout << "High temperature: " << High << '\n';

	//print the lowest temperature
	cout << "Low temperature: " << Low << '\n';

	//print average temperature
	cout << "Average temperature: " << Average << '\n';
	
	keep_window_open();
}
Last edited on
Yes, I need only the last (correct) loop to be displayed. I tried moving the code out of the input loop, but as soon as i do that, the program takes input, but does nothing with it.
Topic archived. No new replies allowed.