simple code, simple question

Here is a piece of premade code i wanted to change to get familiar with vectors.

Its about computing the average of numbers the user typed into the programm.
But it doesnt work. I think there is an logical error somewhere when adding data to the salaries vector. I dont know. Maybe you could have a look?

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
43
44
45
#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<double> between(vector<double> values,
    double low, double high)
{
   vector<double> result;
   for (int i = 0; i < values.size(); i++)
      if (low <= values[i] && values[i] <= high)
         result.push_back(values[i]);
   return result;
}

int main()
{
	bool eingabe = true;
	vector<double> salaries;

	while (eingabe) {
		cout << "Please type in a number:";
		double input;
		cin >> input;
		salaries.push_back(input);
		cout << "Do you want to add another number? (y/n)";
		string weitere;
		cin >> weitere;
		if (weitere == "y") {
			eingabe = true;
		}
		else {
			eingabe = false;
		}
	}

   vector<double> midrange_salaries
      = between(salaries, 45000.0, 65000.0);

   for (int i = 0; i < midrange_salaries.size(); i++)
      cout << midrange_salaries[i] << "\n";

   return 0;
}
all is well but where are you calculating the average. your between function will give all the numbers between 45k to 65k. now in your last for loop you need to add all the salaries and divide it by the size of midrange_salaries.
Topic archived. No new replies allowed.