Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Robot-Scott

macrumors newbie
Original poster
Jan 17, 2009
6
0
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:
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.
*Note: If you want to try running this program in Xcode, make sure you create a new project as Command Line Utility > Foundation Tool.

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!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
What is sum set to when you start your program? I don't think the comma operator does what you think it does.

-Lee

Edit: The expression x = 0 evaluates to 0. That might help.
 

mdeh

macrumors 6502
Jan 3, 2009
345
2
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:

*Note: If you want to try running this program in Xcode, make sure you create a new project as Command Line Utility > Foundation Tool.

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 = 0, 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!

You need to initialize sum, as I did for you.

Also, Kochan has a very nice web-site here

http://www.classroomm.com/objective-c/
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
You need to initialize sum, as I did for you.

Also, Kochan has a very nice web-site here

http://www.classroomm.com/objective-c/

Don't just give it away! =)
You're a few months ahead of him on this book, so a lot of this stuff is obvious now. It's certainly helpful to point out the exact error... the person asking the question gets their program working and can proceed, but they may not have learned as much as they might if they figured it out on their own or with a little nudging.

-Lee
 

mdeh

macrumors 6502
Jan 3, 2009
345
2
Don't just give it away! =)
You're a few months ahead of him on this book, so a lot of this stuff is obvious now. It's certainly helpful to point out the exact error... the person asking the question gets their program working and can proceed, but they may not have learned as much as they might if they figured it out on their own or with a little nudging.

-Lee


Good point lee....Just trying to be as nice to him as you have been to me!! :)
 

jw2002

macrumors 6502
Feb 23, 2008
392
59
Why doesn't the compiler warn that the value is used before it is initialized? This is a pretty cut and dry case to detect cleanly at compile time. It's not like the case of say a function being passed in an uninitialized value can only be detected at run time.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Code:
	int number, next_digit, sum;
	sum, next_digit, number = 0;

Instead of blindly copying correct solutions that have been given here, can you try to look at the second line and explain to us exactly what it does and why? If you manage to do that, it will help you an awful lot understanding C, C++ and Objective-C programming. I mean _understanding_, which is very different from throwing code together until it works, kind of.
 

skochan

macrumors regular
Apr 1, 2006
150
0
California
Code:
sum, next_digit, number = 0;

will only set number to 0, whereas this expression

Code:
sum = next_digit = number = 0;

will set all three variables to 0.

Cheers,

Steve Kochan
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.