calculating the average in a multiset

Hi everyone!
I'm trying to search a multiset for a two values, then calculate the average of the range. When I use multiset.find(e)and accumulate it doesn't include the value e that I entered, it only calculates the average of the elements before e.
I'll appreciate if you help understand the issue. Thank you in advance.
Here is the 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
#include <iostream>
#include <algorithm>
#include <numeric>
#include <set>
#include <fstream>
#include <iterator>

    int main()
    {
    ifstream input ("data.txt");
    multiset<int> values;
    multiset<int>::iterator it1;
    multiset<int>::iterator it2;
    int currValue;
    int b, e;
    while (input >> currValue){
        values.insert(currValue);
    }
    cout << "Enter the beginning range: ";
    cin >> b;
    cout << "Enter the end of the range: ";
    cin >> e;
    it1 = values.find(b);
    it2 = values.find(e);

    if ( it1 != it2){
        cout << "The values in order are: " ; copy(it1,it2,ostream_iterator<int>(cout, " ")); cout <<endl;
        cout << "The average: " << accumulate(it1, it2, 0) /(distance(it1, it2));
    }
    else{
        cout << "The range doesn't exist. " << endl;
    }
   return 0;
}


data.txt
32
25
65
34
82
75
100
80
78
Thank you keskiverto ;)
It works fine but when I test the values that aren't in the multiset, I get either a random result or an infinite loop. Do you have any idea why?
Thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <set>
#include <iostream>
#include <algorithm>
#include <cmath>

double average( const std::multiset<int>& ms, int a, int b )
{
    if( a > b ) std::swap(a,b) ;

    const auto begin = ms.lower_bound(a) ;
    const auto end = ms.upper_bound(b) ;

    return begin == end ? std::nan("1") :
                          std::accumulate( begin, end, 0.0 ) / std::distance(begin,end) ;
}

int main()
{
    std::cout << average( { 5, 8, 3, 8, 5, 3, 2, 6, 8, 3, 1, 3, 5, 6, 5, 3 }, 4, 7 ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/f3fbfecb1336605b
Thank you JLBorges and keskiverto :) You were very helpful.
Topic archived. No new replies allowed.