943,747 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4749
  • C++ RSS
Oct 3rd, 2005
0

number format exceptions

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
frank_hfc is offline Offline
3 posts
since Oct 2005
Oct 4th, 2005
0

Re: number format exceptions

... spent another hour searching for this type of exception and nothing has turned up. any takers?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
frank_hfc is offline Offline
3 posts
since Oct 2005
Oct 4th, 2005
0

Re: number format exceptions

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.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Oct 4th, 2005
0

Re: number format exceptions

Quote originally posted by Stoned_coder ...
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:

C++ Syntax (Toggle Plain Text)
  1. try
  2. {
  3. fracNum = Integer.parseInt(JOptionPane.showInputDialog( Assign3.this,"Enter Numerator") );
  4. fracDen = Integer.parseInt(JOptionPane.showInputDialog( Assign3.this,"Enter Denominator") );
  5. frac.set( fracNum,fracDen );
  6. myCQ.enqueue(frac);}
  7.  
  8. 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).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
frank_hfc is offline Offline
3 posts
since Oct 2005
Oct 4th, 2005
0

Re: number format exceptions

well in c++ you dont need to do that. you can get input like this....
C++ Syntax (Toggle Plain Text)
  1. int an_int;
  2. while( ! cin>>an_int )
  3. {
  4. // didnt get an int so deal with that here
  5. cin.clear(); // clear error flags
  6. cin.ignore(numeric_limits<streamsize>::max(),'\n'); // clear junk input from cin
  7. }
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: summation program
Next Thread in C++ Forum Timeline: Need help with while loop





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC