I have been learning Objective-C with the Kochan book and I can\'t figure out how to do this exercise program. Only odd numbered exercises are listed online and this one is
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
int number; //store the value the user enter
int lastDigit; //pick of the last digit of the integer
int tempNum; //a temporary storage of the integer the user enter
int count = 0; //used to count how many digits were entered
int count2; //going to be use as a duplicate of count
NSLog(@"Enter an integer");
scanf("%i", &number);
tempNum = number;
//Loop to find out how many digits were entered
for (; number != 0; number /= 10) {
count +=1;
}
//Loop to convert the numbers into words
for (; count != 0; count -= 1) {
count2 = count; //set count2 to count so the for and while loop use them independently
number = tempNum; //restore the value entered by by the user to the number variable
//Loop to reverse the order of the last digit
while (count2 != 0) { //loops to the same number of counts to get the first digit
lastDigit = number % 10; //picks off the last value in the integer
number /= 10; //enables the loop to set the last value of the integer to zero
count2 -=1; //loops one less time to get the numbers from front to back
}
//switch statements
switch (lastDigit) {
case 9:
NSLog(@"nine");
break;
case 8:
NSLog(@"eight");
break;
case 7:
NSLog(@"seven");
break;
case 6:
NSLog(@"six");
break;
case 5:
NSLog(@"five");
break;
case 4:
NSLog(@"four");
break;
case 3:
NSLog(@"three");
break;
case 2:
NSLog(@"two");
break;
case 1:
NSLog(@"one");
break;
case 0:
NSLog(@"zero");
break;
default:
break;
}
}
}
return 0;
}
Since you're adding the numbers to a string, and you want to calculate them right to left, prepend the string with each new number. Something like:
numberString = [NSString stringWithFormat:@"%@ %@", theNewNumber, numberString];
Where theNewNumber is a string (like @"six") and numberString is the string that you want to output once you're done...
(oh, and don't forget to initialize numberString before you start looping...something like:
NSString *numberString = @"";
=====
Based on the code you just posted, you could either do it mathematically, or just pre-pend a string like this:
Put this variable in your .h file:
NSString *numberString;
Then put this in your .m:
- (void) prependNumber:(NSString *)num {
numberString = [NSString stringWithFormat:@"%@ %@", num, numberString];
}
NSLog(@"Type in your integer.");
scanf("%i", &number);
numberString = @"";
do
{
digit = number % 10;
if (digit == 0)
[self prependNumber:@"zero"];
if (digit == 1)
[self prependNumber:@"one"];
if (digit == 2)
[self prependNumber:@"two"];
if (digit == 3)
[self prependNumber:@"three"];
if (digit == 4)
[self prependNumber:@"four"];
if (digit == 5)
[self prependNumber:@"five"];
if (digit == 6)
[self prependNumber:@"six"];
if (digit == 7)
[self prependNumber:@"seven"];
if (digit == 8)
[self prependNumber:@"eight"];
if (digit == 9)
[self prependNumber:@"nine"];
number /= 10;
}
while (number != 0);
NSLog (@"%@", numberString);