I'm working out of "Programming in Objective-C 2.0" by Kochan. I'm in the chapter about loops and I'm on the last exercise. The problem is this:
This is the code I have so far:
Everytime I execute the program I get the same answer of -1881141184.
Instead of staring at it for hours I thought I would ask for help. Thanks!
*Note: If you want to try running this program in Xcode, make sure you create a new project as Command Line Utility > Foundation Tool.Write a program that calculates the sum of the digits of an integer. For example, the sum of the digits of the number 2155 is 2 + 1 + 5 + 5, or 13. The program should accept any arbitrary integer the user types.
This is the code I have so far:
Code:
#import <Foundation/Foundation.h>
// Program to add the digits of a number
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number, next_digit, sum;
sum, next_digit, number = 0;
NSLog(@"Enter your number:");
scanf ("%i", &number);
NSLog(@"The sum of the digits is:");
for ( ; number != 0; number /= 10 )
{
next_digit = number % 10;
sum += next_digit;
}
NSLog(@"%i", sum);
[pool drain];
return 0;
}
Everytime I execute the program I get the same answer of -1881141184.
Instead of staring at it for hours I thought I would ask for help. Thanks!