How do i tell the complier what to do in a certain case of exception.

I would like you guys, if you reply, not to tell me the answer directly just like that. Make it tricky so i can understand something or put me do some mindmapping instead.

Here's the code:

#include <iostream>
using namespace std;

int main()
{
int a;
cin >> a;

do
{
if(a = 0)
{
return 0;
}
}while(a < INT_MAX || a > INT_MIN);
}
// if i input "a" as char ( ex: fdsafdsaj21312) program will close or hang.

I would like to know how to handle this, what is a handler or exception and how and what i do with those if they will be involved.


if you insert a char like that, cin.good() will return false. Therefore for this specific case if cin.good() returns false, simply repeat the input.
1
2
3
4
5
do
{
    cin.clear();
    cin >> a;
}while (!cin.good());


Exceptions are things that would otherwise cause a runtime error like exceeding the bounds of an array or something. To handle that stuff check out:
http://cplusplus.com/reference/std/exception/exception/
http://cplusplus.com/doc/tutorial/exceptions/
Last edited on
So basically i will be forced to loop the cin in case it returns false.
Will this do it?
int a;
while (!(cin >> a))
{
system("CLS");
PrimaryMenuInterface();
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

This loops the cin routine until it returns true, it clears the screen, clears the cin memory, and ignores numeric limits while the cin activity is against the type of variable i am working with.

Is this ok, and if it's not, what will be the issue?
Newbie here:-S.
Topic archived. No new replies allowed.