Amazon.com Widgets Chapter 5 - Correction of Exercise 8
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 19, 2014, 12:08:45 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 5
| | | |-+ Chapter 5 - Correction of Exercise 8
Pages: [1] Go Down
Print
Author Topic: Chapter 5 - Correction of Exercise 8 (Read 1620 times)
microapple
Jr. Member
**
Posts: 83



WWW Email




on: August 15, 2012, 08:18:40 PM

Code: (Objective-C)
#import <Foundation/Foundation.h>
int main (int argc, char * argv[])
{
    @autoreleasepool {
       
        int sum=0,degit, number=0 ;
       
        NSLog(@"Please enter a valid number :") ;
        scanf("%i",&number);
       
        while(number!=0) {
            degit = number%10;
            sum+=degit;
            number/=10;
        }
       
        NSLog(@"Sum : %i",sum) ;
       
   
   
    }
   
    return 0;
}
Logged

Ivin
Newbie
*
Posts: 2


Email




Reply #1 on: October 31, 2013, 09:28:13 PM

this is very similar to the code i wrote and i has the same bug as mine.

if you enter a number that has more then 10 digits in it it behave strangely.

for example 5555555555 = 46  //should be 50

or 11111111111 = -55 // what

oh.... that must be because the value is to big for the int variable?

i don't know how to test my theory.
Logged
tamwhitenew
Newbie
*
Posts: 3






Reply #2 on: December 29, 2013, 06:32:35 AM

yes you are right if you defined the variables as long integers you shouldn't have this problem...
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.