I'm stuck on integer math, exercise 5-8 from Kochan book

Discussion in 'Mac Programming' started by Robot-Scott, Mar 7, 2009.

1. Robot-Scott macrumors newbie

Joined:
Jan 17, 2009
#1
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;
sum, next_digit, number = 0;

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!

2. lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#2
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.

3. mdeh macrumors 6502

Joined:
Jan 3, 2009
#3
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/

4. lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#4
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

5. mdeh macrumors 6502

Joined:
Jan 3, 2009
#5

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

6. jw2002 macrumors 6502

Joined:
Feb 23, 2008
#6
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.

7. gnasher729 macrumors G5

Joined:
Nov 25, 2005
#7
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.

8. skochan macrumors regular

Joined:
Apr 1, 2006
Location:
California
#8
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

9. Robot-Scott thread starter macrumors newbie

Joined:
Jan 17, 2009
#9
thanks

Thanks all, I got it to work with your help!