image
image

Go Back   macosx.com > Design, Media, Programming & Scripting > Software Programming & Web Scripting

Reply
 
Thread Tools
  #1  
Old August 1st, 2001, 01:55 PM
knighthawk's Avatar
Registered User
 
Join Date: Jul 2001
Location: Los Angeles, CA
Posts: 166
Thanks: 0
Thanked 0 Times in 0 Posts
knighthawk is on a distinguished road
I would appreciate help with strings

I am trying to learn Cocoa, but do not have a solid understanding of C (only been learning it for three months).

I have a project that I need to input a string one number at a time, convert it to a float for calculations, and convert it back to a string for display.

I know that you use the format commands to display the float in a string, but I am unable to get the string to convert to a number.

Also, how do I do this... ("rand" is random number)

int a, b, c;
a = 6;
b = 3;
c = (a*rand) + b;

i know that this is a float calculation, but i only want the result to be an int. Basically I would like to floor the result. Do I have to convert a and b to a float first?
Reply With Quote
  #2  
Old August 1st, 2001, 02:59 PM
rharder's Avatar
Do not read this sign.
 
Join Date: Mar 2001
Location: Virginia, USA
Posts: 1,186
Thanks: 0
Thanked 0 Times in 0 Posts
rharder is on a distinguished road
First the int thing:
Code:
int a, b, c; 
a = 6; 
b = 3; 
c = (int) ((a*rand) + b);
If you have an NSString that contains a number and nothing else, you can convert it to a number with the intValue, floatValue, or doubleValue messages:
Code:
NSString *pi = @"3.14";
float fpi = [pi floatValue];
If you have a bunch of numbers in one string, you'll want to use the componentsSeparatedByString message:
Code:
NSString *nums = @"3.14, 2.71, 4.0, 10.1";
NSArray *arrayOfNums = [nums componentsSeparatedByString:@", "];
That gives you an array of strings. Be careful though: being separated by ', ' means exactly one comma and one space, so you may have to do some massaging of the string.

More info on strings: http://developer.apple.com/techpubs/.../NSString.html

-Rob
__________________
There are only 10 kinds of people in the world:
Those who understand binary, and those who don't.
Reply With Quote
  #3  
Old August 1st, 2001, 03:14 PM
knighthawk's Avatar
Registered User
 
Join Date: Jul 2001
Location: Los Angeles, CA
Posts: 166
Thanks: 0
Thanked 0 Times in 0 Posts
knighthawk is on a distinguished road
Right now what I have is...

//----------------------------------//
char buttonvalue;
char *currentNumber;
int i; //keeps track of the char array for currentNumber
double myRealNumber;

buttonvalue = [sender title]; //only one char for the title
currentNumber[i] = buttonvalue;

myRealNumber = strtod(currentNumber);
//----------------------------------//

this gives me an error:
WARNING! too few arguments to function 'strtod'

is there a "doubleValue" and will it work with (char *)?

I was unable to get the "appendtoString:buttonvalue" function to work with NSMutableString *currentNumber.

I have several C reference books, but some of the things do not work like they have it in the book (like the strtod function).

I really like REALbasic's help reference... mostly because it is searchable. Mac Help is no help most of the time, and the HTML pages for Cocoa API's require a lot of digging.

Reply With Quote
  #4  
Old August 1st, 2001, 04:07 PM
endian's Avatar
Dis Member
 
Join Date: Sep 2000
Location: inside my skin
Posts: 960
Thanks: 0
Thanked 0 Times in 0 Posts
endian is on a distinguished road
Don't even bother with the HTML pages, just go straight to the headers. I'm not at OSX now, or I'd be of more help :\
__________________
g4 400 AGP - 512 RAM
10GB internal (dumping ground) + 60GB (30 GB OSX partition/30 GB OSX Server partition) - 3GB external SCSI (OS9/and I think the Public Beta is still on there somewhere) - DVD-RAM - ZIP

Reply With Quote
  #5  
Old August 1st, 2001, 08:52 PM
rharder's Avatar
Do not read this sign.
 
Join Date: Mar 2001
Location: Virginia, USA
Posts: 1,186
Thanks: 0
Thanked 0 Times in 0 Posts
rharder is on a distinguished road
I think I see what you're trying to do. You can probably save a lot of pain by avoiding the char and char[] stuff.

Does your sender object's title message return a char or an NSString*? Unless you wrote that sender object yourself, I'm guessing it returns an NSString*. Are you making a sort of calculator app?

Here's how I might try this. I'd create an instance variable in your class's header file:
Code:
NSMutableString *_text;
Then in the action code that triggers when someone clicks a number:
Code:
if( !_text )
    _text = [NSMutableString stringWithCapacity:10];
[_text appendString:[sender title]];
Finally, when you needed to make a number out of it:
Code:
double d = [_text doubleValue];
Hope this helps.

-Rob
__________________
There are only 10 kinds of people in the world:
Those who understand binary, and those who don't.
Reply With Quote
Reply

Thread Tools

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 Off
Pingbacks are Off
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ pointers and strings Da_iMac_Daddy Software Programming & Web Scripting 7 October 9th, 2003 10:50 PM
Simple Encryption with strings WeeZer51402 Software Programming & Web Scripting 15 September 25th, 2003 05:16 PM
How do I use strings in OS X? jjones! Software Programming & Web Scripting 5 February 3rd, 2003 02:18 PM
Output to an existing file, and replace strings with arguments? michaelsanford Unix & X11 2 February 1st, 2003 10:00 PM
Working with strings in Obj-C and Cocoa iconara Software Programming & Web Scripting 12 January 12th, 2002 05:47 PM


All times are GMT -5. The time now is 05:10 AM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.