Jul 25, 2008

..what Happens If

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

..what Happens If

8bit
What happens if I assigned a string to an integer? Because I used a try statement for it and the catch didn't get invoked! Does it just assign a to 0!?

Reply

leiaah
QUOTE(8bit @ Feb 19 2005, 08:41 AM)
What happens if I assigned a string to an integer? Because I used a try statement for it and the catch didn't get invoked! Does it just assign a to 0!?
*




I suppose you're using Java.

You can easily assign a String to an Integer by using parseInt.

String s = "777";
int x = Integer.parseInt(s);

Reply

osknockout
QUOTE
I suppose you're using Java.

You can easily assign a String to an Integer by using parseInt.

String s = "777";
int x = Integer.parseInt(s);
Ok, let's think about this. This is the C/C++ programming forum. What 8bit just said makes perfect sense in C++ (exception handling), so it's probably not Java laugh.gif Just a thought leeiah.
QUOTE
What happens if I assigned a string to an integer? Because I used a try statement for it and the catch didn't get invoked! Does it just assign a to 0!?
Could you please state what compiler you're using (version also) and perhaps the source code? Normally that would not even compile with what I'm using.

Reply

dexter
I can't see it being an accidental thing, as if it were, without casting it, the compiler would complain.

So, if you did cast it... like int x = (int)"777"; then it wouldn't be caught because it wouldn't fail. It wouldn't be correct, but it definately wouldn't fail...

I'm still trying to fathom the need for crossing types in this case... if you want to assign a the value in a string to an int, you'd just use atoi().

I'm really interested to see why this is necessary. More details would be nice.

Reply

8bit
I used MingW and wrote the source with: CONText...but I might be able to find out the problem. And uh...leeiah this is a C/C++ board...why would I post Java stuff here? Very hard question, eh?

Reply

osknockout
I mean source code as in what you wrote...
MingW eh? I only use plain old gcc, so I can't help you with that one.
QUOTE
Very hard question, eh?

Well yes and no, see, it's not defined in the standard as to whether or not you can do so. Why don't you compile it and give it a try? We're all stumped.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics
Looking for ..what, Happens, If

Searching Video's for ..what, Happens, If
advertisement



..what Happens If



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE