Amazon.com Widgets Example 5.8
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 18, 2013, 03:04:04 PM
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
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 5 - Program Looping
| | | |-+  Example 5.8
Pages: [1]   Go Down
Print
Author Topic: Example 5.8  (Read 1023 times)
mjevans
Newbie
*
Posts: 4







« on: July 07, 2009, 01:03:16 PM »

When I tried to input Program Example 5.8, here is what the console of X-Code did after I entered a number:
Quote
The Debugger Debugger is attaching to process
[Session started at 2009-07-07 15:54:13 -0400.]
2009-07-07 15:54:14.010 Chapter5[500:10b] Enter your number.
1234

[Session started at 2009-07-07 15:54:17 -0400.]
GNU gdb 6.3.50-20050815 (Apple version gdb-966) (Tue Mar 10 02:43:13 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 500.

Here is my code. I triple checked it:
Quote
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   int number, right_digit;
   
   NSLog(@"Enter your number.");
   scanf("%i", number);
   
   while ( number != 0 )
   {
      right_digit = number % 10;
      NSLog(@"%i", right_digit);
      number /= 10;
   }
      
[pool drain];
    return 0;
}

What gives?
Logged

MJ
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #1 on: July 07, 2009, 01:05:53 PM »

MJ,

You need an & in front of the variable in the scanf call.

Change

Code: (Objective-C)
  scanf("%i", number);
to

Code: (Objective-C)
  scanf("%i", &number);

Cheers,

Steve Kochan
Logged
mjevans
Newbie
*
Posts: 4







« Reply #2 on: July 07, 2009, 01:18:27 PM »

OK! That was quick!

I actually figured it out after typing example 5.9.

Thanks for the quick response.

Can you explain the message that appeared?
Quote
[Session started at 2009-07-07 15:54:17 -0400.]
GNU gdb 6.3.50-20050815 (Apple version gdb-966) (Tue Mar 10 02:43:13 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 500.
Why didn't it just give me a program error?

Logged

MJ
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #3 on: July 07, 2009, 01:25:20 PM »

Unfortunately, the error doesn't get discovered until runtime, when scanf tries to store the value you entered into the memory address specified by whatever value is stored inside number (some undefined value).  That generates a segmentation fault that throws you into the debugger so that you can try to figure out what went wrong.

There is no compile-time checking done to make sure that the arguments to scanf are correct.  You have to make sure of that yourself.

Cheers,


Steve Kochan
Logged
mjevans
Newbie
*
Posts: 4







« Reply #4 on: July 07, 2009, 01:34:46 PM »

I was kind of thinking that had something to do with it.

Thanks for your fast responses to my meager questions.

I look forward to completing your book. I have enjoyed it and this forum is an unbelievable resource!

 Cool
Logged

MJ
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.