switch statement error

I am using a switch statement as a menu. If I enter a non-integer value as my choice, I expect the error message , "Invalid entry", to display but instead the output keeps running. Please help me fix this.
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
#include <iostream>
#include <cctype>

int read_int( const char* prompt, int minv, int maxv )
{
    int value ;
    char delimiter ;

    std::cout << prompt << " (integer in [" << minv << ',' << maxv << "])? " ;

    // try to read an int, immediately followed by a white space character
    if( std::cin >> value && std::cin.get(delimiter) &&  std::isspace(delimiter) )
    {
        // the user did enter an integer value
        // if value is in with range, return it
        if( value >= minv && value <= maxv ) return value ;

        // if not, inform the user of the error and try again
        std::cout << value << " is out of range\n" ;
    }

    else
    {
        // the user did not enter an integer value
        // inform the user of the error
        std::cout << "input is not an integer\n" ;

        // clean up
        std::cin.clear() ; // clear failed state if any
        std::cin.ignore( 1000, '\n' ) ; // throw away any junk left in the input buffer

        // and try again
    }

    return read_int( prompt, minv, maxv ) ; // try again
}

int main()
{
    const int n = read_int( "please enter a number", 1, 6 ) ;

    std::cout << "you entered the number " << n << '\n' ;
}
Thank you.
Topic archived. No new replies allowed.