Member Avatar

is there an exception type in c++ like the NumberFormatException in java? i'm looking to use a try/catch to do type checking after a cin input from the user.

Member Avatar

... spent another hour searching for this type of exception and nothing has turned up. any takers?

perhaps if you explain better what you are trying to do. maybe a small code sample. Why would you need to type check after a cin.

Member Avatar

perhaps if you explain better what you are trying to do. maybe a small code sample. Why would you need to type check after a cin.

here's what i'm familiar with doing in java:

try
{
fracNum = Integer.parseInt(JOptionPane.showInputDialog( Assign3.this,"Enter Numerator") );
fracDen = Integer.parseInt(JOptionPane.showInputDialog( Assign3.this,"Enter Denominator") );
frac.set( fracNum,fracDen );
myCQ.enqueue(frac);}

catch ( NumberFormatException n ) {JOptionPane.showMessageDialog( Assign3.this,("Please enter an integer."));}

what i'm looking for in c++ is to make sure the input from the user is only a number (float specifically).

well in c++ you dont need to do that. you can get input like this....

int an_int;
while( ! cin>>an_int )
{
   // didnt get an int so deal with that here
   cin.clear(); // clear error flags
   cin.ignore(numeric_limits<streamsize>::max(),'\n'); // clear junk input from cin
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.