exception not working

Well it sort of works but only with integers... The problem is that as I soon as I put a character or string of sort as a response it goes haywire and loops infinitely.... I guess it is that i'm not sure how I would make it detect a character has been detected... Oh and If you put to big of a number as well it crashes as well and loops forever...

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

using namespace std;

int main()
{
    int number;
    int response;
    
    cout << "please enter a number from 1-8" << endl;
    while (true)
    {
        try {
            cin >> number;
            if ((cin) && (number > 2) & (number < 8)){
                break;
            }
            else
                throw 1;
            
        }
    
        catch (int x)
        {
            cout << "Nope dude" << endl;
        }
    
    }
    cout << "Your choice was: " << number;
    cin >> response;
    return 0;
}
Hi,

1
2

if ( (cin) && (number >= 1) && (number <= 8) )


The infinite while loop seems unnecessary, you don't store the result anywhere.

The problem with a char, is that it is still just a small int, have a read about static_assert , assert , or investigate stringstreams.

Hope all goes well :+)
Maybe but I just want them to have to input a number ya know? That is why I put it on a infinite loop so that they have to in order to break free.
Maybe but I just want them to have to input a number ya know? That is why I put it on a infinite loop so that they have to in order to break free.


But if you throw , they won't get another opportunity. C++ exceptions have termination semantics.

stringstream are a better way to handle this.

If you want to investigate exceptions, then try a different example like Latitude and Longitude. Throw if either of these is out of bounds. Negative Latitude means South, while negative Longitude means West,or if you want use two enum for N , S, and E , W and keep the numbers positive.
nooo there has to be a way ive been trying this for a whole day now D:
按照意思你应该:if ((cin) && (number >= 1) && (number <= 8)){

至于为何有死循环,因为输入了字母或者大于int值范围。

解决:获得字符串行,再转化为数字,然后进行你的判断和下面的程序。
Why don't you just continue in the else part avoid using exceptions altogether (a good thing if you can).

By testing cin you have avoided all the other errors, except the input being a char. A char would normally fall outside the range of 1 to 8 (on the keyboard ), unless you did manage to somehow send it one in that range. The earlier problems were due to you using a bitwise & instead of boolean &&

One example of where exceptions can be more useful in a class constructor, in the case of an invariant not being met.

More typical usage of exceptions involves #include <exception> , so one can use all the built in exceptions, and derive you own ones.

So one needs to be careful to use exceptions appropriately, some avoid them altogether.
It makes no sense to throw and catch within the same place. why not line 25 -> 19?

You problem is that cin goes into an error state if you do not enter a number. This state remains (and thus number is not changed anymore). In this case you need to call clear() and ignore():

http://www.cplusplus.com/reference/istream/istream/ignore/
http://www.cplusplus.com/reference/ios/ios/clear/

http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input

Note cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');:

http://www.cplusplus.com/reference/limits/numeric_limits/?kw=numeric_limits
Topic archived. No new replies allowed.