|
#1
| ||||
| ||||
| 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? |
|
#2
| ||||
| ||||
| First the int thing: Code: int a, b, c; a = 6; b = 3; c = (int) ((a*rand) + b); Code: NSString *pi = @"3.14"; float fpi = [pi floatValue]; Code: NSString *nums = @"3.14, 2.71, 4.0, 10.1"; NSArray *arrayOfNums = [nums componentsSeparatedByString:@", "]; 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. |
|
#3
| ||||
| ||||
| 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. |
|
#4
| ||||
| ||||
| 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 |
|
#5
| ||||
| ||||
| 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; Code: if( !_text )
_text = [NSMutableString stringWithCapacity:10];
[_text appendString:[sender title]]; Code: double d = [_text doubleValue]; -Rob
__________________ There are only 10 kinds of people in the world: Those who understand binary, and those who don't. |
![]() |
| Thread Tools | |
|
|
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 |