Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-19-2011, 07:39 PM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 9
linepisode is on a distinguished road
Default calculating problem

hi all,

i am trying to calculate like this:

Code:
float *myResult;
	int myVal=[txt.text floatValue];
        myResult = myVal / 60;

        label1.text = myResult;
it doesnt calculate and doesnt quit the application when i enter 0 - 59 to textfield. but when i enter minimum 60, application closing. where is the problem?
linepisode is offline   Reply With Quote
Old 05-19-2011, 07:53 PM   #2 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

myVal is an int. doing [.... floatValue] will return a float. You are assigning myVal by doing myVal = [... floatValue]. Can you connect the dots?
Domele is offline   Reply With Quote
Old 05-19-2011, 07:57 PM   #3 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 9
linepisode is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
myVal is an int. doing [.... floatValue] will return a float. You are assigning myVal by doing myVal = [... floatValue]. Can you connect the dots?
ok forget this code. I just tried to find right code. Can you get the point what i try to do? I want to make division textfield's value and "60".
linepisode is offline   Reply With Quote
Old 05-19-2011, 08:01 PM   #4 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Your code is almost right. You just need to change one part. You want to assign an int to your variable myVal. You need to get an integer value from your text field. If [...floatValue] returns a float, what do you think the method for getting an int is? If you really don't know or you are really lazy, google it.
Domele is offline   Reply With Quote
Old 05-19-2011, 08:08 PM   #5 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 9
linepisode is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
Your code is almost right. You just need to change one part. You want to assign an int to your variable myVal. You need to get an integer value from your text field. If [...floatValue] returns a float, what do you think the method for getting an int is? If you really don't know or you are really lazy, google it.
i changed my code like this:
Code:
float *myResult;
	int myVal=txt.text;
        myResult = myVal / 60;

        label1.text = myResult;
But same problem again. Application halts when i enter bigger value than 60.
linepisode is offline   Reply With Quote
Old 05-19-2011, 08:16 PM   #6 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

I'm going to put it out there, your code should crash the second it runs. You are trying to assign an int with an NSString. And then you are trying to assign an NSString value with a float. I'm going to try some correct code really quickly to see if it does crash, but I doubt it. Anyways it seems that you are just copying and pasting code from somewhere. Please understand the code before you put it in your project.

Edit: Yep, just tried it. If you do it right it shouldn't crash. If you pay me I'll give you the code but otherwise you need to come up with your own. I can help you but I can't give it to you.

Last edited by Domele; 05-19-2011 at 08:22 PM.
Domele is offline   Reply With Quote
Old 05-19-2011, 08:24 PM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by linepisode View Post
i changed my code like this:
Code:
float *myResult;
	int myVal=txt.text;
        myResult = myVal / 60;

        label1.text = myResult;
But same problem again. Application halts when i enter bigger value than 60.
Code:
	int myVal=txt.text;
This line is wrong. You can't take a string and assign it to an integer.

You had it almost right the first time, with the line


Code:
	float myVal=[txt.text floatValue];
(Except that it should be "float myvalue" not "int myValue")
Now, once you calculate your new value, you want to assign it back to your label. The label.text property is a string. So, you need to convert your result, which is a float, to a string. You can't just set a string to a floating point number. It will give you a compiler warning, and may crash, depending on the value you assign.

So this line:

Code:
        label1.text = myResult;
is VERY wrong. Crash your program wrong.

You need to convert your floating point value to a string.

Take a look at the method stringWithFormat. It lets you build a string out of different parts, and do format conversion while you're at it.

the expression
[NSString stringWithFormat: @"%f", 30.0]

will convert the floating point number 30 to a string, @"30". See if you can adapt that to solve your problem.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 05-19-2011 at 08:28 PM.
Duncan C is offline   Reply With Quote
Old 05-19-2011, 08:35 PM   #8 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 9
linepisode is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
Code:
	int myVal=txt.text;
This line is wrong. You can't take a string and assign it to an integer.

You had it almost right the first time, with the line


Code:
	float myVal=[txt.text floatValue];
(Except that it should be "float myvalue" not "int myValue")
Now, once you calculate your new value, you want to assign it back to your label. The label.text property is a string. So, you need to convert your result, which is a float, to a string. You can't just set a string to a floating point number. It will give you a compiler warning, and may crash, depending on the value you assign.

So this line:

Code:
        label1.text = myResult;
is VERY wrong. Crash your program wrong.

You need to convert your floating point value to a string.

Take a look at the method stringWithFormat. It lets you build a string out of different parts, and do format conversion while you're at it.

the expression
[NSString stringWithFormat: @"%f", 30.0]

will convert the floating point number 30 to a string, @"30". See if you can adapt that to solve your problem.
Thank you Duncan C, i'll try it tomorrow morning. 4:35 AM here
Also thank you, Domele. BTW i'm not copy/paste programmer. I'm newbie and trying improve myself. Sometimes i can make wrong, but i'm learning new things when i do wrong.


Edit: Thank you again Duncan C, it works for me

Last edited by linepisode; 05-20-2011 at 01:29 AM.
linepisode is offline   Reply With Quote
Reply

Bookmarks

Tags
calculate, division, label, textfield, values

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 358
7 members and 351 guests
doffing81, dre, iOS.Lover, jenniead38, Kirkout, PlutoPrime, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:07 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0