7 Replies - 6818 Views - Last Post: 19 August 2011 - 10:34 AM

#1 Synerate  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 24-March 09

Scanf & X-code 4 issues

Posted 18 August 2011 - 09:46 AM

I am at my wits end with this in my code.
I am following a code example in "Programming in Objective-c"

here is the code example:


#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>


int main(int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    

    int n, number, triangularNumber;
    
    NSLog(@"What triangular number do you want?");
    
    scanf("%i", &number);
    
    
    triangularNumber = 0;
    
    for( n = 1; n <= number; ++n)
    {
        triangularNumber += n;
    }
        NSLog(@"Triangular number %i is %i\n", number, triangularNumber);
    
    
    
    [pool drain];
    return 0;
}


The issue is that it just goes straight through the entire program without waiting for user input. I've tried tricking with conditional statements but it just seems to not want to get any user input.

I've crawled the web for about day on this, so if anyone has had a similar experience, please let me know. Im Using X-code and obj-c, im fairly new to it. But Ive been doing C/C++ for a little more then 3 years just to give a little background on my C knowledge.

Appreciate the help in advance
~Syn

This post has been edited by Synerate: 18 August 2011 - 09:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Scanf & X-code 4 issues

#2 GWatt  Icon User is offline

  • member icon

Reputation: 262
  • View blog
  • Posts: 3,054
  • Joined: 01-December 05

Re: Scanf & X-code 4 issues

Posted 18 August 2011 - 11:24 PM

It could be garbage on stdin, or it could be that mac doesn't support the '%i' format string. Not having access to a mac I can't test the second theory. If using '%d' instead doesn't fix your problem, try seeing what's on stdin instead of reading to an int.
char str[100] = { 0 };

scanf("%99s", str);

NSLog(@"from STDIN: '%s'", str);



If that yields something then there is definitely garbage on stdin. If nothing happens, then you could try stepping through scanf in a debugger, but that's going to be much more complicated.
Was This Post Helpful? 0
  • +
  • -

#3 Synerate  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 24-March 09

Re: Scanf & X-code 4 issues

Posted 19 August 2011 - 06:46 AM

View PostGWatt, on 19 August 2011 - 12:24 AM, said:

It could be garbage on stdin, or it could be that mac doesn't support the '%i' format string. Not having access to a mac I can't test the second theory. If using '%d' instead doesn't fix your problem, try seeing what's on stdin instead of reading to an int.
char str[100] = { 0 };

scanf("%99s", str);

NSLog(@"from STDIN: '%s'", str);



If that yields something then there is definitely garbage on stdin. If nothing happens, then you could try stepping through scanf in a debugger, but that's going to be much more complicated.



%i has worked for me in the past. Ill try to see if there's garbage in it.
I really appreciate the reply.


edit:
I tried your example and it did the same exact thing, which is skip the scanf and just go till return 0.
nothing was in the stdin' '

This post has been edited by Synerate: 19 August 2011 - 06:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 GWatt  Icon User is offline

  • member icon

Reputation: 262
  • View blog
  • Posts: 3,054
  • Joined: 01-December 05

Re: Scanf & X-code 4 issues

Posted 19 August 2011 - 07:19 AM

So I was able to sit down at a mac computer today, but I am unable to replicate your bug. I have a couple suggestions.
1) Recompile your program with all of the warning options on. Assuming you compiled from the terminal, your command should look something like this:
gcc -o triangle triangle.m -framework Foundation -W -Wall -Wextra -pedantic -Werror

2) Write a very simple program using scanf to see if it's more than just that other program. If more things are experiencing this strange behavior something else (I don't know what) is probably the cause.
Was This Post Helpful? 1
  • +
  • -

#5 Synerate  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 24-March 09

Re: Scanf & X-code 4 issues

Posted 19 August 2011 - 08:00 AM

View PostGWatt, on 19 August 2011 - 08:19 AM, said:

So I was able to sit down at a mac computer today, but I am unable to replicate your bug. I have a couple suggestions.
1) Recompile your program with all of the warning options on. Assuming you compiled from the terminal, your command should look something like this:
gcc -o triangle triangle.m -framework Foundation -W -Wall -Wextra -pedantic -Werror

2) Write a very simple program using scanf to see if it's more than just that other program. If more things are experiencing this strange behavior something else (I don't know what) is probably the cause.


ok I can try but im not sure how to do a terminal mode in X-code4. Ill look into it.

What im investigating now is if I need to #include/import something. And for a moment I thought I need #import <stdio>
But that did not change anything *Sigh*

But Ill try to get a terminal program going, as far as I know the debugger(gdb) is always on in Xcode.
Again I appreciate you taking time to bounce ideas off with me on this.

~Syn
Was This Post Helpful? 0
  • +
  • -

#6 GWatt  Icon User is offline

  • member icon

Reputation: 262
  • View blog
  • Posts: 3,054
  • Joined: 01-December 05

Re: Scanf & X-code 4 issues

Posted 19 August 2011 - 08:05 AM

Mac OS comes with a terminal emulator. It's called "Terminal." You can quickly launch it by pressing command+space and searching for "terminal"
Was This Post Helpful? 0
  • +
  • -

#7 Synerate  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 24-March 09

Re: Scanf & X-code 4 issues

Posted 19 August 2011 - 10:04 AM

View PostGWatt, on 19 August 2011 - 09:05 AM, said:

Mac OS comes with a terminal emulator. It's called "Terminal." You can quickly launch it by pressing command+space and searching for "terminal"



It works in Terminal, I'm convinced there's something about X-Code4 that i don't understand.
Now I know where the problem lies now.
Thanks for your help, If you have additional input on how to debug this issue in X-code4 I'm all ears ^_^
~Syn
Was This Post Helpful? 0
  • +
  • -

#8 GWatt  Icon User is offline

  • member icon

Reputation: 262
  • View blog
  • Posts: 3,054
  • Joined: 01-December 05

Re: Scanf & X-code 4 issues

Posted 19 August 2011 - 10:34 AM

I think that the XCode console is simply there to show program output. I don't think it's supposed to be interactive.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1