0

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.

2
Contributors
4
Replies
5
Views
12 Years
Discussion Span
Last Post by Stoned_coder
0

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

0

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.

0

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).

0

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
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.