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?
First the int thing:
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:
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:
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...Foundation/ObjC_classic/Classes/NSString.html

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 = 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.

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 :\
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:
NSMutableString *_text;
Then in the action code that triggers when someone clicks a number:
if( !_text )
    _text = [NSMutableString stringWithCapacity:10];
[_text appendString:[sender title]];
Finally, when you needed to make a number out of it:
double d = [_text doubleValue];
Hope this helps.