c++ input exception handling

Hey everyone, I’m expecting a double value from the user

a and d are double values while the c is a char value


    cin >> a >> c >> b;
      cout << "enter" << endl;

if the user accidentally enters a character or anything(that is not a double or an integer) this method should ask for the input again but what it is doing is keep printing the string “enter”. Any suggestion how can I fix this. Thanks

I actually looked up to it, but couldnt figure anything out.

Using cin.good and cin.bad , I am clueless about them honestly, since I never had to use them anywhere.

But checking if user entered an integer or not can be done. You can take input as a string, check if ASCII values lie in range of [48,57] (i.e. range of [‘0’,‘9’]). Pay special attention for double, they have a decimal “.” . If its correct, then convert it into double/int and store it (Either manually via loop or inbuilt function if any)

1 Like

@vijju123 please answer bro

I don’t have to use cin.good I just want something that can ask input again if the given input is not double or integer

You can use infinite loop then. And if the input we got is proper, we can break out of it.