Thread: scanf and int input problem

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    scanf and int input problem

    Been trying to figure out this for an hour with no luck. Learned other things along the way, but not an answer to this problem.

    I've a simple program to use scanf ... enter a number, print a number.

    Code:
    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
    
    	int a;
    	
    	printf ("Enter a number: ");
    	scanf ("%d", &a);
    	printf ("Number is %d\n", a);
    	 
        return 0;
    }
    When I run it, I type in a number and then press Enter. System just sits there, not moving on to the printf command. The only way I could get it to work was to type a number and then actually type in \n before pressing Enter (e.g. 456\n). Then the program would proceed.

    But when I changed my input type to a char, things worked fine with a simple Enter after a char.

    Please help me understand what I'm missing here. Thanks.

    Jim

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't think I know what Foundation/Foundation.h is, so I can't help there. If you use the real
    #include <stdio.h>
    instead, what happens?

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Foundation/Foundation.h seems to be the superset, including stdio.h, that xcode seems to use. But I tried including stdio.h with the same result.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Are you compiling it as Objective-C? Simply naming your file .m instead of .c should invoke the correct GCC module. Your example works for me by the way when I compile it as Objective-C.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    On a Mac, using #include <stdio.h> and using gcc from the command line, everything works perfectly. If I put your code in a Cocoa project, I get 3951 errors, but that's probably me not putting it into Cocoa correctly (since I don't really use it at all -- all the errors are from trying to compile ObjC as real C). So how are you trying to build this project?

    EDIT: Changing your file to a .m and compiling it from the command line with gcc also gives no errors (and no requirement for typing \n). So I'm thinking it definitely is something in xcode....
    Last edited by tabstop; 04-13-2010 at 09:01 PM.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    1
    Thanks for the suggestion

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math calculator and compiler errors
    By littlefermat245 in forum C Programming
    Replies: 2
    Last Post: 10-12-2009, 09:45 PM
  2. Errors in Code i am unfamiliar with, Please Help
    By msween21 in forum C Programming
    Replies: 6
    Last Post: 10-03-2009, 12:06 AM
  3. Frustated with mess table printing
    By benedicttobias in forum C Programming
    Replies: 6
    Last Post: 04-16-2009, 05:50 PM
  4. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  5. code problem
    By kashifk in forum C Programming
    Replies: 6
    Last Post: 04-22-2003, 05:22 PM