"scanf" problem in Project Builder

Discussion in 'General Mac Discussion' started by emeraldstone, Sep 7, 2003.

  1. emeraldstone macrumors newbie

    Joined:
    Sep 7, 2003
    Location:
    Ireland
    #1
    I'm using Project Builder.
    When I write a program in C without "scanf", the program runs perfect, however when I use "scanf", it refuses to run correctly. I have to type numbers/letters and press return then it excutes the entire program, In the Project Builder console. I have used other IDE's, there is no error in the code.

    Maybe some one could tell me the equivilent of "scanf" for Objective-C

    Thanks
     
  2. Taft macrumors 65816

    Taft

    Joined:
    Jan 31, 2002
    Location:
    Chicago
    #2
    Could you post the code?

    Also, which project type did you put the code into under Project Builder?

    Taft
     
  3. emeraldstone thread starter macrumors newbie

    Joined:
    Sep 7, 2003
    Location:
    Ireland
    #3
    "scanf" problem in Project Builder

    I selected Cocoa as the project type.
    Cocoa is objective-C, it should understabd C perfectly. If the code contains no "scanf" the code runs fine, as soon as "scanf" is placed in the code it doesn't work correctly. It compiles without errors or warnings though.

    I'm just a beginner, if that matters.

    Here is some code examples:

    this code runs fine:

    #include <stdio.h>

    int main()
    {
    printf("text");

    return 0;
    }

    this doesn't:

    #include <stdio.h>

    int var;

    int main()
    {
    printf("text");
    scanf("%d", var);

    return 0;
    }
     
  4. emeraldstone thread starter macrumors newbie

    Joined:
    Sep 7, 2003
    Location:
    Ireland
    #4
    I should have said, I created a /New Project/Application/Cocoa Application
     
  5. thegfunk macrumors newbie

    Joined:
    Sep 8, 2003
    #5
    the problem is you need to scanf into the address of the variable.
    so, "scanf( "%d", &var);", otherwise you are trying to write the input value to the address specified by var. If you want to get text, then "scanf("%s", somestring);". Generally I wouldn't use scanf to get a string, instead I would use gets. But that also has the downside of introducing security issues, which may not be an issue with your application.

    Grant
     
  6. cubist macrumors 68020

    Joined:
    Jul 4, 2002
    Location:
    Muncie, Indiana
    #6
    Personally, I prefer not to call scanf directly, as it does funny things if you need multiple values. I prefer to use gets and then sscanf, e.g.

    char buf[ 82 ];
    int var1, var2, cnt;

    printf( "prompt:" );
    gets( buf ); // user enters two numbers and presses return

    // sscanf returns the number of items processed from the
    // buffer

    cnt = sscanf( buf, "%d %d", &var1, &var2 );

    if( cnt < 2 ) {
    printf( "you should have entered two numbers\n" );
    return 1;
    }

    ....
     
  7. emeraldstone thread starter macrumors newbie

    Joined:
    Sep 7, 2003
    Location:
    Ireland
    #7
    I'm sorry I made a mistake in the example code above, I left out the &, thats not what I'm doing in my source code.

    I'll try to explain the problem again.

    I can write code without the scanf() function, it compiles, and runs without problems. As soon as I place a scanf() function in the code it causes problems, it compiles, but when the program runs it waits for input when it should be printing text. as soon as it recives input it excutes the entire program when it shouldn't. It also makes the Project Builder window inactive. This is a problem with Project Builder, not the code. I need to know a way to get around it.

    I'm using MacOS 10.2.3,
    Project Builder 2.1
     
  8. thegfunk macrumors newbie

    Joined:
    Sep 8, 2003
    #8
    It sounds like the stdout isn't getting flushed out before the scanf... I forget the call (flush maybe?) but it would be worth a try to flush stdout before calling scanf.

    You might also want to try runing it from the console if you haven't tried that.

    Grant
     
  9. emeraldstone thread starter macrumors newbie

    Joined:
    Sep 7, 2003
    Location:
    Ireland
    #9
    The problem is sorted. PB has a bug. The program runs perfect if I compile and run from the Terminal.
    I read a previous post"Project Builder and C"

    Thanks anyway
     

Share This Page